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. 去掉PowerDesigner生成SQL脚本中字段名带的引号

    使用PowerDesigner生成数据库建表SQL脚本时,尤其是Oracle数据库时,表名一般会带引号.其实加引号是PL/SQL的规范,数据库会严格按照“”中的名称建表,如果没有“”,会按照ORACL ...

  2. CentOS 7安装TigerVNC Server

    http://blog.csdn.net/wamath/article/details/76003128 1. CentOS 7安装TigerVNC Server 本文介绍如何在CentOS 7上安装 ...

  3. jeecms一些经典标签

    http://www.121ask.com/thread-5512-1.html [@cms_channel_list]标签详细介绍 http://www.jeecmstheme.com/2014/0 ...

  4. 锐捷 rg-S2026f 学习笔记

    1.通过串口连接交换机: http://support.ruijie.com.cn/forum.php?mod=viewthread&tid=32250&extra=page%3D1& ...

  5. SpringCloud初体验:一、Eureka 服务的注册与发现

    Eureka :云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移. Eureka 可以大致理解为 房产中介 和 房东 的关系,房东想让租客租房子,首先要把房子 ...

  6. Asp.net MVC 出现“Failed to map the path '/'.”错误

    Server Error in '/' Application. Parser Error Description: An error occurred during the parsing of a ...

  7. (转)为C# Windows服务添加安装程序

    本文转载自:http://kamiff.iteye.com/blog/507129 最近一直在搞Windows服务,也有了不少经验,感觉权限方面确定比一般程序要受限很多,但方便性也很多.像后台运行不阻 ...

  8. Java 简单UDP通信

    Java手册 java.net Class DatagramSocket java.lang.Object java.net.DatagramSocket All Implemented Interf ...

  9. 【Codeforces】Educational Codeforces Round 46(Contest 1000)

    题目 传送门:QWQ A:Codehorses T-shirts 题意: 给定一些字符串表示去年和今年的衣服型号大小( XL XXL M...... ),要求用最少的次数把去年的衣服大小改成今年需要的 ...

  10. Oracle 级联with admin option 和 with grant option

    · 授权通过grant 语法:GRANT object_priv[(columns)][ON object] TO {user|role|public} [WITH GRANT OPTION] · 回 ...