ResultType和ResultMap的用法和区别(供自己学习查阅使用)

综述


MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap。resultType是直接表示返回类型的(String、int、Object对象),而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。

在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值。

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

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

一、ResultType

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

返回常见类型:

1.返回int或者Integer

  <select id="getLogCount" resultType="int">
    select COUNT(*) from table_a where "字段" = ****;
  </select>

2.返回Map

<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>

3.返回list,list里面其实是一个resultType

<select id="queryAllDeviceInfo" resultType="com.zhang.DeviceInfo">
select * from deviceInfo where tb_isDelete = 0;
</select>

4.返回一个封装的实体类对象,parameterType="" 属于传递来的参数类型是什么

<select id="selectUsers" parameterType="int" resultType="User">
  select
    user_id             as "id",
    user_name           as "userName",
    hashed_password     as "hashedPassword"
  from some_table
  where id = #{id}
</select>

二、ResultMap

适合使用返回值是自定义实体类的情况,映射实体类的数据类型:

id:resultMap的唯一标识

column: 库表的字段名

property: 实体类里的属性名

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:当前库表映射文件的命名空间,唯一的不能重复 -->
<mapper namespace="com.hao947.sql.mapper.PersonMapper">
  <!-- type:映射实体类的数据类型 id:resultMap的唯一标识 -->
  <resultMap type="person" id="BaseResultMap">
    <!-- column:库表的字段名 property:实体类里的属性名 -->
    <id column="person_id" property="personId" />
    <result column="name" property="name" />
    <result column="gender" property="gender" />
    <result column="person_addr" property="personAddr" />
    <result column="birthday" property="birthday" />
  </resultMap>
  <!--id:当前sql的唯一标识
     parameterType:输入参数的数据类型 
     resultType:返回值的数据类型 
     #{}:用来接受参数的,如果是传递一个参数#{id}内容任意,如果是多个参数就有一定的规则,采用的是预编译的形式select 
    * from person p where p.id = ? ,安全性很高 -->
 
  <!-- sql语句返回值类型使用resultMap -->
  <select id="selectPersonById" parameterType="java.lang.Integer"
    resultMap="BaseResultMap">
    select * from person p where p.person_id = #{id}
  </select>
  <!-- resultMap:适合使用返回值是自定义实体类的情况 
  resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型 -->
  <select id="selectPersonCount" resultType="java.lang.Integer">
    select count(*) from
    person
  </select>
 
  <select id="selectPersonByIdWithMap" parameterType="java.lang.Integer"
    resultType="java.util.Map">
    select * from person p where p.person_id= #{id}
    </select>
 
</mapper>

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT猛男赵四

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值