1. 程式人生 > 程式設計 >MyBatis中resultMap和resultType的區別詳解

MyBatis中resultMap和resultType的區別詳解

總結

基本對映 :(resultType)使用resultType進行輸出對映,只有查詢出來的列名和pojo中的屬性名一致,該列才可以對映成功。(資料庫,實體,查詢欄位,這些全部都得一一對應)高階對映 :(resultMap) 如果查詢出來的列名和pojo的屬性名不一致,通過定義一個resultMap對列名和pojo屬性名之間作一個對映關係。(高階對映,欄位名稱可以不一致,通過對映來實現

resultType和resultMap功能類似 ,都是返回物件資訊 ,但是resultMap要更強大一些 ,可自定義。因為resultMap要配置一下,表和類的一一對應關係,所以說就算你的欄位名和你的實體類的屬性名不一樣也沒關係,都會給你映射出來,但是,resultType就比較雞肋了,必須欄位名一樣,比如說 cId和c_id 這種的都不能對映 。下面介紹幾個常用的對映關係:

MyBatis中resultMap和resultType的區別詳解

MyBatis中resultMap和resultType的區別詳解

單表查詢: resultMap:當使用resultMap做SQL語句返回結果型別處理時,通常需要在mapper.xml中定義resultMap進行pojo和相應表字段的對應。

訂單查詢關聯使用者的resultMap

將整個查詢的結果對映到cn.itcast.mybatis.po.Orders中

<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserResultMap">
  <!-- 配置對映的訂單資訊 -->
  <!-- id:指定查詢列中的唯 一標識,訂單資訊的中的唯 一標識,如果有多個列組成唯一標識,配置多個id
    column:訂單資訊的唯 一標識 列
    property:訂單資訊的唯 一標識 列所對映到Orders中哪個屬性
   -->
  <id column="id" property="id"/>
  <result column="user_id" property="userId"/>
  <result column="number" property="number"/>
  <result column="createtime" property="createtime"/>
  <result column="note" property="note"/>    
</resultMap>

關聯查詢(一對一):resultMap對於一對一表連線的處理方式通常為在主表的pojo中新增巢狀另一個表的pojo,然後在mapper.xml中採用association節點元素進行對另一個表的連線處理。例如

訂單查詢關聯使用者的resultMap

將整個查詢的結果對映到cn.itcast.mybatis.po.Orders中

<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserResultMap">
  <!-- 配置對映的訂單資訊 -->
  <!-- id:指定查詢列中的唯 一標識,訂單資訊的中的唯 一標識,如果有多個列組成唯一標識,配置多個id
    column:訂單資訊的唯 一標識 列
    property:訂單資訊的唯 一標識 列所對映到Orders中哪個屬性
   -->
  <id column="id" property="id"/>
  <result column="user_id" property="userId"/>
  <result column="number" property="number"/>
  <result column="createtime" property="createtime"/>
  <result column="note" property=note/>
  
  <!-- 配置對映的關聯的使用者資訊 -->
  <!-- association:用於對映關聯查詢單個物件的資訊
  property:要將關聯查詢的使用者資訊對映到Orders中哪個屬性
   -->
  <association property="user" javaType="cn.itcast.mybatis.po.User">
    <!-- id:關聯查詢使用者的唯 一標識
    column:指定唯 一標識使用者資訊的列
    javaType:對映到user的哪個屬性
     -->
    <id column="user_id" property="id"/>
    <result column="username" property="username"/>
    <result column="sex" property="sex"/>
    <result column="address" property="address"/>
  
  </association>
</resultMap>

關聯查詢(一對多):resultMap的處理方式為在訂單表資料的pojo中新增一個list,list中為訂單明細表的屬性,在mapper.xml中採用如下的處理方式:

訂單及訂單明細的resultMap

使用extends繼承,不用在中配置訂單資訊和使用者資訊的對映

<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersAndOrderDetailResultMap" extends="OrdersUserResultMap">
  <!-- 訂單資訊 -->
  <!-- 使用者資訊 -->
  <!-- 使用extends繼承,不用在中配置訂單資訊和使用者資訊的對映 -->
  
  
  <!-- 訂單明細資訊
  一個訂單關聯查詢出了多條明細,要使用collection進行對映
  collection:對關聯查詢到多條記錄對映到集合物件中
  property:將關聯查詢到多條記錄對映到cn.itcast.mybatis.po.Orders哪個屬性
  ofType:指定對映到list集合屬性中pojo的型別
   -->
   <collection property="orderdetails" ofType="cn.itcast.mybatis.po.Orderdetail">
     <!-- id:訂單明細唯 一標識
     property:要將訂單明細的唯 一標識 對映到cn.itcast.mybatis.po.Orderdetail的哪個屬性
      -->
     <id column="orderdetail_id" property="id"/>
     <result column="items_id" property="itemsId"/>
     <result column="items_num" property="itemsNum"/>
     <result column="orders_id" property="ordersId"/>
   </collection>
</resultMap>

association:

作用:
將關聯查詢資訊對映到一個pojo物件中

場合:
為了方便查詢關聯查詢可以使用assocation將關聯查詢資訊對映為使用者物件的pojo屬性中。

比如:查詢訂單及關聯使用者資訊
使用resultType無法查詢結果對映到pojo物件的pojo屬性中,根據對結構集查詢遍歷的需要選擇使用resultType還是resultMap。

collection:

作用:
將關聯查詢資訊對映到一個list集合中。
場合:
為了方便查詢遍歷關聯資訊可以使用cellection將關聯資訊對映到list集合中。
比如:
查詢使用者許可權範圍模組及模組下的選單,可使用collection將模組對映到模組list中將選單列表對映到模組物件的選單list屬性中,這樣做的目的也是方便對查詢結果集進行遍歷如果使用resultType無法將查詢結果對映到list集合中

到此這篇關於mybaties中resultMap和resultType的區別詳解的文章就介紹到這了,更多相關mybaties中resultMap和resultType的區別內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!