The difference between resulttype and resultmap

1. Overview
When querying select mapping in MyBatis, the return type can use resultType or resultMap. The resultType directly represents the return type, and the resultMap is a reference to the external ResultMap, but the resultType and the resultMap cannot exist at the same time.
When MyBatis performs query mapping, in fact, each attribute queried is placed in a corresponding Map, where the key is the attribute name and the value is its corresponding value.
① When the provided return type attribute is resultType, MyBatis will extract the key-value pair in the Map and assign it to the attribute corresponding to the object specified by resultType. So in fact, the return type of each query map of MyBatis is ResultMap, but when the provided return type attribute is resultType, MyBatis automatically assigns the corresponding value to the attribute of the object specified by resultType.
② When the provided return type is resultMap, because Map cannot represent the domain model well, you need to further convert it into the corresponding object, which is often very useful in complex queries.

2. Parameter passing

The parameter transfer method can be @Param ("***"), where *** is the parameter type, which can be defined at will, but it must be consistent with the mapping file.

E.g:

method:

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

Mapping file:

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

can also be an object,

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

The same can also be obtained: #{0}, #{1}, #{2}, #{3},,,,,,,

When passed an array:

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

Mapping file:

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

3. resultType

resultType can directly return the given return value type, such as String, int, Map, etc. Returning List also defines the return type as Map, and then mybatis will automatically put these maps in a List, and resultType can also be a object, for example:

Return common types:

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

Return a Map or 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>

MyBatis会自动创建一个ResultMap对象,然后基于查找出来的属性名进行键值对封装,然后再看到返回类型是DeviceInfo对象,再从ResultMap中取出与DeviceInfo对象对应的键值对进行赋值。

四、resultMap

当返回类型直接是一个ResultMap的时候也是非常有用的,这主要用在进行复杂联合查询上,因为进行简单查询是没有什么必要的。先看看一个返回类型为ResultMap的简单查询,再看看复杂查询的用法。

<resultMap id="BaseResultMap" type="com.cachee.ilabor.att.clientmodel.User"> 
<result column="ID" property="id" jdbcType="INTEGER" /> 
<result column="SN" property="SN" jdbcType="VARCHAR" /> 
<result column="companyId" property="companyId" jdbcType="VARCHAR" /> 
<result column="tb_isDelete" property="tb_isDelete" jdbcType="VARCHAR" />
<result column="tb_createTime" property="tb_createTime" jdbcType="VARCHAR" /> 
</resultMap>

 

参考链接:

http://blog.csdn.net/woshixuye/article/details/27521071

http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326064111&siteId=291194637
Recommended