嵌套查询
使用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")