如何在myBatis中映射一对多关系?

时间:2016-08-24 01:49:18

标签: java database orm mybatis

所以我有下表,我必须映射到Java Objects:

+---------+-----------+---------------------+---------------------+--------+
| task_id | attribute | lastModified        | activity            | row_id |
+---------+-----------+---------------------+---------------------+--------+
|       1 |         1 | 2016-08-23 21:05:09 | first activity      |      1 |
|       1 |         3 | 2016-08-23 21:08:28 | connect to db       |      2 |
|       1 |         3 | 2016-08-23 21:08:56 | create web services |      3 |
|       1 |         4 | 2016-08-23 21:08:56 | data dump           |      4 |
|       1 |         5 | 2016-08-23 21:08:56 | test cases          |      5 |
|       1 |         6 | 2016-08-23 21:08:57 | dao object          |      6 |
|       1 |         7 | 2016-08-23 21:08:57 | buy streetfood      |      7 |
|       2 |         6 | 2016-08-23 21:08:57 | drink coke          |      8 |
|       2 |         6 | 2016-08-23 21:09:00 | drink tea           |      9 |
|       2 |         1 | 2016-08-23 21:12:30 | make tea            |     10 |
|       2 |         2 | 2016-08-23 21:13:32 | charge phone        |     11 |
|       2 |         3 | 2016-08-23 21:13:32 | shower              |     12 |
|       2 |         4 | 2016-08-23 21:13:32 | sleep               |     13 |
+---------+-----------+---------------------+---------------------+--------+

此处,每个Task对象(由task_id列标识)都有多个attribute个对象。这些属性对象包含lastModifiedactivity字段。到目前为止,我的方法是创建一个Row对象,通过myBatis将表的每一行映射到Row对象。然后做一些Java端处理来排序所有内容。有没有办法通过myBatis注释和/或xml直接映射此表,以便创建2个Task对象,每个对象都有一个填充的Atttribute对象列表?

1 个答案:

答案 0 :(得分:0)

以下是mybatis文档:http://www.mybatis.org/mybatis-3/sqlmap-xml.html。您可以使用mybatis集合来解决您的问题。