resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中。
<!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性--><resultMap id="唯一的标识" type="映射的pojo对象"><id column="表的主键字段,或者可以为查询语句中的别名字段"jdbcType="字段类型" property="映射pojo对象的主键属性" /><result column="表的一个字段(可以为任意表的一个字段)"jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/><association property="pojo的一个对象属性"javaType="pojo关联的pojo对象"><id column="关联pojo对象对应表的主键字段" jdbcType="字段类型"property="关联pojo对象的主席属性"/><result column="任意表的字段" jdbcType="字段类型"property="关联pojo对象的属性"/></association><!-- 集合中的property须为oftype定义的pojo对象的属性--><collection property="pojo的集合属性" ofType="集合中的pojo对象"><id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型"property="集合中pojo对象的主键属性" /><result column="可以为任意表的字段" jdbcType="字段类型"property="集合中的pojo对象的属性" /></collection></resultMap>
association 应用场景:many2one or one2one
<resultMap type="com.db.sys.vo.SysUserDeptVo"
id="sysUserDeptVo">
<!--
1)association 用于定义关联查询
2)association 应用场景:many2one or one2one-->
<association property="sysDept"
column="deptId"
select="com.db.sys.dao.SysDeptDao.findById">
</association>
</resultMap>
<select id="findPageObjects"
resultMap="sysUserDeptVo">
select *
from sys_users
<include refid="queryWhereId"/>
order by createdTime desc
limit #{startIndex},#{pageSize}
</select>
collection应用场景: one2many
<resultMap type="com.db.sys.vo.SysRoleMenuVo"
id="sysRoleMenuVo">
<!-- one2many(基于角色id查询菜单id)
id:角色id
menuIds:SysRoleMenuVo 中的setMenuIds方法 -->
<collection property="menuIds"
column="id"
select="com.db.sys.dao.SysRoleMenuDao.findMenuIdsByRoleId">
</collection>
</resultMap>
<select id="findObjectById"
resultMap="sysRoleMenuVo">
select id,name,note
from sys_roles
where id=#{id}
</select>
