Mybatis 实用篇(四)返回值类型

一、返回 List、Map

List<User> getUsers();
<select id="getUsers" resultType="User">
select * from user;
</select> Map<String, Object> getUsers();
<select id="getUsers" resultType="map">
select * from user;
</select>

二、返回指定的 key

    @MapKey("id")
Map<Integer, User> getUsers();
<select id="getUsers" resultType="User">
select * from user;
</select>

三、resultMap

mapUnderscoreToCamelCase=true 时可以自动将下划线转为驼峰规则,如果还不能满足要求就需要自定义返回类型。如下:

List<User> getUsers();
<select id="getUsers" resultMap="myMap">
select u.id, u.name, u.did, d.name dname from user u, dept d where u.did=d.id;
</select> <resultMap id="myMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="dept.id" column="did"/>
<result property="dept.name" column="dname"/>
</resultMap>

3.1 association

association 可以将一个 java bean 对象 dept 封装到起来,如:

public class User {

    private int id;
private String name;
private DePartment dept;
}
<resultMap id="myMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<association property="dept" javaType="DePartment">
<result property="id" column="did"/>
<result property="name" column="dname"/>
</association>
</resultMap>
<!-- association 可以分步查找 -->
<resultMap id="myMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<!-- 指定 select 语句的 id 和要传入的参数 id -->
<association property="dept" javaType="DePartment" select="getDept" column="id"/>
</resultMap> <select id="getUsers" resultMap="myMap">
select id, name from user;
</select>
<select id="getDept" resultType="DePartment">
select id, name from dept where id=#{id};
</select>

3.2 collection

collection 可以将 java bean 对象的 list 集合 users 封装到起来,如:

public class DePartment {

    private int id;
private String name;
private List<User> users;
}
<resultMap id="myMap" type="DePartment">
<id property="id" column="did"/>
<result property="name" column="dname"/>
<collection property="users" ofType="User">
<result property="id" column="id"/>
<result property="name" column="name"/>
</collection>
</resultMap>
<select id="getDept" resultMap="myMap">
select u.id, u.name, u.did, d.name dname from user u, dept d where u.did=d.id;
</select>

也可以分步查找,注意 N + 1 问题

<resultMap id="myMap" type="DePartment">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="users" ofType="User" select="getUsers" column="id"/>
</resultMap>
<select id="getUsers" resultType="User">
select id, name from user where did=#{did};
</select>
<select id="getDept" resultMap="myMap">
select id, name from dept;
</select>

注意:

  1. 支持多列传值:column="{key1=colum1, key2=colum2}"
  2. 分步查找支持缓存,可以配置属性 fetchType="eager(立即执行)/lazy(延迟加载)",也可以全局配置
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>

使用时如果用到了就会去查找,否则不会查找:

List<DePartment> depts = userMapper.getDept();
System.out.println(depts.get(0).getName());
System.out.println(depts.get(0).getUsers());

mybatis 查找时的 sql 如下,可以看到用到 dept 的 users 属性时才进行查找:

2018-09-06 06:50:30 DEBUG getDept9:159 - ==>  Preparing: select id, name from dept;
2018-09-06 06:50:30 DEBUG getDept9:159 - ==> Parameters:
2018-09-06 06:50:31 DEBUG getDept9:159 - <== Total: 2
dev
2018-09-06 06:50:31 DEBUG getUsers9:159 - ==> Preparing: select id, name from user where did=?;
2018-09-06 06:50:31 DEBUG getUsers9:159 - ==> Parameters: 1(Integer)
2018-09-06 06:50:31 DEBUG getUsers9:159 - <== Total: 2
[User{id=1, name='binarylei', age=0, sex='null'}, User{id=3, name='binarylei3', age=0, sex='null'}]

每天用心记录一点点。内容也许不重要,但习惯很重要!

