为了账号安全,请及时绑定邮箱和手机立即绑定

MyBatis Plus学习笔记(六)

标签:
Java SSM

image-20210203114921218

6. 条件构造器

MyBatis的另外一个特点就是支持动态的sql,可以使用框架提供的语法进行动态的sql组装。MP为此提供了强大的条件造器AbstractWrapper依旧不用编写sql语句,就能实现动态sql的组装。
条件构造器有以下几类,可以看到AbstractWrapper是QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类,用于生成 sql 的where 条件;而且构造器支持普通的泛型参数和lambda表达式参数。

image-20210113170201964
注意 : 普通的构造器使用的是 数据库字段,不是Java属性 !而Lambda构造器可以使用Java属性。

6.1. 通用的条件构造方法

AbstractWrapper里面包含的方法是公共的。我们只介绍其方法,真正在使用的时候,会使用子类来构件查询条件。

6.1.1. allEq

全部eq(或个别isNull),参数说明:

参数名称 参数类型 参数描述
condition boolean 表示该条件是否加入最后生成的sql中
params Map<R, V> key为数据库字段名,value为字段值
filter filter 过滤函数,是否允许字段传入比对条件中
null2IsNull boolean 为true则在map的value为null时调用 isNull 方法,为false时则忽略value为null的

用法举例:

@Test
public void allEqTest(){
    Map<String,Object> paramMap = new HashMap<>();
    paramMap.put("id",6);
    paramMap.put("last_name","刘六");
    paramMap.put("age",null);
    //根据构造的查询条件map进行拼接条件
    userMapper.selectList(new QueryWrapper<User>().allEq(paramMap));
    //会忽略掉is null的属性
    userMapper.selectList(new QueryWrapper<User>().allEq(paramMap,false));
    //过滤掉key里面不含a的条件
    userMapper.selectList(new QueryWrapper<User>().allEq((k,v)->k.indexOf("a")>=0,paramMap,false));
    //条件不在最终的sql上拼接
    userMapper.selectList(new QueryWrapper<User>().allEq(false,(k,v)->k.indexOf("a")>=0,paramMap,false));
}

对应生成的sql语句

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE last_name = '刘六' AND id = 6 AND age IS NULL;
------------------------------------------------------------------------------------------------------------------------

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE last_name = '刘六' AND id = 6;
------------------------------------------------------------------------------------------------------------------------

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE last_name = '刘六';
------------------------------------------------------------------------------------------------------------------------

SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.2. eq

等于(=),参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
val Object 查询条件的值

用法举例:

@Test
public void eqTest(){
    userMapper.selectList(new QueryWrapper<User>().eq("last_name","刘六"));
    userMapper.selectList(new QueryWrapper<User>().eq(false,"last_name","刘六"));
    //Lambda查询条件构造器
    userMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getLastName,"刘六"));
    userMapper.selectList(new LambdaQueryWrapper<User>().eq(false,User::getLastName,"刘六"));
    
    //错误写法,数据库表中并没有level字段
    userMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getLevel,0));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE last_name = '刘六';
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE last_Name = '刘六';
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

注:
- 可以看出两种条件构造器最终生成的SQL语句是一样的,但是使用Lambda的条件构造器是通过实体的属性然后由MP帮我们转换为数据库对应的字段,避免普通条件构造器数据库字段书写失误或者数据库字段修改而查询条件时候不容易发现漏掉修改导致查询错误。所以后面的示例将都采取Lambda的条件构造器。
- 使用Lambda的条件构造器如果实体中的使用@TableField(exist = false)排除的字段这里并没有生效?

6.1.3. ne

不等于(<>),参数说明:

同上

用法举例:

@Test
public void neTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().ne(User::getLastName,"刘六"));
    userMapper.selectList(new LambdaQueryWrapper<User>().ne(false,User::getLastName,"刘六"));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE last_Name <> '刘六';
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.4. gt

大于 (>),参数说明:

同上

用法举例:

@Test
public void gtTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().gt(User::getAge,20));
    userMapper.selectList(new LambdaQueryWrapper<User>().gt(false,User::getAge,20));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE age > 20;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.5. ge

大于等于 (>=),参数说明:

同上

用法举例:

@Test
public void geTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().ge(User::getAge,20));
    userMapper.selectList(new LambdaQueryWrapper<User>().ge(false,User::getAge,20));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE age >= 20;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.6. lt

小于 (<),参数说明:

同上

用法举例:

@Test
public void ltTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().lt(User::getAge,60));
    userMapper.selectList(new LambdaQueryWrapper<User>().lt(false,User::getAge,60));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE age < 60;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.7. le

