mybatis中resultMap与resultType区别

mybatis中resultMap与resultType区别


 

resultType和resultMap功能类似  ,都是返回对象信息  ,但是resultMap要更强大一些 ,可自定义。因为resultMap要配置一下,表和类的一一对应关系,所以说就算你的字段名和你的实体类的属性名不一样也没关系,都会给你映射出来,但是,resultType必须字段名一样,比如说 cId和c_id 这种的都不能映射 。

(1)MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap。

         resultType:直接表示返回类型;

         result Map:对外部result Map的引用;

        二者不能同时存在。

(2)当提供的返回类型是resultMap时,因为Map不能很好表示领域模型,就需要自己再进一步的把 它转化为对应的对象,这常常在复杂查询中很有作用

(3)当提供的返回类型属性是resultType时,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。所以其实MyBatis的每一个查询映射的返回类型都是ResultMap,只是当提供的返回类型属性是resultType的时候,MyBatis对自动的给把对应的值赋给resultType所指定对象的属性

result Map:

<resultMap id="userResultMap" type="User">
    <id property="id" column="user_id" />  
    <result property="username" column="username"/>  
    <result property="password" column="password"/>
</resultMap>

<select id="selectUsers" parameterType="int" resultMap="userResultMap">  
    select user_id, user_name, hashed_password  from some_table  where id = #{id}
</select>

column:从数据库中查到的属性

property:查询出来的属性对应的值,赋给实体对象的那个属性

resultMap:一个外部resultMap的id,表示返回结果映射到那一个result Map上

关联查询(一对一):

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>

result Type:

resultType可以直接返回给出的返回值类型,比如String、int、Map,等等,其中返回List也是将返回类型定义为Map,然后mybatis会自动将这些map放在一个List中,resultType还可以是一个对象

返回常见类型:

<select id="getLogCount" resultType="int">
   select COUNT(*) from AttLog where attTime = #{attTime} and userId = #{userId};
</select>

返回Map或List:

<select id="getDeviceInfoByDeviceId" resultType="Map">
select userCount as usercount,
  fingerCount as fingercount,
  faceCount as facecount,
  attRecordCount as recordcount,
  lastOnline,
  state as status
  from DeviceInfo where deviceId = #{deviceId} and tb_isDelete = 0;
</select>

返回一个对象:

<select id="queryAllDeviceInfo" resultType="com.cachee.ilabor.att.clientmodel.DeviceInfo">
    select * from deviceInfo where tb_isDelete = 0;
</select>

参数传递:

传参方法可以@Param(“***”),其中***是参数类型,可以随意定义,但是一定要和映射文件一致。

数据类型:

方法:

int getLogCount(@Param("attTime")String attTime,@Param("userId")String userId);

映射文件:

<select id="getLogCount" resultType="int">
    select COUNT(*) from AttLog where attTime = #{attTime} and userId = #{userId};
</select>

对象:

<insert id="saveDeviceUserInfo" parameterType="com.cachee.ilabor.att.clientmodel.DeviceUserInfo">
insert into deviceUserInfo(deviceId,companyId,userId,pin,name,pri,passwd,card,grp,tz,verify)
values(#{deviceId},#{companyId},#{userId},#{pin},#{name},#{pri},#{passwd},#{card},#{grp},#{tz},#{verify});
</insert>

数组:

方法:

void updateSendState(@Param("updateId")int[] updateId);

映射文件:

<update id="updateSendState">
    update deviceUserInfo set sendState = 1 where id in
    <foreach item="item" index="index" collection="updateId" open="(" separator="," close=")">
        #{item}
    </foreach>
</update>

猜你喜欢

转载自blog.csdn.net/weixin_44324023/article/details/89060161