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. Linux的发行版之间的联系和区别

    转载:https://blog.csdn.net/suixin788/article/details/52555558 联系 Linux的内核源代码和Linux的应用程序都可以自由获得,因此很多公司组 ...

  2. Python的安装和使用ubuntu下

    ubuntu下Python的安装和使用 https://www.cnblogs.com/luckyalan/p/6703590.html ubuntu下Python的安装和使用 1 文章介绍 本文介绍 ...

  3. 【Xamarin】Visual Studio 2013 Xamarin for iOS 环境搭建

    一.Mac安装Xamarin.iOS 1,我的Mac 环境:OSX 10.10.3.Xcode 6.3.2 (使用虚拟机亲测也成功  VMware 11 安装 Mac OS X10.10  ) Xam ...

  4. linux(centOs)下memcached安装

    1.libevent安装.为啥先安装它?因为不先装,memcached这座房子就没打地基: yum install libevent-devel 敲回车后出现: Loaded plugins: fas ...

  5. mysql索引优化续

    (1)索引类型: Btree索引:抽象的可以理解为“排好序的”快速查找结构myisam,innodb中默认使用Btree索引 hash索引:hash索引计算速度非常的快,但数据是随机放置的,无法对范围 ...

  6. Oracle本地网络服务名配置

    1.安装Oracle 11G Client后可以在开始菜单中找到 选择NETCA->本地网络服务名配置 选择添加本地网服务名配置 这里的服务名:指的是也就是数据库名 在网络中架设C/S 客户端选 ...

  7. 进程基本-进程创建,僵尸进程,exec系列函数

    Linux系统中,进程的执行模式划分为用户模式和内核模式,当进程运行于用户空间时属于用户模式,如果在用户程序运行过程中出现系统调用或者发生中断事件,就要运行操作系统(即核心)程序,进程的运行模式就变为 ...

  8. MAMP pro mac 本地集成环境 php sal apache等集成软件

    http://www.sdifen.com/mamppro411.html 已存在我的百度云盘 安装后,打开  MAMP 第一步:配置启动和停止选项 默认启动项.默认停止项,只需要勾选: 1.Star ...

  9. Spring MVC、MyBatis整合文件配置详解

    Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...

  10. PackedSyncPtr

    folly/PackedSyncPtr.h A highly specialized data structure consisting of a pointer, a 1-bit spin lock ...