小于等于 (<=),参数说明:

同上

用法举例:

@Test
public void leTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().le(User::getAge,60));
    userMapper.selectList(new LambdaQueryWrapper<User>().le(false,User::getAge,60));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE age <= 60;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.8. between

BETWEEN 值1 AND 值2,参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
val1 Object 查询条件的值1
val2 Object 查询条件的值2

用法举例:

@Test
public void betweenTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().between(User::getAge,20,60));
    userMapper.selectList(new LambdaQueryWrapper<User>().between(false,User::getAge,20,60));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE age BETWEEN 20 AND 60;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.9. notBetween

NOT BETWEEN 值1 AND 值2,参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
val1 Object 查询条件的值1
val2 Object 查询条件的值2

用法举例:

@Test
public void notBetweenTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().notBetween(User::getAge,20,60));
    userMapper.selectList(new LambdaQueryWrapper<User>().notBetween(false,User::getAge,20,60));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE age NOT BETWEEN 20 AND 60;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.10. like

LIKE ‘%值%’,参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
val Object 查询条件的值

用法举例:

@Test
public void likeTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().like(User::getLastName,"张"));
    userMapper.selectList(new LambdaQueryWrapper<User>().like(false,User::getLastName,"张"));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE last_Name LIKE '%张%';
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.11. notLike

NOT LIKE ‘%值%’,参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
val Object 查询条件的值

用法举例:

@Test
public void notLikeTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().notLike(User::getLastName,"张"));
    userMapper.selectList(new LambdaQueryWrapper<User>().notLike(false,User::getLastName,"张"));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE last_Name NOT LIKE '%张%';
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.12. likeLeft

LIKE ‘%值’,参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
val Object 查询条件的值

用法举例:

@Test
public void likeLeftTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().likeLeft(User::getLastName,"张"));
    userMapper.selectList(new LambdaQueryWrapper<User>().likeLeft(false,User::getLastName,"张"));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE last_Name NOT LIKE '%张';
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.13. likeRight

LIKE ‘值%’,参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
val Object 查询条件的值

用法举例:

@Test
public void likeRightTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().likeRight(User::getLastName,"张"));
    userMapper.selectList(new LambdaQueryWrapper<User>().likeRight(false,User::getLastName,"张"));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE last_Name NOT LIKE '张%';
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.14. isNull

字段 IS NULL,参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名

用法举例:

@Test
public void isNullTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().isNull(User::getEmail));
    userMapper.selectList(new LambdaQueryWrapper<User>().isNull(false,User::getEmail));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE email IS NULL;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.15. isNotNull

字段 IS NOT NULL,参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名

用法举例:

@Test
public void isNotNullTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().isNotNull(User::getEmail));
    userMapper.selectList(new LambdaQueryWrapper<User>().isNotNull(false,User::getEmail));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE email IS NOT NULL;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.16. in

字段 IN (value.get(0), value.get(1), …),参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
coll Collection<?> 范围值列表形式
values Object… 范围值多个值,与coll不能同用

用法举例:

@Test
public void inTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().in(User::getId, Arrays.asList(1,2,3)));
    userMapper.selectList(new LambdaQueryWrapper<User>().in(User::getId, 1,2,3));
    userMapper.selectList(new LambdaQueryWrapper<User>().in(false,User::getId, 1,2,3));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE id IN (1,2,3);
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE id IN (1,2,3);
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.17. notIn

字段 NOT IN (value.get(0), value.get(1), …),参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
coll Collection<?> 范围值列表形式
values Object… 范围值多个值,与coll不能同用

用法举例:

@Test
public void notInTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().notIn(User::getId, Arrays.asList(1,2,3)));
    userMapper.selectList(new LambdaQueryWrapper<User>().notIn(User::getId, 1,2,3));
    userMapper.selectList(new LambdaQueryWrapper<User>().notIn(false,User::getId, 1,2,3));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE id NOT IN (1,2,3);
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE id NOT IN (1,2,3);
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.18. inSql

字段 IN ( sql语句 ),参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
inValue String sql语句

用法举例:

@Test
public void inSqlTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().inSql(User::getId,"1,2,3,4,5"));
    userMapper.selectList(new LambdaQueryWrapper<User>().inSql(User::getLastName,"'张三','李四'"));
    userMapper.selectList(new LambdaQueryWrapper<User>().inSql(User::getId,"select id from tbl_user where age >20"));
    userMapper.selectList(new LambdaQueryWrapper<User>().inSql(false,User::getId,"select id from tbl_user where age >20"));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE id IN (1,2,3,4,5);
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE last_Name IN ('张三','李四');
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE id IN (select id
 FROM tbl_user
 WHERE age >20);
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

