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

(原文地址:http://blog.csdn.net/acmman/article/details/46509375

一、resultType

使用resultType进行输出映射,只有查询出来的列名和pojo(实体bean)中的属性名一致,该列才可以映射成功。

如果查询出来的列名和pojo中的属性名全部不一致,没有创建pojo对象。
只要查询出来的列名和pojo中的属性有一个一致,就会创建pojo对象。

1、输出简单类型

1).需求

用户信息的综合查询列表总数,通过查询总数和上边用户综合查询列表才可以实现分页。

2).mapper.xml

[html] view
plain
 copy
  1. <mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper">
  2. <!-- 用户信息综合查询
  3. #{UserCustom.sex}取出包装对象中性别值
  4. ${UserCustom.username}取得pojo包装对象中用户名称
  5. -->
  6. <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo"
  7. resultType="cn.edu.hpu.mybatis.PO.UserCustom">
  8. select * from user
  9. where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
  10. </select>
  11. <!-- 用户信息综合查询总数 -->
  12. <select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">
  13. select count(*) from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
  14. </select>
  15. ......
  16. </mapper>

3).mapper.java

[java] view
plain
 copy
  1. //用户管理的Dao接口
  2. public interface UserMapper {
  3. //用户信息综合查询
  4. public List<UserCustom> findUserList(UserQueryVo userQueryVo) throws Exception;
  5. //用户信息综合查询总数
  6. public int findUserCount(UserQueryVo userQueryVo) throws Exception;
  7. ......
  8. }

4).测试代码

[java] view
plain
 copy
  1. //用户信息综合查询总数
  2. @Test
  3. public void testFindUserCount() throws Exception{
  4. SqlSession sqlSession=sqlSessionFactory.openSession();
  5. //创建UserMapper代理对象
  6. UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
  7. //创建包装对象,设置查询条件
  8. UserQueryVo userQueryVo=new UserQueryVo();
  9. UserCustom userCustom=new UserCustom();
  10. userCustom.setSex("男");
  11. userCustom.setUsername("张三");
  12. userQueryVo.setUserCustom(userCustom);
  13. //调用userMapper的方法
  14. int count=userMapper.findUserCount(userQueryVo);
  15. System.out.println("总数为:"+count);
  16. }

测试结果:总数为:2

输出日志:

[plain] view
plain
 copy
  1. DEBUG [main] - Opening JDBC Connection
  2. DEBUG [main] - Created connection 7832149.
  3. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@778255]
  4. DEBUG [main] - ==>  Preparing: select count(*) from user where user.sex=? and user.username like '%张三%'
  5. DEBUG [main] - ==> Parameters: 男(String)
  6. DEBUG [main] - <==      Total: 1

5).小结

查询出来的结果集只有一行且一列,可以使用简单类型进行输出映射。(输出简单类型的要求)

2、输出pojo对象和pojo列表

不管是输出的pojo单个对象还是一个列表(list中包括pojo),在mapper.xml中resultType指定的类型是一样的。
在mapper.java指定的方法返回值类型不一样:
1).输出单个pojo对象,方法返回值是单个对象类型
2).输出pojo对象list,方法返回值是List<Pojo>

生成的动态代理对象中是根据mapper方法的返回值类型确定是调用selectOne(返回单个对象调用)还是selectList
(返回集合对象调用 ).

3、输出HashMap

输出pojo对象可以改用HashMap输出类型,将输出的字段名称作为map的key,value为字段值。如果是集合,那就是list里面套了HashMap。

二、.resultMap

mybatis中使用resultMap完成高级输出结果映射。

1、resultMap使用方法

如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。

2、下面来进行实验

1).实验需求

将下边的sql使用User完成映射
SELECT id id_,username username_ FROM USER WHERE id=#{value}

User类中属性名和上边查询列名不一致。

resultMap使用方法:(以下属性均定义在Mapper.xml映射文件中)

2).定义resultMap

[html] view
plain
 copy
  1. <!-- 定义resultType
  2. 将select id id_,username _username from user和User类中的属性做一个映射关系
  3. type:resultMap最终所映射的Java对象类型,可以使用别名
  4. id:对resultMap的唯一标识
  5. -->
  6. <resultMap type="user" id="userResultMap">
  7. <!-- id表示查询结果集中唯一标识
  8. column:查询出的列名
  9. property:type所指定的POJO中的属性名
  10. 最终reslutMap对column和property做一个映射关系(对应关系)
  11. -->
  12. <id column="_id" property="id"/>
  13. <!-- 对普通列的映射定义 -->
  14. <result column="_username" property="username"/>
  15. </resultMap>

3).使用resultMap作为statement的输出映射类型

[html] view
plain
 copy
  1. <!-- 使用resultMap进行输出映射
  2. resultMap:指定定义的resultMap的id,如果这个resultMap在其它的mapper文件,前面需要加namespace
  3. -->
  4. <select id="findUserByResultMap" parameterType="int" resultMap="userResultMap">
  5. select id _id,username _username from user where id=#{value}
  6. </select>

4).mapper接口类中添加相应方法

