Mybatis連3表查詢資料resultMap結果映射__mybatis

來源:互聯網
上載者:User
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








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.