1. 程式人生 > >mybatis高階對映總結

mybatis高階對映總結

一、介紹
resultType :
作用:將查詢結果按照sql列名pojo屬性名一致性對映到pojo中。
場合:

常見一些明細記錄的展示,比如使用者購買商品明細,將關聯查詢資訊全部展示在頁面時,此時可直接使用 resultType 將每一條記錄對映到 pojo 中,在前端頁面遍歷 list ( list 中是 pojo )即可。

resultMap :

使用 association 和 collection 完成一對一和一對多高階對映(對結果有特殊的對映要求)。

association :

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

場合:

為了方便查詢關聯資訊可以使用 association 將關聯訂單資訊對映為使用者物件的 pojo 屬性中,比如:查詢訂單及關聯使用者資訊。

使用 resultType 無法將查詢結果對映到 pojo 物件的 pojo 屬性中,根據對結果集查詢遍歷的需要選擇使用 resultType 還是 resultMap 。

collection :

作用:將關聯查詢資訊對映到一個list集合中。

場合:

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

如果使用 resultType 無法將查詢結果對映到 list 集合中。

二、一對一查詢

resultType 和 resultMap 實現一對一查詢小結:

a.resultType :使用 resultType 實現較為簡單,如果 pojo 中沒有包括查詢出來的列名,需要增加列名對應的屬性,即可完成對映。

b.如果沒有查詢結果的特殊要求建議使用 resultType 。

c.resultMap :需要單獨定義 resultMap ,實現有點麻煩,如果對查詢結果有特殊的要求,使用 resultMap 可以完成將關聯查詢對映 pojo 的屬性中。

d.resultMap 可以實現延遲載入, resultType 無法實現延遲載入。

2.1.需求:查詢訂單資訊,關聯查詢使用者資訊;

2.2 resultType實現

public class Orders {
  /** 主鍵訂單Id */
  private Integer id;
  /** 下單使用者id */
  private Integer userid;
  /** 訂單號 */
  private String number;
  /** 建立訂單時間 */
  private Date createTime;
  /** 備註 */
  private String note;
  // 使用者資訊
  private User user;
  // 訂單明細
  private List<OrderDetail> orderdetails;
     //  getter and setter ......
}

public class OrdersCustom extends Orders {
  // 新增使用者的屬性
  private String username;
  private String sex;
  private String address;
    // getter and setter......
}


public interface OrdersCustomMapper {
    /** 查詢訂單,關聯查詢使用者資訊 */
    public List<OrdersCustom> findOrdersUser();
}
<mapper namespace="com.mybatis.mapper.OrdersCustomMapper">
    <!-- 查詢訂單,關聯查詢使用者資訊 -->
  <select id="findOrdersUser" resultType="com.mybatis.entity.OrdersCustom">
  SELECT t1.*,
    t2.username,
    t2.sex,
    t2.address
  FROM
    orders t1,
    t_user t2
  WHERE t1.user_id=t2.id
  </select>
</mapper>

2.3 resultMap實現

<resultMap type="com.mybatis.entity.Orders" id="OrdersUserResultMap">
          <!-- 配置對映的訂單資訊 -->

          <!-- id:查詢列中的唯一標識,訂單資訊中的唯一標識,如果多列組成唯一標識(如:一般資料庫設計中的字典表 使用聯合主鍵),就需要配置多個id 
              column:訂單資訊的唯一標識 列
              property:訂單資訊的唯一標識列所對映到orders中的那個屬性(假如:資料庫中orders表中的主鍵為orders_id,而實體屬性名稱為ordersId,
                  則這個配置應為<id column="orders_id" property="ordersId"/>,類似hibernate實體對映檔案配置)。
          -->
         <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="com.mybatis.entity.User">
             <!-- id:關聯查詢使用者的唯一標識 
                 column:指定唯一標識使用者資訊的列
                 property:對映到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實現 -->
      <select id="findOrdersUserResultMap" resultMap="OrdersUserResultMap">
              SELECT t1.*,
                  t2.username,
                  t2.sex,
                  t2.address
              FROM
                  orders t1,
                  t_user t2
             WHERE t1.user_id=t2.id
     </select>

三、一對多查詢

mybatis 使用 resultMap 的 collection 對關聯查詢的多條記錄對映到一個 list 集合屬性中。

