序言:

昨天做一个项目,看到很多刚开始用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. Java Lock & Condition

    /* jdk1.5以后将同步和锁封装成了对象. 并将操作锁的隐式方式定义到了该对象中, 将隐式动作变成了显示动作. Lock接口: 出现替代了同步代码块或者同步函数.将同步的隐式锁操作变成现实锁操作. ...

  2. C++ 游戏之点点水果

    大二时利用C++编写的点水果小游戏 程序代码总共3个文件,main.cpp Fruit.h Fruit.cpp  代码将在图片下面给出 至于讲解,由于过了一年多的时间,有点忘记了,但我会努力回忆并即时 ...

  3. Linux基础五(系统管理)

    Linux 系统管理 1. 进程管理 1.1 进程管理简介 进程的简介: 一个程序在运行的时候会占用系统的资源,即系统分配资源给某个程序使用,进程就是正在运行中的某个程序或者命令.进程又可以细分为线程 ...

  4. JPEG标准推荐的亮度、色度DC、AC Huffman编码表

    JPEG 标准推荐的亮度.色度DC.AC Huffman 编码表 博主在完成数字图像处理大作业时利用搜索引擎查找了很久完整的四张Huffman 编码表(亮度AC Huffman编码表.亮度DC Huf ...

  5. lr几个常用的函数

    将字符串保存为参数 lr_save_string("string you want to save", "arg_name"); 将int型数字保存为参数 lr ...

  6. 解决win10激活错误代码0xc004c003

    打开命令窗口(管理员). win10电脑图解-2 输入slmgr.vbs /upk,回车 激活错误电脑图解-3 输入:slmgr /ipk W269N-WFGWX-YVC9B-4J6C9-T83GX, ...

  7. 使用 MQTTnet 快速实现 MQTT 通信

    1 什么是 MQTT ? MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是 IBM 开发的一个即时通讯协议,有可能成为物联网的重要组成部分.MQT ...

  8. sql学习. case + group by 都干了啥子事情

    select case pref_name when 'fudao' then 'siguo' when 'xiangchuan' then 'siguo' when 'aiyuan' then 's ...

  9. struts 跳转的四种常用类型

    1 dispatcher 默认的跳转类型 地址栏不变 2.redirect 跳转后地址会变化 3 chain 跳转到一个动作类 地址栏不会变 4 redirectAction 跳转到一个动作类 地址栏 ...

  10. 链表java实现

    链表是用指针将多个节点联系在一起,通过头节点和尾节点还有节点数量,可以对链表进行一系列的操作.是线性表的链式存储实现. 1.链表是多个不连续的地址组成在一起根据指针链接在一起的,由多个节点组成,每个节 ...