Fluent Mybatis零xml配置实现复杂嵌套查询
作者:tryternity 发布时间:2022-02-03 19:34:32
嵌套查询
使用Fluent Mybatis, 不用手写一行xml文件或者Mapper文件,在dao类中即可使用java api构造中比较复杂的嵌套查询。
让dao的代码逻辑和sql逻辑合二为一。
前置准备,maven工程设置
参考文章 使用FluentMybatis实现mybatis动态sql拼装和fluent api语法
in (select 子查询)
嵌套查询表和主查询表一样的场景
.column().in( query-> {对query设置条件})
只需要在in里面引用一个lambda表达式,lambda表达式入参是一个同名的Query。对这个入参可以设置where参数。
@DisplayName("嵌套查询和主查询的表是同一个")
@Test
void test_in_same_table_query() {
UserQuery query = new UserQuery()
.where.id().in(q -> q.selectId()
.where.id().eq(3L).end())
.and.userName().like("user")
.and.age().gt(23).end();
List list = mapper.listEntity(query);
// 通过Test4J工具,验证sql语句
db.sqlList().wantFirstSql()
.eq("SELECT id, gmt_create, gmt_modified, is_deleted, account, age, avatar, birthday, bonus_points, e_mail, password, phone, status, user_name " +
"FROM user WHERE id IN (SELECT id FROM user WHERE id = ?) " +
"AND user_name LIKE ? " +
"AND age > ?");
}
嵌套查询表是另外表的场景
.column().in(queryClass, query-> {对query设置条件})
如果嵌套查询的不是同表一张表,需要在in方法里面显式声明一下Query对象的class类型, 后面用法同方法一。
@DisplayName("嵌套查询和主查询的表是不同")
@Test
void test_in_difference_table_query() {
UserQuery query = new UserQuery()
.selectId()
.where.addressId().in(ReceivingAddressQuery.class, q -> q.selectId()
.where.id().in(new int[]{1, 2}).end())
.end();
mapper.listEntity(query);
// 通过Test4J工具,验证sql语句
db.sqlList().wantFirstSql()
.eq("SELECT id " +
"FROM user " +
"WHERE address_id IN (SELECT id FROM receiving_address WHERE id IN (?, ?))");
}
not in嵌套查询: 使用方法同 in 嵌套查询
exists (select子查询)
嵌套查询表和主查询表一样的场景
Exists查询不需要指定字段,直接在query where中可以引用exists方法。
exists( query-> {对query设置条件})
如果exists查询的表和主查询一致,直接在lambada表达式中使用同类型query参数即可,参数用法同in方法。
exists(queryClass, query-> {对query设置条件})
如果exists查询的表和主查询不一致,在exists方法第一个参数指定query类型,第二个参数同方法1。
具体示例
@DisplayName("EXISTS查询")
@Test
void test_exists_query() {
UserQuery query = new UserQuery()
.where.exists(ReceivingAddressQuery.class, q -> q
.where.detailAddress().like("杭州")
.and.id().apply(" = user.address_id").end())
.end();
mapper.listEntity(query);
// 通过Test4J工具,验证sql语句
db.sqlList().wantFirstSql()
.eq("SELECT id, gmt_create, gmt_modified, is_deleted, account, address_id, age, avatar, birthday, bonus_points, e_mail, password, phone, status, user_name " +
"FROM user " +
"WHERE EXISTS (SELECT *" +
" FROM receiving_address" +
" WHERE detail_address LIKE ?" +
" AND id = user.address_id)",
StringMode.SameAsSpace);
}
注:示例中的测试,是使用H2内存数据库,你可以直接运行,不需要你额外建表。
但使用Test4J执行测试,你需要在加入vm参数:-javaagent:/这里是你本地maven仓库地址/org/jmockit/jmockit/1.48/jmockit-1.48.jar
以我本机为例,具体参数如图:
Fluent Mybatis文档&示例
Fluent Mybatis源码, github
来源:https://blog.csdn.net/tryternity/article/details/109062641
猜你喜欢
- 一、多表联合分页查询1.多表联合查询结果集建议使用VO类,当然也可以使用resultMappackage com.cjhx.tzld.ent
- MyBatis Plus是一个MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。Mybati
- @EqualsAndHashCode注解@EqualsAndHashCode注解的作用就是自动实现model类的equals方法和hashc
- Condition就是实现了管程里面的条件变量。Java 语言内置的管程里只有一个条件变量,而Lock&Condition实现的管程
- 本文实例讲述了Android ActionBar Item用法。分享给大家供大家参考,具体如下:这里主要讲述ActionBar Item的使
- 本文实例为大家分享了Java实现打字游戏的具体代码,供大家参考,具体内容如下新建一个项目,然后在src里面建一个MyGame.java文件,
- 今天朋友圈又火了,听说原因是 @腾讯官网 就能得到一顶绿色的帽子,啊呸,是一个好看的国庆节头像,可是听说没一会就502了,那么我们自己动手实
- C语言奇偶排序算法奇偶排序,或奇偶换位排序,或砖排序,是一种相对简单的排序算法,最初发明用于有本地互连的并行计算。这是与冒泡排序特点类似的一
- 前言本文主要介绍下Spring事务中的传播行为。事务传播行为是Spring框架独有的事务增强特性,他不属于的事务实际提供方数据库行为。这是S
- C#自己没有Inputbox这个类,但是Inputbox也蛮好用的,所以有两种方法可以使用一:间接调用vb中的Inputbox功能 
- 什么是自动装箱,拆箱先抛出定义,Java中基础数据类型与它们的包装类进行运算时,编译器会自动帮我们进行转换,转换过程对程序员是透明的,这就是
- java 中cookie的详解Java对cookie的操作比较简单,主要介绍下建立cookie和读取cookie,以及如何设定cookie的
- 更正说明:我之前的的标题有点文不对题,我这篇博客的内容明明说的是:java中对象创建的过程,对内存之种底层的东西,我其实提的不太多。所以我原
- 本文为大家分享了Tablayout简单的使用方法,供大家参考,具体内容如下一、TabLayout普通用法在项目中使用viewpager的时候
- 本文主要讲解安装AndroidStudio和配置环境变量遇到一些问题,以及解决方法。需要的软件:AndriodStudio安装包.java
- 1. 前言什么是特殊矩阵?C++,一般使用二维数组存储矩阵数据。在实际存储时,会发现矩阵中有许多值相同的数据或有许多零数据,且分布呈现出一定
- 摘 要1. 生成多个防伪码,防伪码的长度和个数由用户指定。2. 防伪码由"0123456789ABCDEFGHJKLMNPQRST
- 本文讲述了WinForm中实现拖拽效果的功能,即在WinForm中有一个Button,可以实现拖拽这个Button到目标位置后生成一个该控件
- 一、首先编写一个工具类Hello:public class Hello { public static void say(Str
- 本文实例讲述了Android编程简单实现九宫格。分享给大家供大家参考,具体如下:实现的步骤1. 一个整体的容器部分。就是上图中包括整个图片项