注:inSql方法就是把条件的sql当做字符串拼接进去了,前两种方法属于取巧,应该使用in(),这里建议里面只使用sql语句。

6.1.19. notInSql

字段 NOT IN ( sql语句 ),参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
inValue String sql语句

用法举例:

@Test
public void notInSqlTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().notInSql(User::getId,"select id from tbl_user where age >20"));
    userMapper.selectList(new LambdaQueryWrapper<User>().notInSql(false,User::getId,"select id from tbl_user where age >20"));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE id NOT IN (select id
 FROM tbl_user
 WHERE age >20);
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.20. groupBy

分组:GROUP BY 字段, …,参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
columns R… 数据库对应的多个字段名

用法举例:

@Test
public void groupBy(){
    userMapper.selectList(new LambdaQueryWrapper<User>().groupBy(User::getId,User::getAge));
    //可配合select,对需要的字段使用聚合函数,select后面会演示
    userMapper.selectList(new QueryWrapper<User>().select("age,count(*)").lambda().groupBy(User::getAge));
    userMapper.selectList(new LambdaQueryWrapper<User>().groupBy(false,User::getId,User::getAge));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user GROUP BY id,age;
------------------------------------------------------------------------------------------------------------------------
SELECT age,count(*)
 FROM tbl_user GROUP BY age;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.21. orderByAsc

排序:ORDER BY 字段, … ASC,参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
columns R… 数据库对应的多个字段名

用法举例:

@Test
public void orderByAscTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().orderByAsc(User::getAge,User::getId));
    userMapper.selectList(new LambdaQueryWrapper<User>().orderByAsc(false,User::getAge,User::getId));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user ORDER BY age ASC , id ASC;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.22. orderByDesc

排序:ORDER BY 字段, … DESC,参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
column R 数据库对应的字段名
columns R… 数据库对应的多个字段名

用法举例:

@Test
public void orderByDescTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().orderByDesc(User::getAge,User::getId));
    userMapper.selectList(new LambdaQueryWrapper<User>().orderByDesc(false,User::getAge,User::getId));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user ORDER BY age DESC , id DESC;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.23. orderBy

排序:ORDER BY 字段, …,参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
isAsc boolean 是否是 ASC 排序
columns R… 数据库对应的多个字段名

用法举例:

@Test
public void orderByTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().orderBy(true,true,User::getAge));
    userMapper.selectList(new LambdaQueryWrapper<User>().orderBy(true,false,User::getAge));
    userMapper.selectList(new LambdaQueryWrapper<User>().orderBy(false,false,User::getAge));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user ORDER BY age ASC;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user ORDER BY age DESC;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.24. having

HAVING ( sql语句 ),必须配合GROUP BY使用,如果前面没有GROUP BY,那么最终生成的SQL不会拼装上having语句。参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
sqlHaving String sql 语句
columns R… 数据库对应的多个字段名

用法举例:

@Test
public void havingTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().having("sum(age) > 10"));
    userMapper.selectList(new LambdaQueryWrapper<User>().groupBy(User::getId,User::getAge).having("sum(age) > 10"));
    userMapper.selectList(new LambdaQueryWrapper<User>().groupBy(User::getId,User::getAge).having("sum(age) > {0}",10));
    userMapper.selectList(new LambdaQueryWrapper<User>().groupBy(User::getId,User::getAge).having(false,"sum(age) > {0}",10));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user GROUP BY id,age HAVING sum(age) > 10;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user GROUP BY id,age HAVING sum(age) > 10;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user GROUP BY id,age;
------------------------------------------------------------------------------------------------------------------------

6.1.25. or

拼接 OR,参数说明:
参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
func Function<Param, Param> 函数

用法举例:

@Test
public void orTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().or().eq(User::getLastName,"张三"));
    userMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getAge,30).or().eq(User::getLastName,"张三"));
    userMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getAge,30).or(u->u.eq(User::getLastName,"张三").ne(User::getId,6)));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE last_Name = '张三';
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE age = 30 OR last_Name = '张三';
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE age = 30 OR ( last_Name = '张三' AND id <> 6 );
------------------------------------------------------------------------------------------------------------------------

6.1.26. and

拼接 AND,参数说明:
参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
func Function<Param, Param> 函数

用法举例:

@Test
public void andTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getAge,30).and(u->u.eq(User::getLastName,"张三").ne(User::getId,6)));
    userMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getAge,30).and(false,u->u.eq(User::getLastName,"张三").ne(User::getId,6)));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE age = 30 AND ( last_Name = '张三' AND id <> 6 );
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE age = 30;
------------------------------------------------------------------------------------------------------------------------