Mybatis 实用篇(四)返回值类型的更多相关文章

  1. MyBatis查询结果resultType返回值类型详细介绍

    一.返回一般数据类型 比如要根据 id 属性获得数据库中的某个字段值. mapper 接口: // 根据 id 获得数据库中的 username 字段的值 String getEmpNameById( ...

  2. MyBatis中Mapper的返回值类型

    insert.update.delete语句的返回值类型 对数据库执行修改操作时,数据库会返回受影响的行数. 在MyBatis(使用版本3.4.6,早期版本不支持)中insert.update.del ...

  3. ResultMap和ResultType在使用中的区别、MyBatis中Mapper的返回值类型

    在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解 resultType:当使 ...

  4. int不可为null引发的 MyBatis做持久层框架,返回值类型要为Integer问题

    MyBatis做持久层框架,返回值类型要为Integer MyBatis 做持久层时,之前没注意,有时候为了偷懒使用了int类型做为返回的类型,这样是不可取的,MyBatis做持久层框架,返回值类型要 ...

  5. mybatis的XML返回值类型报错

    昨天项目里一直报错说是一个文件里的返回值java.util.hashmap不对,然后去定位这个文件发现并没有问题,后来在全局搜索的帮助下查找了返回值类型为resultMap的文件里看到写的代码里有: ...

  6. springMVC入门(四)------参数绑定与返回值类型

    简介 从之前的介绍,已经可以使用springMVC完成完整的请求.返回数据的功能. 待解决的问题:如何将数据传入springMVC的控制器进行后续的处理,完成在原生servlet/jsp开发中Http ...

  7. C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解

    前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 C#进阶系列——WebApi接口传参不再困惑:传参详解  ...

  8. WebApi 接口返回值类型详解 ( 转 )

    使用过Webapi的园友应该都知道,Webapi的接口返回值主要有四种类型 void无返回值 IHttpActionResult HttpResponseMessage 自定义类型 此篇就围绕这四块分 ...

  9. WebApi接口返回值不困惑:返回值类型详解

    前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.作为程序猿,我们都知道参数和返回值是编程领域不可分割的两大块,此前分享了 ...

随机推荐

  1. 第2本MATLAB书

    前面看的<DSP using MATLAB>后面还有两章,其内容太难了,看不下去,暂时放放: 因为工作中需要MATLAB和电磁场的相关知识,从网上找了本 初步翻了翻,里面有代码有图片,英文 ...

  2. string学习

    来自:http://www.cnblogs.com/kkgreen/archive/2011/08/24/2151450.html 0,new是创了两个对象,一个在堆,一个在常量池 1,变量+字符串= ...

  3. 如何利用 AVDictionary 配置参数(转)

    本文是我的 FFMPEG Tips 系列的第五篇文章,准备介绍下 ffmpeg 提供的一个非常好用的健值对工具:AVDictionary,特别是对于没有 map 容器的 c 代码,可以充分利用它来配置 ...

  4. logback节点配置详解

    一 :根节点  <configuration></configuration> 属性 : debug : 默认为false ,设置为true时,将打印出logback内部日志信 ...

  5. Bootstrap-Plugin:折叠(Collapse)插件

    ylbtech-Bootstrap-Plugin:折叠(Collapse)插件 1.返回顶部 1. Bootstrap 折叠(Collapse)插件 折叠(Collapse)插件可以很容易地让页面区域 ...

  6. TCL数据类型

    原始数据类型在Tcl中是字符串,我们常常可以找到字符串和引用在Tcl语言中.这些原始数据类型依次创建复合数据类型列表和关联数组.在Tcl中,数据类型可以表示不仅是简单Tcl的对象,但也可以代表相同的句 ...

  7. java之JMS

    一.简介:JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进 ...

  8. 本地通过源码方式启动solr

      首先,下载solr5.5.0源码,http://apache.fayea.com/lucene/solr/5.5.0/solr-5.5.0-src.tgz   解压完成后,分为几个目录,然而sol ...

  9. PyQt5系列教程(二)利用QtDesigner设计UI界面

    软硬件环境 OS X EI Capitan Python 3.5.1 PyQt 5.5.1 PyCharm 5.0.1 前言 在PyQt5系列教程的第一篇http://blog.csdn.net/dj ...

  10. uwsgi配置文件的一些细节,uwsgi错误invalid request block size

    [uwsgi] #socket = #这种是使用代理方式访问的,不能直接输入端口访问,要搭配其他的HTTP服务比如NGINX,设置反向代理 http =: #这种是直接可以输入IP端口访问 modul ...