resultmap标签是mybatis框架中经常使用的一个元素,也是很是重要的映射元素,用于实现mybatis的高级映射.sql
其应用场景:数组
1)表中字段与pojo类中属性名不一致时,(如stu_id/stuId).mybatis
<resultMap type="返回类型全限定名" id="命名"> <id property="id" column="id" /> <result property="name" column="name"/> <result property="note" column="note"/> </resultMap>
使用方法如上,type为返回类型全限定名,id为本身为resultmap定义的名字,在sql标签上resultmap出写id,resultmap中<id>必须是表中定义的主键,<result>为表中其余属性,其中的property属性为pojo定义的属性名,column为表中的字段名.框架
2)sql语句嵌套查询:当咱们查询的是1对n关系时,能够选择collection元素,一样也是property属性为pojo定义的属性名,column为表中的字段名,select属性指向全限定名的另外一个Dao层的方法.code
<resultMap type="返回类型全限定名" id="命名"> <id property="id" column="id" /> <result property="name" column="name"/> <result property="note" column="note"/> <!-- collection通常应用于one2many查询 --> <collection property="menuIds" column="id"> <select="全限定类名"/> </collection> </resultMap>
3)多表关联查询:经过左外或者右外链接,将所需的表关联在一块儿进行查询,将基准表的信息写在<id><result>中,关联的表一个表写在一个<collection>中,ofType属性为property数组中,单个数据的类型.io
<select id="findObjectById" resultMap="sysRoleMenuVo"> select r.id,r.name,r.note,rm.menu_id from sys_roles r left join sys_role_menus rm on r.id=rm.role_id where r.id=#{id} </select> <resultMap type="com.cy.pj.sys.pojo.SysRoleMenuVo" id="sysRoleMenuVo"> <id property="id" column="id" /> <result property="name" column="name"/> <result property="note" column="note"/> <!-- collection通常应用于one2many查询 --> <collection property="menuIds" ofType="integer"> <result column="menu_id"/> </collection> </resultMap>