主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接),所以and()最主要的用法就是嵌套条件使用。

6.1.27. nested

正常嵌套 不带 AND 或者 OR,参数说明:
参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
func Function<Param, Param> 函数

用法举例:

@Test
public void nestedTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().nested(i -> i.eq(User::getId, 2L).or().eq(User::getId, 3L))
            .and(i -> i.ge(User::getAge, 20)));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE ( id = 2 OR id = 3 ) AND ( age >= 20 );
------------------------------------------------------------------------------------------------------------------------

6.1.28. apply

拼接 sql,参数说明:
参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
applySql String sql语句
value Object… 参数值数组

用法举例:

@Test
public void applyTest(){
    //有sql注入的风险
    userMapper.selectList(new LambdaQueryWrapper<User>().apply("id = 6"));
    userMapper.selectList(new LambdaQueryWrapper<User>().apply("id = {0} or last_name like {1}",1,"%张三%"));
    userMapper.selectList(new LambdaQueryWrapper<User>().gt(User::getAge,18).apply("id = {0} or last_name like {1}",1,"%张三%"));
    userMapper.selectList(new LambdaQueryWrapper<User>().apply("id = {0} or last_name like {1}",1,"%张三%").gt(User::getAge,18));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE id = 6;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE id = 1 or last_name like '%张三%';
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE age > 18 AND id = 1 or last_name like '%张三%';
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE id = 1 or last_name like '%张三%' AND age > 18;
------------------------------------------------------------------------------------------------------------------------

注意事项:
该方法可用于数据库函数 动态入参的params对应前面applySql内部的{index}部分.这样是不会有sql注入风险的,反之会有!

6.1.29. last

无视优化规则直接拼接到 sql 的最后,参数说明:
参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
lastSql String sql语句

用法举例:

@Test
public void lastTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().apply("id = 6"));
    //此写法会报错,因为last是无视优化规则直接拼接到 sql 的最后,而apply会根据优化规则生成关键字
    //userMapper.selectList(new LambdaQueryWrapper<User>().last("id = 6"));
    userMapper.selectList(new LambdaQueryWrapper<User>().last("limit 1"));
    //多次使用last会覆盖之前的,以最后一次为准
    userMapper.selectList(new LambdaQueryWrapper<User>().last("limit 1").last("limit 2").last("limit 3"));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE id = 6;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user
 LIMIT 1;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user
 LIMIT 3;
------------------------------------------------------------------------------------------------------------------------

6.1.30. exists

拼接 EXISTS ( sql语句 ),参数说明:
参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
existsSql String sql语句

用法举例:

@Test
public void existsTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().exists("select id from tbl_user where age = 1"));
    userMapper.selectList(new LambdaQueryWrapper<User>().exists(false,"select id from tbl_user where age = 1"));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE EXISTS (select id
 FROM tbl_user
 WHERE age = 1);
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.1.31. notExists

拼接 NOT EXISTS ( sql语句 ),参数说明:
参数说明:

参数名称 参数类型 参数描述
condition boolean 同上
existsSql String sql语句

用法举例:

@Test
public void existsTest(){
    userMapper.selectList(new LambdaQueryWrapper<User>().notExists("select id from tbl_user where age = 1"));
    userMapper.selectList(new LambdaQueryWrapper<User>().notExists(false,"select id from tbl_user where age = 1"));
}

对应生成的SQL:

SELECT id,last_Name,age,email
 FROM tbl_user
 WHERE NOT EXISTS (select id
 FROM tbl_user
 WHERE age = 1);
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,age,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

6.2. QueryWrapper查询条件构造器

QueryWrapper单独提供了设置查询字段的方法。

6.2.1. select

设置查询字段,参数说明:

参数名称 参数类型 参数描述
columns SFunction<T, ?>… 查询字段
entityClass Class 查询实体的类型
predicate Predicate 过滤方式

用法举例:

