本篇内容介绍了“mybatisplus如何实现条件查询”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
用mp也可以方便的实现稍复杂点的条件查询,当然了很复杂的就还是要xml编写sql了。
先看下mp的条件构造抽象类的结构:
Wrapper: 条件构造抽象类,最顶端父类
AbstractWrapper: 用于查询条件封装,生成 sql 的 where 条件
QueryWrapper: Entity 对象封装操作类,不是用lambda语法
UpdateWrapper: Update 条件封装,用于Entity对象更新操作
AbstractLambdaWrapper: Lambda 语法使用 Wrapper统一处理解析lambda获取数据库字段
LambdaQueryWrapper: 用于Lambda语法使用的查询Wrapper
LambdaUpdateWrapper: 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。
... ... // 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)
... ... // 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)
... ... // 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)
... ... // 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
... ... // 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:
... ... // 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)
... ... // 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)
... ... // 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)
... ... // 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)
... ... // 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)
如果我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)
此外,还可以使用链式编程,直接在后面继续.调用别的方法。
@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)
默认情况下,在不调拨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)
查询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)
等于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)
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)
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:
只查询出指定的字段,比如"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:
“mybatisplus如何实现条件查询”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。