mybatis中resultMap使用之返回分组数据

来源:互联网 发布:矩阵lu分解例题3*3 编辑:程序博客网 时间:2024/06/11 18:16

1. resultMap

1.1 引言

resultMap是mybatis最重要的强大元素。通过描述数据的关系结构,将结果集进行映射到java类或java bean中,达到结果集复杂处理的目的。本文解决的主要问题的分组数据的返回

1.2 问题

假设有如下sql查询语句

select id, otherId from mytalbe 

该sql查询语句查询结果如下

id   otherId01   0000101   0000201   0000302   00004

有java实体类如下

class Id{  private String id;  private List<String> otherId;}

怎样通过一次查询将结果以分组的形式放到List< Id >中呢?
通过执行一次查询,返回如下形式的数据

List<Id> aa = new ArrayList<Id>();aa = ...//通过mybatis执行slqaa:[     {01,[00001,00002,00003,00004]},     {02,[00004]}    ]

解决上述问题呢,就需要用到resultMap了

2. 带有collection 属性的resultMap

<resultMap id="myid" type="Id">  <id Property="id" column="id">  <collecton property="otherId" ofType="String" javaType="ArrayList">     <result colume="otherId">  </collection></resultMap>

问题解决,上述的关系映射足以满足要求。需要注意的是< id > 标签,这是分组的标识。一定要注意。
关于resultMap各标签的意义,请自行查资料吧。