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>