resultmap标签是mybatis框架中罕用的一个元素,也是十分重要的映射元素,用于实现mybatis的高级映射.

其利用场景:

1)表中字段与pojo类中属性名不统一时,(如stu_id/stuId).

<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层的办法.

<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数组中,单个数据的类型.

<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>