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

1.引言

resultMap是mybatis最重要的强大元素。通过描述数据的关系结构,

将结果集进行映射到java类或javabean中,

达到结果集复杂处理的目的。本文解决的主要问题的分组数据的返回

2.问题

假设有如下sql查询语句

select id, otherId from mytalbe

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

id otherId

01 00001

01 00002

01 00003

02 00004

有java实体类如下:

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

怎样通过一次查询将结果以分组的形式放到List中呢?

通过执行一次查询,返回如下形式的数据

List aa = new ArrayList();

aa = …//通过mybatis执行slq

aa:
[{01,[00001,00002,00003,00004]},

{02,[00004]}]

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

3.带有collection 属性的resultMap

<resultMap id=DevCodeTag type=iguard.health.entity.tag.DevCodeTag>
<collection property=tag ofType=iguard.health.entity.tag.Tag>
<id column=dev_code property=devCode jdbcType=VARCHAR />
<result column=label_name property=labelName jdbcType=VARCHAR/>
<result column=id property=id jdbcType=VARCHAR/>
</collection>
</resultMap>

问题解决,上述的关系映射足以满足要求。需要注意的是< id >

标签,这是分组的标识。一定要注意。

关于resultMap各标签的意义,请自行查资料吧。


-------------本文结束感谢您的阅读-------------
0%