Mybatis連結3表查詢資料resultMap結果映射
一、前言
Mybatis實現了sql與java代碼的分離,達到瞭解耦合的目的,配置sql語句時有個resultType=""的屬性,用於定義sql查詢返回結果資料類型,但它有局限性,就是當連表查詢的時候,你很難說定義返回的是某一個類型,這時就需要用到一個標籤了,那就是resultMap,結果映射.
二、從sql查詢結果到模型實體
在深入ResultMap標籤前,我們需要瞭解從SQL查詢結果集到JavaBean或POJO實體的過程。
1. 通過JDBC查詢得到ResultSet對象
2. 遍曆ResultSet對象並將每行資料暫存到HashMap執行個體中,以結果集的欄位名或欄位別名為鍵,以欄位值為值
3. 根據ResultMap標籤的type屬性通過反射執行個體化領域模型
4. 根據ResultMap標籤的type屬性和id、result等標籤資訊將HashMap中的索引值對,填充到領域模型執行個體中並返回
三、ResultMap標籤
id屬性:標識resultMap,通過它去識別.
type屬性:傳回值類型,類的全定向名.
autoMapping屬性:值為true(預設)|false,是否自動對應。自動對應功能就是自動尋找與欄位名小寫同名的屬性名稱,並調用setter方法。而設定為false後,則需要在`resultMap`內明確註明映射關係才會調用對應的setter方法。
四、ResultMap中子標籤
在講標籤時先講述下資料庫中表資料的對應關係,如一對一,一對多,多對多等關係,具體來說,如有一個俱樂部表,一個全員表,一個俱樂部裡有許多球員,而許多球員對應一個俱樂部,則俱樂部與球員之間的關係就是一對多與多對一的關係。
<collection>子標籤:對應表格關係中的多
<association>子標籤:對應表格關係中的一
五、連接三表示例
樣本是連接三表,查詢結果作為示範,就算連接再多表也能舉一反三。這三個表的關係如下圖
sql語句連接:http://download.csdn.net/detail/sunrise_zhu/9687991 核心代碼: clubMapper.xml中結果映射
<!-- 結果映射 --><resultMap type="com.sxt.entity.Club" id="clubBean" autoMapping="true"><!--column指向資料庫列名 property指向pojo對象中欄位名--><result column="cid" property="cid"/><result column="cname" property="cname"/><result column="city" property="city"/><!-- property指的是在bean中欄位名 ofType類的全定向名 --><collection property="players" ofType="com.sxt.entity.Player"><result column="pid" property="pid"/><result column="pname" property="pname"/><result column="position" property="position"/><result column="cid" property="cid"/><association property="abilities" javaType="com.sxt.entity.Abilities"><result column="aid" property="aid"/><result column="pid" property="pid"/><result column="shoot" property="shoot"/></association></collection></resultMap>
clubMapper.xml中sql語句
<select id="joinTwo" resultMap="clubBean">select c.*,p.*,a.* from clubs c join player pon c.cid = p.cid join abilities a on a.pid = p.pid;</select>
playerMapper.xml中的結果映射:
<!-- 結果映射 --><resultMap type="com.sxt.entity.Player" id="playerBean"><!--column指向資料庫列名 property指向pojo對象中欄位名 --><result column="pid" property="pid" /><result column="pname" property="pname" /><result column="position" property="position" /><result column="cid" property="cid" /><association property="club" javaType="com.sxt.entity.Club"><result column="cid" property="cid" /><result column="cname" property="cname" /><result column="city" property="city" /></association><association property="abilities" javaType="com.sxt.entity.Abilities"><result column="aid" property="aid"/><result column="pid" property="pid"/><result column="shoot" property="shoot"/></association></resultMap>
abilitiesMapper.xml結果映射
<!-- 結果映射 --><resultMap type="com.sxt.entity.Abilities" id="abilitiesBean"><!--column指向資料庫列名 property指向pojo對象中欄位名 --><result column="aid" property="aid"/><result column="pid" property="pid"/><result column="shoot" property="shoot"/><association property="player" javaType="com.sxt.entity.Player"><result column="pid" property="pid"/><result column="pname" property="pname"/><result column="position" property="position"/><result column="cid" property="cid"/></association>
五、參考檔案
http://www.cnblogs.com/fsjohnhuang/p/4076592.html