[java] view
plain
 copy
  1. //用户管理的Dao接口
  2. public interface UserMapper {
  3. public User findUserByResultMap(int id) throws Exception;
  4. ......
  5. }

测试:

[java] view
plain
 copy
  1. @Test
  2. public void testFindUserByResultMap() throws Exception{
  3. SqlSession sqlSession=sqlSessionFactory.openSession();
  4. //创建UserMapper代理对象
  5. UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
  6. //调用userMapper的方法
  7. User user=userMapper.findUserByResultMap(1);
  8. System.out.println(user.getUsername());
  9. }

测试结果:张三

输出日志:

[plain] view
plain
 copy
  1. EBUG [main] - Opening JDBC Connection
  2. DEBUG [main] - Created connection 1465214.
  3. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@165b7e]
  4. DEBUG [main] - ==>  Preparing: select id _id,username _username from user where id=?
  5. DEBUG [main] - ==> Parameters: 1(Integer)
  6. DEBUG [main] - <==      Total: 1

三、总结

使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。

如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。

2018-3-27更新



Mybatis使用resultType查出的字段一定要用驼峰法则重新命名成跟实体属性相同,否则接收不到

Mybatis中输出映射resultType与resultMap的区别的更多相关文章

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

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

  2. 5.Mybatis的输出映射(就是对查询的结果集的映射)

    Mybatis的输出映射,也就是对查询结果集的一个映射,主要有两种: 1.resultType(不需要配置,可以直接用) 一般是实体类 基本类型也可以 2.resultMap(需要配置resultMa ...

  3. MyBatis标签之Select resultType和resultMap

    摘要:介绍MyBatis 中Select标签的两个属性resultType和resultMap及其区别. 1 MyBatis动态SQL之if 语句 2 MyBatis动态sql之where标签|转 3 ...

  4. 【mybatis笔记】 resultType与resultMap的区别

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

  5. resultType和resultMap的区别

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

  6. MyBatis中resultType和resultMap的区别

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

  7. Mybatis中输入输出映射和动态Sql

    一.输入映射 我们通过配置parameterType的值来指定输入参数的类型,这些类型可以是简单数据类型.POJO.HashMap等数据类型 1.简单类型 2.POJO包装类型 ①这是单表查询的时候传 ...

  8. Mybatis中 SIMPLE、REUSE、BATCH的区别

    Executor分成两大类,一类是CacheExecutor,另一类是普通Executor. 普通类又分为: ExecutorType.SIMPLE: 这个执行器类型不做特殊的事情.它为每个语句的执行 ...

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

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

随机推荐

  1. 单例模式(Singleton)(单一实例)

    单例模式基本要点: 用于确保一个类只有一个实例,并且这个实例易于被访问. 让类自身负责保存他的唯一实例.这个类可以保证没有其他实例创建,并且他可以提供一个访问实例的方法,来实现单例模式. (1)把构造 ...

  2. 2019.10.28 csp-s模拟测试91 反思总结

    有一场没一场的233 T1: 胡乱分析一下题意,发现和为n的x个正整数,不同的数字种类不会超过√n个.假设这x个数字都不同,最多也就是(x+1)*x/2=n. 所以可以维护现有的size值以及对应的数 ...

  3. zabbix告警模板

    邮件 webhook模板 ZABBIX告警通知 告警状态:[{TRIGGER.STATUS}] 告警主机:[{HOST.NAME}] 主机地址:[{HOST.IP}] 告警时间:[{EVENT.DAT ...

  4. Java问题解读系列之基础相关---抽象类和接口

    今天来说一波自己对Java中抽象类和接口的理解,含参考内容: 一.抽象类 1.定义: public abstract class 类名{} Java语言中所有的对象都是用类来进行描述,但是并不是所有的 ...

  5. Cannot allocate memory for the buffer pool

    优化了一通,启动不了 直接上日志 innodb_buffer_pool_size”.”key_buffer_size” 的大小设置,适当的调大内存分配,减小,然后保存配置文件,重新尝试启mysql 成 ...

  6. python邮件发送:普通文本、html、添加附件

    # -*- coding: utf-8 -*- # @Time : 2019/9/19 13:46 # @Author : HuangWenjun # @Email : 350920551@qq.co ...

  7. CentOS7配置中文支持与部署GitLab服务器

    给你的 CentOS 7 安装中文支持 1.首先需要中文字体以便支持命令行终端的中文显示需求: yum groupinstall "fonts" 碰到提示输入 y 回车继续安装,大 ...

  8. Vue. 之 Element table 高度自适应

    Vue. 之 Element table 高度自适应 使用vue创建table后,其高度自适应浏览器高度. 在创建的 el-table 中添加:height属性,其值为一个变量(tableHeight ...

  9. linux升级或安装程序后无法进入图形界面

    报错如下: Failed to start the X server (your graphical interface). lt is likely that it is not set up co ...

  10. 从0开始学习 GitHub 系列之「06.团队合作利器 Branch」

    Git 相比于 SVN 最强大的一个地方就在于「分支」,Git 的分支操作简直不要太方便,而实际项目开发中团队合作最依赖的莫过于分支了,关于分支前面的系列也提到过,但是本篇会详细讲述什么是分支.分支的 ...