序言:

昨天做一个项目,看到很多刚开始用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. 树莓派配置RTC时钟(DS3231,I2C接口)

    1.购买基于DS3231的RTC时钟模块,并且支持3.3V的那种 2.配置树莓派 a.打开树莓派的i2c接口 sudo raspi-config -->Interfacing Options - ...

  2. 深入理解JAVA I/O系列三:字符流详解

    字符流为何存在 既然字节流提供了能够处理任何类型的输入/输出操作的功能,那为什么还要存在字符流呢?容我慢慢道来,字节流不能直接操作Unicode字符,因为一个字符有两个字节,字节流一次只能操作一个字节 ...

  3. Internet History, Technology and Security (Week 3)

    Week 3 History: The Web Makes it Easy to Use Welcome to week 3! This is our fourth and final week of ...

  4. testng对执行失败的用例,再次执行

    前段时间在网络上看到通过重写TestNG的接口,可以再次执行失败的测试用例,于是学习了,我之前的做法是当自己的脚本中碰到异常,就自动调用方法本身来达到再次执行用例的目的,这个过程中有设定重试的次数 对 ...

  5. Iaas

    IaaS(Infrastructure as a Service),即基础设施即服务. 消费者通过Internet 可以从完善的计算机基础设施获得服务.这类服务称为基础设施即服务.基于 Interne ...

  6. JavaScript本地存储实践(html5的localStorage和ie的userData)

    http://www.css88.com/archives/3717 JavaScript本地存储实践(html5的localStorage和ie的userData) 发表于 2011年06月11日  ...

  7. Java中,一切皆是对象!为何数据类型中还分为:基本类型和对象?

    Java中一切皆是对象!这句话没错,因为八种基本类型都有对应的包装类(int的包装类是Integer),包装类自然就是对象了. 基本类型一直都是Java语言的一部分,这主要是基于程序性能的考量, 基本 ...

  8. 解题:TJOI 2015 组合数学

    题面 通过这个题理解了一下反链的概念,更新在图论知识点里了 每个点向右和下连边可以建出一张图,这个题事实上是让我们求图的最小链覆盖.Dilworth定理告诉我们,最小链覆盖等于最长反链(反链:DAG中 ...

  9. BZOJ 4321 queue2

    4321: queue2 Description n 个沙茶,被编号 1~n.排完队之后,每个沙茶希望,自己的相邻的两人只要无一个人的编号和自己的编号相差为 1(+1 或-1)就行: 现在想知道,存在 ...

  10. (转)编码规范系列(一):Eclipse Code Templates设置

    背景:长久以来,对java编程中的注释不甚理解.再次学习<疯狂JAVA讲义>基础,深深的感到自己基本功的不牢固.所以要做到事无巨细,好好修炼. 认识注释 常识 注释的作用: 回顾原有的代码 ...