resultMap mybatis高级映射
resultMap是mybatis中非常重要的一种映射元素,用于实现mybatis中的高级映射:
应用场景:
1)表中字段与类中属性名字不一致
2)sql嵌套查询
3)多表关联查询
<resultMap type="com.cy.pj.sys.pojo.SysRoleMenu" //type指向pojo对象的全路径名
id="sysRoleMenu">//id:标识这个resultmap
<id property="id" column="id"/>//进行映射 property:是数据对象的属性,column:是数据表中的列
<result property="name" column="name"/> //主键用id 非主键用result
<result property="note" column="note"/>
<collection property="menuIds" //通过column 的值调用命名空间中的方法查询结果映射到property
column="id"
select="com.cy.pj.sys.dao.SysRoleMenuDao.findMenuIdsByRoleId">
</collection>
</resultMap>
<select id="findById"
resultMap="sysRoleMenu">//指定使用哪一个resultmap
select id,name,note
from sys_roles
where id=#{id}
</select>
<resultMap type="com.cy.pj.sys.pojo.SysRoleMenu"
id="sysRoleMenu">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="note" column="note"/>
<collection property="menuIds" ofType="integer"> //当查询出来的多个值属于对象的一个属性,ofType:menuIds中值的类型
<result column="menu_id"/>///映射:将查出的menu_id映射到menuIds中的一个值
</collection>
</resultMap>

浙公网安备 33010602011771号