序言:

昨天做一个项目,看到很多刚开始用mybatis的同事对于resultType和resultMap的理解与使用含糊不清,这里我试图用最好理解的说法写一写,欢迎大家勘误。

两者异同:

相同点:resultType和resultMap都是映射结果集到Javabean用的

不同点:

  1. resultType属于自动映射到javabean,而resultMap是手动映射到Javabean的,其中简单的映射关系可以使用resultType,复杂映射关系的推荐使用resultMap。
  2. 使用resultMap需要先在mapper.xml中定义resultMap。而resultType则无需定义。

下面我举两个正例、两个反例:

需要映射的JavaBean:User

 package com.github.hellxz.entity;

 /**
* @Author : Hellxz
* @Description: 被映射的Javabean,常见的User
* @Date : 2018/3/9 8:25
*/
public class User { private Long userId;
private String username;
private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Long getUserId() {
return userId;
} public void setUserId(Long userId) {
this.userId = userId;
}
}

UserMapper接口定义:

 package com.github.hellxz.dao;

 /**
* @Author : Hellxz
* @Description: User的dao接口
* @Date : 2018/3/9 8:44
*/
public interface UserMapper { User selectByUsername(String username);
}

正例:

【resultType正例】:resultType指向具体类型或别名 ✔

 <?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" >
<mapper namespace="com.github.hellxz.dao.UserMapper" >
<sql id="Base_Column_List" >
user_id, user_name, password
</sql>
<select id="selectByUsername" resultType="com.github.hellxz.entity.User" parameterType="string" >
select
<include refid="Base_Column_List" />
from user
where user_name = #{username}
</select> </mapper>

【resultMap正例】:resultMap引用定义好的resultMap的id 

 <?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" >
<mapper namespace="com.github.hellxz.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.github.hellxz.entity.User" >
<id column="user_id" property="userId" jdbcType="BIGINT" />
<result column="user_name" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
user_id, user_name, password
</sql>
<select id="selectByUsername" resultMap="BaseResultMap" parameterType="string" >
select
<include refid="Base_Column_List" />
from user
where user_name = #{username}
</select> </mapper>

反例:

【resultType反例】:使用resultType去引用定义的resultMap ✘

 <?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" >
<mapper namespace="com.github.hellxz.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.github.hellxz.entity.User" >
<id column="user_id" property="userId" jdbcType="BIGINT" />
<result column="user_name" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
user_id, user_name, password
</sql>
<select id="selectByUsername" resultType="BaseResultMap" parameterType="string" >
select
<include refid="Base_Column_List" />
from user
where user_name = #{username}
</select> </mapper>

【resultMap反例】:使用resultType去引用定义的resultMap或者引用没有定义的resultMap 

 <?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" >
<mapper namespace="com.github.hellxz.dao.UserMapper" >
<sql id="Base_Column_List" >
user_id, user_name, password
</sql>
<select id="selectByUsername" resultMap="com.github.hellxz.entity.User" parameterType="string" >
select
<include refid="Base_Column_List" />
from user
where user_name = #{username}
</select> </mapper>

【mybatis笔记】 resultType与resultMap的区别的更多相关文章

  1. MyBatis中resultType和resultMap的区别

    resultType和resultMap功能类似  ,都是返回对象信息  ,但是resultMap要更强大一些 ,可自定义.因为resultMap要配置一下,表和类的一一对应关系,所以说就算你的字段名 ...

  2. Mybatis中输出映射resultType与resultMap的区别

    Mybatis中输出映射resultType与resultMap的区别 (原文地址:http://blog.csdn.net/acmman/article/details/46509375) 一.re ...

  3. MyBatis有关resultType和resultMap差异

    MyBatis有关resultType和resultMap差异   MyBatis中在查询进行select映射的时候,返回类型能够用resultType,也能够用resultMap.resultTyp ...

  4. resultType和resultMap的区别

    1.resultType和resultMap的区别 1>resultType 返回的结果类型 2>resultMap 描述如何将结果集映射到Java对象 2.resultMap节点 1&g ...

  5. [转]MyBatis中resultType与resultMap区别

    MyBatis中关于resultType和resultMap的具体区别如下: MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap.resu ...

  6. Mybatis中resultType和resultMap

    一.概述MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部Res ...

  7. mybatis <sql /> 配置中 返回值 resultType 与resultMap的区别

    mybatis的objectMapper.xml中, 1) 若<sql /> 查询语句中配置的是resultType=“实体类/DTO” ,则从mybatis返回的键值对结果集(Map)会 ...

  8. Mybatis的mapper文件中#和$的区别 以及 resultType和resultMap的区别

    一般#{}用于传递查询的参数,一般用于从dao层传递一个string或者其他的参数过来,mybatis对这个参数会进行加引号的操作,将参数转变为一个字符串. SELECT * FROM employe ...

  9. mybatis中resultType和resultMap的联系

    在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解 比如,我们平时使用的单表查 ...

随机推荐

  1. IEEE 802.11 无限局域网

    (1)无线通讯的两个重要特征 ——Hidden node problem 双方虽然听不到对方的讯号,但同时传送给相同的对象导致了碰撞(这个时候双方都不知道发生了碰撞) ——Exposed node p ...

  2. rhel和centos7下更改网卡名称ens33为eth0

    Linux使用小Tips 整理些Linux些常遇到的问题. 修改网卡ens33为eth0 在使用RHEL和Centos7,发现网卡名称变成了EnoX,挺不习惯.现更改回旧名称eth0看着顺眼. 备份/ ...

  3. RTMP、RTSP、HTTP视频协议详解(附:直播流地址、播放软件)

  4. UVA 12633 Super Rooks on Chessboard(FFT)

    题意: 给你一个R*C的棋盘,棋盘上的棋子会攻击,一个棋子会覆盖它所在的行,它所在的列,和它所在的从左上到右下的对角线,那么问这个棋盘上没有被覆盖的棋盘格子数.数据范围R,C,N<=50000 ...

  5. 51nod 1376 最长上升子序列的数量 | DP | vector怒刷存在感!

    51nod 1376 最长上升子序列的数量 题解 我们设lis[i]为以位置i结尾的最长上升子序列长度,dp[i]为以位置i结尾的最长上升子序列数量. 显然,dp[i]要从前面的一些位置(设为位置j) ...

  6. Permutations II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...

  7. 解题:BZOJ 2818 GCD

    题面 转化一下题目,即是求$1$到$n$中对于某个素数$pri$使得$gcd(x*pri,y*pri)=pri$的$(x,y)$的数目 这样一来就可以考虑每个质数$pri$对答案的贡献,即为$1$到$ ...

  8. [SCOI2016] 背单词 (Trie树)

    $pdf\space solution$      link #include<iostream> #include<algorithm> #include<cstrin ...

  9. boost::asio 同步&异步例子

    同步客户端: using boost::asio; io_service service; ip::tcp::endpoint ep( ip::address::from_string(); ip:: ...

  10. [转]Asp.Net MVC使用HtmlHelper渲染,并传递FormCollection参数的陷阱

    http://www.cnblogs.com/errorif/archive/2012/02/13/2349902.html 在Asp.Net MVC 1.0编程中,我们经常遇见这样的场景,在新建一个 ...