1. 程式人生 > >MyBatis中resultType和resultMap的區別

MyBatis中resultType和resultMap的區別

items 如果 分享圖片 ID itcast err http ber cast

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

技術分享圖片

技術分享圖片

單表查詢: 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>
View Code

關聯查詢(一對一):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>
View Code

關聯查詢(一對多):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>
View Code

MyBatis中resultType和resultMap的區別