用mp也可以方便的实现稍复杂点的条件查询,当然了很复杂的就还是要xml编写sql了。
一、wapper介绍
先看下mp的条件构造抽象类的结构:
Wrapper
: 条件构造抽象类,最顶端父类AbstractWrapper
: 用于查询条件封装,生成 sql 的 where 条件QueryWrapper
: Entity 对象封装操作类,不是用lambda语法UpdateWrapper
: Update 条件封装,用于Entity对象更新操作AbstractLambdaWrapper
: Lambda 语法使用 Wrapper统一处理解析lambda获取数据库字段LambdaQueryWrapper
: 用于Lambda语法使用的查询WrapperLambdaUpdateWrapper
: Lambda 更新封装Wrapper
不过最常用的还是QueryWrapper
、UpdateWrapper
等这些。
套路还是那样,先创建QueryWrapper
对象,然后再调用各种方法。
// 测试条件查询 @Test void testQueryWrapper() { //创建对象,泛型里加上实体对象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); // 设置查询的条件 // ge表示 >= , 这里就是查询age字段,大于40的数据 wrapperUser.ge("age", 40); // 调用查询方法中,传入wrapper对象 List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
这里就会查询表里age>=40,的数据,看下执行过程的sql语句:
二、常用的条件方法
在构造条件的时候,除了上面的ge
,还有很多其他的方法,这里简单介绍下比较常用的,并且贴出执行的sql。
1. gt
表示 >
... ... // gt表示 > , 这里就是查询age字段,大于40的数据 wrapperUser.gt("age", 40); ... ...
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age > ? ==> Parameters: 40(Integer)
2. le
表示 <=
... ... // le表示 <=, 这里就是查询age字段,小于等于40的数据 wrapperUser.le("age", 40); ... ...
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age <= ? ==> Parameters: 40(Integer)
3. lt
表示 <
... ... // lt表示 <, 这里就是查询age字段,小于40的数据 wrapperUser.lt("age", 40); ... ...
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age < ? ==> Parameters: 40(Integer)
4. isNull
表示 查询值为null
... ... // isNull wrapperUser.isNull("name"); ... ...
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND name IS NULL ==> Parameters: <== Total: 0
5. isNotNull
表示 查询值为不为null
... ... // isNotNull wrapperUser.isNotNull("name"); ... ...
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND name IS NOT NULL ==> Parameters:
6. eq
表示 =
... ... // eq wrapperUser.eq("name", "大周4"); ... ...
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND name = ? ==> Parameters: 大周4(String)
7. ne
表示 !=
... ... // eq wrapperUser.ne("name", "大周4"); ... ...
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND name <> ? ==> Parameters: 大周4(String)
8. between
表示 在范围之间,包含边界值
... ... // between wrapperUser.between("age", 40, 50); ... ...
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age BETWEEN ? AND ? ==> Parameters: 40(Integer), 50(Integer)
9. notBetween
表示 在范围之外,不含边界值
... ... // between wrapperUser.notBetween("age", 40, 50); ... ...
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age NOT BETWEEN ? AND ? ==> Parameters: 40(Integer), 50(Integer)
10. notBetween
表示 在范围之外,不含边界值
... ... // between wrapperUser.notBetween("age", 40, 50); ... ...
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age NOT BETWEEN ? AND ? ==> Parameters: 40(Integer), 50(Integer)
11. allEq
多条件查询
如果我where后面要加多个条件,可以使用allEq。先创建一个hashmap,然后把多个条件put进去,再调用allEq即可。
@Test void testQueryWrapper() { QueryWrapper<User> wrapperUser = new QueryWrapper<>(); Map<String, Object> map = new HashMap<>(); map.put("id", 5); map.put("name", "wesson5"); map.put("age", 29); wrapperUser.allEq(map); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND name = ? AND id = ? AND age = ? ==> Parameters: wesson5(String), 5(Integer), 29(Integer)
12. .
链式编程,多条件查询
此外,还可以使用链式编程,直接在后面继续.
调用别的方法。
@Test void testQueryWrapper() { //创建对象,泛型里加上实体对象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.eq("age", 29) .eq("name", "wesson5") .eq("id", 5); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age = ? AND name = ? AND id = ? ==> Parameters: 29(Integer), wesson5(String), 5(Integer)
13. or、and
默认情况下,在不调拨or()方法的情况下,是使用and()。
@Test void testQueryWrapper() { //创建对象,泛型里加上实体对象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.eq("age", 29) .or() .eq("name", "wesson5") .or() .eq("id", 5); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age = ? OR name = ? OR id = ? ==> Parameters: 29(Integer), wesson5(String), 5(Integer)
14. 嵌套or、嵌套and
查询sql经常会有嵌套or或者and的情况,可以这样写:
@Test void testQueryWrapper() { //创建对象,泛型里加上实体对象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.eq("age", 29) .or( i -> i.eq("name", "wesson5") .or() .eq("id", 5) ); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age = ? OR ( name = ? OR id = ? ) ==> Parameters: 29(Integer), wesson5(String), 5(Integer)
15. in、notIn
等于sql里的 in和not in。
@Test void testQueryWrapper() { //创建对象,泛型里加上实体对象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.in("id", 1, 2, 3); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND id IN (?,?,?) ==> Parameters: 1(Integer), 2(Integer), 3(Integer)
16. inSql、notinSql
inSql、notinSql可以用来子查询,比如 where id in (select * ... ...)
@Test void testQueryWrapper() { //创建对象,泛型里加上实体对象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.in("id", "select id from user where id < 5"); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND id IN (?) ==> Parameters: select id from user where id < '5'(String)
17. last
last可以直接拼接sql到最后,只能调用一次,多次调用以最后一次为准。
注意:有sql注入的风险,慎用。
@Test void testQueryWrapper() { //创建对象,泛型里加上实体对象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.last("limit 1"); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp执行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 limit 1 ==> Parameters:
18. 指定要查询的列
只查询出指定的字段,比如"id", "name", "age"。
@Test void testQueryWrapper() { //创建对象,泛型里加上实体对象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.select("id", "name", "age"); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp执行的sql:
==> Preparing: SELECT id,name,age FROM user WHERE deleted=0 ==> Parameters:
以上是一些在业务开发中常用的,稍复杂些的条件查询,实际情况可能还有其他组合变化。