Mybatis 实用篇(四)返回值类型
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>
注意:
- 支持多列传值:column="{key1=colum1, key2=colum2}"
- 分步查找支持缓存,可以配置属性 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 实用篇(四)返回值类型的更多相关文章
- MyBatis查询结果resultType返回值类型详细介绍
一.返回一般数据类型 比如要根据 id 属性获得数据库中的某个字段值. mapper 接口: // 根据 id 获得数据库中的 username 字段的值 String getEmpNameById( ...
- MyBatis中Mapper的返回值类型
insert.update.delete语句的返回值类型 对数据库执行修改操作时,数据库会返回受影响的行数. 在MyBatis(使用版本3.4.6,早期版本不支持)中insert.update.del ...
- ResultMap和ResultType在使用中的区别、MyBatis中Mapper的返回值类型
在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解 resultType:当使 ...
- int不可为null引发的 MyBatis做持久层框架,返回值类型要为Integer问题
MyBatis做持久层框架,返回值类型要为Integer MyBatis 做持久层时,之前没注意,有时候为了偷懒使用了int类型做为返回的类型,这样是不可取的,MyBatis做持久层框架,返回值类型要 ...
- mybatis的XML返回值类型报错
昨天项目里一直报错说是一个文件里的返回值java.util.hashmap不对,然后去定位这个文件发现并没有问题,后来在全局搜索的帮助下查找了返回值类型为resultMap的文件里看到写的代码里有: ...
- springMVC入门(四)------参数绑定与返回值类型
简介 从之前的介绍,已经可以使用springMVC完成完整的请求.返回数据的功能. 待解决的问题:如何将数据传入springMVC的控制器进行后续的处理,完成在原生servlet/jsp开发中Http ...
- C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解
前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 C#进阶系列——WebApi接口传参不再困惑:传参详解 ...
- WebApi 接口返回值类型详解 ( 转 )
使用过Webapi的园友应该都知道,Webapi的接口返回值主要有四种类型 void无返回值 IHttpActionResult HttpResponseMessage 自定义类型 此篇就围绕这四块分 ...
- WebApi接口返回值不困惑:返回值类型详解
前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.作为程序猿,我们都知道参数和返回值是编程领域不可分割的两大块,此前分享了 ...
随机推荐
- 《DSP using MATLAB》Problem 2.9
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- Ext.js 之MVC
Ext.js 4.0之MVC
- (简单)冒泡和直接选择排序同时调用swap算法
void swap(int &a , int &b) { int temp; temp = a; a=b; b=temp; } void bubble(int a[],int n) { ...
- Spring 部署Tomcat 404 错误解决方案
将Spring项目部署到tomcat后,访问网页出现404错误 HTTP Status 404 – Not Found The origin server did not find a current ...
- C#:进程、线程、应用程序域(AppDomain)与上下文分析
进程 进程是操作系统用于隔离众多正在运行的应用程序的机制.在.Net之前,每一个应用程序被加载到单独的进程中,并为该进程指定私有的虚拟内存.进程不能直接访问物理内存,操作系统通过其它的处理把这 ...
- SQL中減少日志文件大小
SQL中減少日志文件大小 编写人:CC阿爸 2014-6-14 在日常SQL数据库的操作中,常常会出现SQL日志文件超大,大小都超过正常MDF数据库文件,作为一般用户来讲,LDF太大,只会影响服务 ...
- XSS 与 CSRF 两种跨站攻击
在前几年,大家一般用拼接字符串的方式来构造动态 SQL 语句创建应用,于是 SQL 注入成了很流行的攻击方式, 但是现在参数化查询 已经成了普遍用法,我们已经离 SQL 注入很远了.但是历史同样悠久的 ...
- Netty私有协议栈 读书笔记
1.数据结构定义 1)netty消息:NettyMessage package com.cherry.netty.demo.protocolstack.pojo; import com.cherry. ...
- request 里面参数设置 (有空瞄下)
Requests 是用python语言编写的第三方库,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,完全满足 HTTP 测试需求, ...
- Bootstrap-CL:面包屑导航
ylbtech-Bootstrap-CL:面包屑导航 1.返回顶部 1. Bootstrap 面包屑导航(Breadcrumbs) 面包屑导航(Breadcrumbs)是一种基于网站层次信息的显示方式 ...