【mybatis笔记】 resultType与resultMap的区别
序言:
昨天做一个项目,看到很多刚开始用mybatis的同事对于resultType和resultMap的理解与使用含糊不清,这里我试图用最好理解的说法写一写,欢迎大家勘误。
两者异同:
相同点:resultType和resultMap都是映射结果集到Javabean用的
不同点:
- resultType属于自动映射到javabean,而resultMap是手动映射到Javabean的,其中简单的映射关系可以使用resultType,复杂映射关系的推荐使用resultMap。
- 使用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的区别的更多相关文章
- MyBatis中resultType和resultMap的区别
resultType和resultMap功能类似 ,都是返回对象信息 ,但是resultMap要更强大一些 ,可自定义.因为resultMap要配置一下,表和类的一一对应关系,所以说就算你的字段名 ...
- Mybatis中输出映射resultType与resultMap的区别
Mybatis中输出映射resultType与resultMap的区别 (原文地址:http://blog.csdn.net/acmman/article/details/46509375) 一.re ...
- MyBatis有关resultType和resultMap差异
MyBatis有关resultType和resultMap差异 MyBatis中在查询进行select映射的时候,返回类型能够用resultType,也能够用resultMap.resultTyp ...
- resultType和resultMap的区别
1.resultType和resultMap的区别 1>resultType 返回的结果类型 2>resultMap 描述如何将结果集映射到Java对象 2.resultMap节点 1&g ...
- [转]MyBatis中resultType与resultMap区别
MyBatis中关于resultType和resultMap的具体区别如下: MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap.resu ...
- Mybatis中resultType和resultMap
一.概述MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部Res ...
- mybatis <sql /> 配置中 返回值 resultType 与resultMap的区别
mybatis的objectMapper.xml中, 1) 若<sql /> 查询语句中配置的是resultType=“实体类/DTO” ,则从mybatis返回的键值对结果集(Map)会 ...
- Mybatis的mapper文件中#和$的区别 以及 resultType和resultMap的区别
一般#{}用于传递查询的参数,一般用于从dao层传递一个string或者其他的参数过来,mybatis对这个参数会进行加引号的操作,将参数转变为一个字符串. SELECT * FROM employe ...
- mybatis中resultType和resultMap的联系
在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解 比如,我们平时使用的单表查 ...
随机推荐
- 2016011998+sw
Coding.net原码仓库地址:https://git.coding.net/laolaf/sw.git 目录 1 需求分析 2 功能设计 3 设计实现 4 算法详解 5 测试运行 6 满意代码 1 ...
- Head First Java & static
- C++内存布局(1)-让new出的两个变量在堆上的地址连续
大家都知道栈的地址按照从高到低的顺序增长的, 而堆的地址是按照从底到高的顺序增长的. ); ); cout<<"n1,n2所指的地址:" << n1 < ...
- elicpse
摘自http://blog.csdn.net/bug_love/article/details/72636505 eclipse error pages打红X的解决方法 我每次建一个Maven项目转为 ...
- [2017BUAA软工]个人项目心得体会:数独
心得体会 回顾此次个人项目,感受比较复杂,最明显的一点是--累!代码编写.单元测试.代码覆盖.性能优化,环环相扣,有种从作业发布开始就一直在赶DDL的感觉,但是很充实,也学习到和体验了很多东西.最令人 ...
- 关于VS2005中C#代码用F12转到定义时,总是显示从元数据的问题
元数据是:NET 程序集中的标记信息. 是在代码中选择了转到定义时候给定位的吧.因为没有找到源代码,VS通过反射读取元数据中的信息生成了那个. 解决方法: 1. 要把项目先添加到解决方案中. 2. 再 ...
- loadrunner在win10破解提示:Cannot save the license information because acceses to the registry is denied的解决办法
方法1 下图1-1中提示就是说明了破解的时候权限不足导致,解决办法就是使用管理员权限打开loadrunner破解就好了,但是右键“以管理员身份运行”选项打开loadrunner又是会提示1-2中的问题 ...
- 【Web Shell】- 技术剖析中国菜刀 - Part II
在第一部分,简单描述了中国菜刀的基本功能.本文我将剖析中国菜刀的平台多功能性.传输机制.交互模式和检测.我希望通过我的讲解,您能够根据您的环境检测出并清除它. 平台 那么中国菜刀可以在哪些平台上运行? ...
- @Primary 注解引出的问题
@Primary 注解 刚看到这个,还以为是持久层的注解呢,以为和@Id差不多,一查才知道,这两个风马牛不相及,反倒和@Qualifier以及@Resource有点像了,但是相比而言,后面两个更加的灵 ...
- Linux禁止root账户远程登录
Linux系统中,root用户几乎拥有所有的权限,远高于Windows系统中的administrator用户权限.一旦root用户信息被泄露,对于我们的服务器来说将是极为致命的威胁.所以禁止root用 ...