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. StreamSets 部署 Pipelines 到 SDC Edge

    可以使用如下方法: 下载edge 运行包并包含pipeline定义文件. 直接发布到edge 设备. 在data colelctor 机器配置并配置了edge server 地址(主要需要网络可访问) ...

  2. Cocos2d-x调用Java 代码

    Java代码: package com.dishu; import com.dishu.org.R; import android.app.Activity; import android.app.A ...

  3. An internal error occurred during: "Android Library Update".

    打开 proj.android下的.cproject文件,把多余的CConfiguration配置节删掉,只留第一个就行了.

  4. Hadoop NameNode 高可用 (High Availability) 实现解析[转]

    NameNode 高可用整体架构概述 在 Hadoop 1.0 时代,Hadoop 的两大核心组件 HDFS NameNode 和 JobTracker 都存在着单点问题,这其中以 NameNode ...

  5. django创建第一个项目helloworld

    环境:centos 7,已安装python 3.6环境 1.安装django并创建django第一个项目 1.1.使用pip安装django# pip install Django或指定安装版本# p ...

  6. RK3288 双屏异显,两屏默认方向不一致

    CPU:RK3288 系统:Android 5.1 RK3288 支持双屏异显,一般都会同方向显示,如果遇到两个 lcd 的默认方向不一致,只需修改下面参数即可. 例如:主屏为mipi接口,分辨率为 ...

  7. 简单桶排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def BucketSort(a, n): barrel = np.zeros((1, n), dtype = 'i ...

  8. 【翻译】HTML5开发——轻量级Web Database存储库html5sql.js

    方式1: html5sql官方网址:http://html5sql.com/ 阅读之前,先看W3C关于WEB Database的一段话: Beware. This specification is n ...

  9. 慕课网 -- 性能优化之PHP优化总结笔记

    视频链接,感兴趣的可以去看看,对我来说耳目一新. http://www.imooc.com/learn/205 什么情况下遇到PHP性能问题 1 :PHP语法使用不恰当 2 :使用了PHP语言他不擅长 ...

  10. 彻底解密C++宽字符(一)

    彻底解密C++宽字符(一) 转:http://club.topsage.com/thread-2227977-1-1.html 1.从char到wchar_t “这个问题比你想象中复杂” 从字符到整数 ...