序言:

昨天做一个项目,看到很多刚开始用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. 封装GetQueryString()方法来获取URL的value值(转载)

    首先测试URL:http://192.168.1.82:8020/juzhong/daojishi.html?name=xiangruding&sex=nuuu&age=90 代码如下 ...

  2. Beta阶段——第四篇 Scrum 冲刺博客

    i. 提供当天站立式会议照片一张: ii. 每个人的工作 (有work item 的ID) (1) 昨天已完成的工作: 充值与账单的数据库操作结合,实现余额功能 (2) 今天计划完成的工作: 用户权限 ...

  3. 【vue】router-link 与 router-view

    1 router-link <router-link :to="{ path: '/hello', component: HelloWorld }">hello< ...

  4. [转帖]IPV6取代IPV4之路 为何道阻且长?

    IPV6取代IPV4之路 为何道阻且长? 经济学人公众号 IPV6作为IPV4的续命神术,从被提出到现今,逾26年之久.而IPV6在中国更是犹抱琵琶半遮面,千呼万唤难出来,IPV6取代IPV4之路,为 ...

  5. ACM数论之旅14---抽屉原理,鸽巢原理,球盒原理(叫法不一又有什么关系呢╮(╯▽╰)╭)

    这章没有什么算法可言,单纯的你懂了原理后会不会运用(反正我基本没怎么用过 ̄ 3 ̄) 有366人,那么至少有两人同一天出生(好孩子就不要在意闰年啦( ̄▽ ̄")) 有13人,那么至少有两人同一月 ...

  6. javascript易混淆的split()、splice()、slice()方法详解

    很多时候,一门语言总有那么些相似的方法,容易让人傻傻分不清楚,尤其在不经常用的时候.而本文主要简单总结了JavaScript中的关于字符串和数组中三个容易混淆的方法.旨在方便查阅,在容易混淆的时候有据 ...

  7. enginefuncs_t 结构体中的函数

    就是常见的 g_engfuncs 中的函数.AMXX 里就是 fakemeta 的 EngFunc_** // 这些函数由引擎提供给EXTDLL使用.mp.dll hl.dll ... typedef ...

  8. Educational Codeforces Round 56 Div. 2 翻车记

    A:签到. B:仅当只有一种字符时无法构成非回文串. #include<iostream> #include<cstdio> #include<cmath> #in ...

  9. Spring Boot 学习笔记1---初体验之3分钟启动你的Web应用

    前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文 ...

  10. [AT697]フィボナッチ

    题目大意:给你$n,k(n\leqslant10^9,k\leqslant10^3)$,求$f_n$.$f$数组满足$f_1=f_2=\cdots=f_k=1$,$f_n=\sum\limits_{i ...