MyBaties中resultMap簡單入門

一、簡介
resultType可以把查詢結果封裝到pojo類型中,但必須pojo類的屬性名和查詢到的數據庫表的字段名一致。 如果sql查詢到的字段與pojo的屬性名不一致,則需要使用resultMap將字段名和屬性名對應起來,進行手動配置封裝,將結果映射到pojo中。resultMap可以實現將查詢結果映射爲複雜類型的pojo,比如在查詢結果映射對象中包括pojo和list實現一對一查詢和一對多查詢。

二、簡單使用
order訂單表:id,user_id,number,createtime,note

<!-- 查詢所有的訂單數據 -->
    <!-- resultMap:填入配置的resultMap標籤的id值 -->
    <select id="queryOrderAll" resultMap="orderResultMap">
        SELECT id, user_id,
        number,
        createtime, note FROM `order`
    </select>
<!-- resultMap最終還是要將結果映射到pojo上,type就是指定映射到哪一個pojo -->
    <!-- id:設置ResultMap的id -->
    <resultMap  id="orderResultMap"  type="order">
        <!-- 定義主鍵 ,非常重要。如果是多個字段,則定義多個id -->
        <!-- property:主鍵在pojo中的屬性名 -->
        <!-- column:主鍵在數據庫中的列名 -->
        <id property="id" column="id" />
        <!-- 定義普通屬性 -->
        <result property="userId" column="user_id" />
        <result property="number" column="number" />
        <result property="createtime" column="createtime" />
        <result property="note" column="note" />
    </resultMap>

三、關聯查詢
一對一數據模型:訂單用戶 
一個訂單信息只會是一個人下的訂單,所以從查詢訂單信息出發關聯查詢用戶信息爲一對一查詢
首先在Order類中增加屬性User,然後編寫mapper:

<resultMap type="order" id="orderUserResultMap">
    <id property="id" column="id" />
    <result property="userId" column="user_id" />
    <result property="number" column="number" />
    <result property="createtime" column="createtime" />
    <result property="note" column="note" />
    <!-- association :配置一對一屬性 -->
    <!-- property:order裏面的User屬性名 -->
        <!-- javaType:屬性類型 -->
    <association property="user" javaType="user">
        <!-- id:聲明主鍵,表示user_id是關聯查詢對象的唯一標識-->
        <id property="id" column="user_id" />
        <result property="username" column="username" />
        <result property="address" column="address" />
    </association>
</resultMap>
<!-- 一對一關聯,查詢訂單,訂單內部包含用戶屬性 -->
<select id="queryOrderUserResultMap" resultMap="orderUserResultMap">
    SELECT
    o.id,
    o.user_id,
    o.number,
    o.createtime,
    o.note,
    u.username,
    u.address
    FROM
    `order` o
    LEFT JOIN `user` u ON o.user_id = u.id
</select>

一對多數據模型:用戶訂單
從用戶信息出發查詢用戶下的訂單信息則爲一對多查詢,因爲一個用戶可以下多個訂單
首先在User類中增加屬性List<Order> orders,然後編寫mapper:

<resultMap type="user" id="userOrderResultMap">
    <id property="id" column="id" />
    <result property="username" column="username" />
    <result property="birthday" column="birthday" />
    <result property="sex" column="sex" />
    <result property="address" column="address" />
    <!-- 配置一對多的關係
        property:填寫pojo類中集合類類屬性的名稱
        javaType:填寫集合類型的名稱 
    -->
    <collection property="orders" javaType="list" ofType="order">
        <!-- 配置主鍵,是關聯Order的唯一標識 -->
        <id property="id" column="oid" />
        <result property="number" column="number" />
        <result property="createtime" column="createtime" />
        <result property="note" column="note" />
    </collection>
</resultMap>
<!-- 一對多關聯,查詢訂單同時查詢該用戶下的訂單 -->
<select id="queryUserOrder" resultMap="userOrderResultMap">
    SELECT
    u.id,
    u.username,
    u.birthday,
    u.sex,
    u.address,
    o.id oid,
    o.number,
    o.createtime,
    o.note
    FROM
    `user` u
    LEFT JOIN `order` o ON u.id = o.user_id
</select>

四、對返回數據進行分組
假設有如下sql查詢語句:
select id, otherId from mytalbe 
返回數據:
id   otherId
01   00001
01   00002
01   00003
02   00004

有java實體類如下:
class Id{
  private String id;
  private List<String> otherId;
}
怎樣通過執行一次查詢,返回如下形式的數據呢?
List<Id> aa = new ArrayList<Id>();
aa = ...//通過mybatis執行slq
aa:[
     {01,[00001,00002,00003,00004]},
     {02,[00004]} 
   ]
實現方法如下:

<resultMap id="myid" type="Id">
  <id Property="id" column="id">
  <collecton property="otherId" ofType="String" javaType="ArrayList">
     <result colume="otherId">
  </collection>
</resultMap>
<select id="list" resultMap="myid">
select id, otherId from mytalbe 
</select>


需要注意的是<id> 標籤,這是分組的標識。一定要注意。
 
文章參考:https://blog.csdn.net/qq_42780864/article/details/81429114

文章參考:https://blog.csdn.net/shijunjoy/article/details/75119671

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章