使用 resultType 實現:將訂單明細對映到orders中的orderdetails中,需要自己處理,使用雙重迴圈遍歷,去掉重複記錄,將訂單明細放在orderdetails中。

3.1 需求:查詢訂單(關聯使用者)及訂單明細;
3.2 對映思路
3.2. 在 orders.java 類中新增 List orderDetails 屬性(上面實體已新增) 。

最終會將訂單資訊對映到 orders 中,訂單所對應的訂單明細對映到 orders 中的 orderDetails 屬性中.

3.3 核心程式碼

<!-- 查詢訂單關聯查詢使用者及訂單明細 -->
<select id="findOrdersAndOrderDetailResultMap" resultMap="ordersAndOrderDetailResultMap">
    SELECT 
      t1.*,
      t2.username,
      t2.sex,
      t2.address,
      t3.id orderdetail_id,
      t3.items_id,
      t3.items_num,
      t3.orders_id
    FROM
      orders t1,
      t_user t2,
      orderdetail t3
    WHERE t1.user_id = t2.id AND t3.orders_id=t1.id
</select>
<!-- 查詢訂單(關聯使用者)及訂單明細的resultMap -->
<resultMap type="com.mybatis.entity.Orders" id="ordersAndOrderDetailResultMap" extends="OrdersUserResultMap">
  <!-- 訂單資訊 -->
  <!-- 關聯使用者資訊 -->
  <!-- 使用extends繼承,不用在中配置訂單資訊和使用者資訊的對映-->
  <!-- 關聯訂單明細資訊 
    一個訂單關聯查詢出了多條訂單明細,要使用collection對映
    collection:對關聯查詢到的多條記錄對映到集合中
    property:將關聯查詢到的多條記錄對映到orders類的那個屬性
    ofType:指定對映的集合屬性中pojo的型別
  -->
  <collection property="orderdetails" ofType="com.mybatis.entity.OrderDetail">
    <!-- id:唯一標識
       property:要將訂單明細的唯一標識對映到com.mybatis.entity.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>

四、多對多查詢

4.1.需求:查詢使用者以及使用者購買的商品資訊

4.2.對映思路

將使用者資訊對映到 user 中。

在 user 類中新增訂單列表屬性 List orderslist ,將使用者建立的訂單對映到 orderslist;

在 Orders 中新增訂單明細列表屬性 Listorderdetials ,將訂單的明細對映到 orderdetials;

在 OrderDetail 中新增 Items 屬性,將訂單明細所對應的商品對映到 Item;

4.3 核心程式碼

<!-- 查詢使用者即購買的商品資訊的ResultMap -->
  <resultMap type="com.mybatis.entity.User" id="userAndItemsResultMap">
    <!-- 使用者資訊 -->
    <id column="user_id" property="id"/>
    <result column="username" property="username"/>
    <result column="sex" property="sex"/>
    <result column="address" property="address"/>
  <!-- 訂單資訊
    一個使用者對應多個訂單,使用collection對映 -->
    <collection property="ordersList" ofType="com.mybatis.entity.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"/>
     <!-- 訂單明細
         一個訂單包括 多個明細
        -->
        <collection property="orderdetails" ofType="com.mybatis.entity.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"/>
           <!-- 商品資訊
              一個訂單明細對應一個商品
             -->
           <association property="items" javaType="com.mybatis.entity.Items">
             <id column="items_id" property="id"/>
             <result column="items_name" property="itemsName"/>
             <result column="items_detail" property="detail"/>
             <result column="items_price" property="price"/>
           </association>
        </collection>
      </collection>
  </resultMap>
<!-- 查詢使用者及使用者購買的商品資訊,使用resulaMap-->
<select id="findUserAndItemsResultMap" resultMap="userAndItemsResultMap">
    SELECT 
         t1.*,
         t2.username,
         t2.sex,
         t2.address,
         t3.id orderdetail_id,
         t3.items_id,
         t3.items_num,
         t3.orders_id,
         t4.itemsname items_name,
         t4.detail items_detail,
         t4.price items_price
    FROM
        orders t1,
        t_user t2,
        orderdetail t3,
        items t4
    WHERE t1.user_id =  t2.id AND  t3.orders_id=t1.id AND t3.items_id = t4.id
</select>