@Test
public void selectTest(){
    //前三种方法效果是一样的
    userMapper.selectList(new QueryWrapper<User>().select("id,last_name"));
    userMapper.selectList(new QueryWrapper<User>().lambda().select(User::getId,User::getLastName));
    userMapper.selectList(new LambdaQueryWrapper<User>().select(User::getId,User::getLastName));
    //i->false时只获取主键,true时获取所有字段
    userMapper.selectList(new LambdaQueryWrapper<User>().select(User.class,i->false));
    //可以根据TableFieldInfo属性获取字段
    userMapper.selectList(new LambdaQueryWrapper<User>().select(User.class, TableFieldInfo::isCharSequence));
    //过滤字段名
    userMapper.selectList(new LambdaQueryWrapper<User>().select(User.class, i->i.getProperty().startsWith("last")));
    //入参不包含 class 的调用前需要wrapper内的entity属性有值
    userMapper.selectList(new LambdaQueryWrapper<>(new User()).select(i -> i.getProperty().startsWith("last")));
    //这两类方法重复调用以最后一次为准
    userMapper.selectList(new LambdaQueryWrapper<User>().select(User.class, i->i.getProperty().startsWith("last"))
            .select(User::getId,User::getLastName,User::getEmail));
}

对应生成的SQL:

SELECT id,last_name
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------
SELECT id
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name
 FROM tbl_user
 WHERE id=0;
------------------------------------------------------------------------------------------------------------------------
SELECT id,last_Name,email
 FROM tbl_user;
------------------------------------------------------------------------------------------------------------------------

以上方分法为两类. 第一类方法为:指定查询字段. 第二类方法为:过滤查询字段(主键除外),入参不包含 class 的调用前需要wrapper内的entity属性有值! 这两类方法重复调用以最后一次为准.

6.2.2. excludeColumns

排除查询字段,已从3.0.5版本上移除此方法!

6.3. UpdateWrapper 更新条件构造器

UpdateWrapper单独提供了设置跟新字段的方法。

6.3.1. set

设置更新部分的set字段,参数说明:

参数名称 参数类型 参数描述
condition boolean 是否加入 set
column R 字段
val Object

用法举例:

@Test
public void setTest(){
    //实体中设置的值和构造器设置的值会共同作用,最终会以最后一个设置为准
    User user = new User();
    user.setId(6l);
    user.setLastName("李三");
    userMapper.update(user,new LambdaUpdateWrapper<User>().set(User::getLastName,"王五").set(User::getAge,19)
            .eq(User::getId,6));
    // 附加更新内容为空时  不能传入null  需要传入空实体对象
    userMapper.update(new User(),new LambdaUpdateWrapper<User>().set(User::getLastName,"李四").set(User::getAge,19)
            .eq(User::getId,6l));
    //更新构造器是进行的set部分的构造
    //如果通过更新构造器只设置了更新数值,而没有使用eq设置id,即使实体设置过id最终生成的SQL语句也不会拼接上id
    //所以这是批量更新操作,需要搭配查询构造器构建条件,否则就是全表更新要谨慎使用
    userMapper.updateById(user);
    userMapper.update(user,new LambdaUpdateWrapper<User>().set(User::getLastName,"李四").set(User::getAge,19));
}

对应生成的SQL:

UPDATE tbl_user SET last_Name='李三', last_Name='王五',age=19
 WHERE id = 6;
------------------------------------------------------------------------------------------------------------------------
UPDATE tbl_user SET last_Name='李四',age=19
 WHERE id = 6;
------------------------------------------------------------------------------------------------------------------------
UPDATE tbl_user SET last_Name='李三'
 WHERE id=6;
------------------------------------------------------------------------------------------------------------------------
UPDATE tbl_user SET last_Name='李三', last_Name='李四',age=19;
------------------------------------------------------------------------------------------------------------------------

6.3.2. setSql

设置 SET 部分 SQL,参数说明:

参数名称 参数类型 参数描述
condition boolean 是否加入 set
sql String set sql片段

用法举例:

@Test
public void setSqlTest(){
    // 附加更新内容为空时  不能传入null  需要传入空实体对象
    userMapper.update(new User(),new LambdaUpdateWrapper<User>().setSql("last_name='Tom'").eq(User::getId,6l));
}

对应生成的SQL:

UPDATE tbl_user SET last_name='Tom'
 WHERE id = 6;
------------------------------------------------------------------------------------------------------------------------

6.3.3. lambda

获取 LambdaWrapper,
在QueryWrapper中是获取LambdaQueryWrapper
在UpdateWrapper中是获取LambdaUpdateWrapper

6.4 Wrapper中的condition使用场景

userService.query().likeRight(StringUtils.isNotEmpty(loginName), User::getLoginName, loginName)
                        .likeRight(StringUtils.isNotEmpty(nickname), User::getNickname, nickname)
                        .eq(Objects.nonNull(status), User::getStatus, status);

当查询参数loginName不为空的时候,likeRight才会拼接SQL。
最终生成的SQL:

SELECT uid AS id,login_name,password,nickname,email,phone,ip,status,create_uid,create_time,update_time FROM sys_user WHERE login_name LIKE '1%';
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消