SpringBoot整合MybatisPlus有关条件构造器的相关查询方法

详情或有不足请参考官方文档:https://mp.baomidou.com

最近在学习mybatisPlus时总结了一些常用的有关条件构造器的查询方法,以此做下记录,方便学习使用!

eq: 等于 =

例子:eq("name", "老王")--->name = '老王'

ne: 不等于 <>

例子:ne("name", "老王")--->name <> '老王'

gt: 大于 >

例子:gt("age", 18)--->age > 18

ge: 大于等于 >=

例子:ge("age", 18)--->age >= 18

lt: 小于 <

例子:lt("age", 18)--->age < 18

le: 小于等于 <=

例子:le("age", 18)--->age <= 18

between: 值 1 AND 值 2

例子:between("age", 18, 30)--->age between 18 and 30

notBetween: Not BETWEEN 值 1 AND 值 2

例子:notBetween("age", 18, 30)--->age not between 18 and 30

like: like'%值%'

例子:like("name", "王")--->name like '%王%'

notLike: NOT LIKE '%值%'

例子:notLike("name", "王")--->name not like '%王%'

likeLeft: Like'%值'

例子:likeLeft("name", "王")--->name like '%王'

likeRight: LIKE'值%'

例子:likeRight("name", "王")--->name like '王%'

isNull: 字段 IS NULL

例子:isNull("name")--->name is null

isNotNull:字段 IS NOT NULL

例子:isNotNull("name")--->name is not null

in: 字段IN(value.get(0),value.get(1),...)

例子:in("age",{1,2,3})--->age in (1,2,3)

     字段 IN (v0, v1, ...)

例子:in("age", 1, 2, 3)--->age in (1,2,3)

notIn: 字段 IN (value.get(0), value.get(1), ...)

例子:notIn("age",{1,2,3})--->age not in (1,2,3)

           字段 NOT IN (v0, v1, ...)

例子:notIn("age", 1, 2, 3)--->age not in (1,2,3)

inSql: 字段 IN(SQL语句)

例子1:inSql("age", "1,2,3,4,5,6")--->age in (1,2,3,4,5,6)

例子2:inSql("id", "select id from table where id < 3")--->id in (select id from  table where id < 3)

notInSql: 字段NOT IN (sql语句)

例子1:notInSql("age", "1,2,3,4,5,6")--->age not in (1,2,3,4,5,6)

例子2:notInSql("id", "select id from table where id < 3")--->age not in (select id from table where id < 3)

分组:GROUP BY 字段,...

例子:groupBy("id", "name")--->group by id,name

升序: ORDER BY 字段,...ASC

例子:orderByAsc("id", "name")--->order by id ASC,name ASC

降序:ORDER BY 字段,...DESC

例子:orderByDesc("id", "name")--->order by id DESC,name DESC

排序:OREDR BY 字段,...

例子:orderBy(true, true, "id", "name")--->order by id ASC,name ASC

聚合函数作为条件HAVING (sql语句)

例子1:having("sum(age) > 10")--->having sum(age) > 10(有sql注入风险)

例子2:having("sum(age) > {0}", 11)--->having sum(age) > 11(推荐使用)

拼接 OR: or()

例子:eq("id",1).or().eq("name","老王")--->id = 1 or name = '老王'

注意事项:主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接)

OR 嵌套

例子:or(i -> i.eq("name", "李白").ne("status", "活着"))--->or (name = '李白' and status <> '活着')

AND 嵌套

例子:and(i -> i.eq("name", "李白").ne("status", "活着"))--->and (name = '李白' and status <> '活着')

nested:正常嵌套 不带AND 或者 OR

例子:nested(i -> i.eq("name", "李白").ne("status", "活着"))--->(name = '李白' and status <> '活着')

拼接 sql:apply

例子1:apply("id = 1")--->id = 1

例子2:apply("date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")

              --->date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")

例子3:apply("date_format(dateColumn,'%Y-%m-%d') = {0}", "2008-08-08")

             --->date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")

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

无视优化规则直接拼接到 sql 的最后:last

例子:last("limit 1")

注意事项:只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用

exists:拼接EXISTS(sql语句)

例子:exists("select id from table where age = 1")--->exists (select id from table where age = 1)

 

 

在学习过程中通过几个小例子(需求)验证了一下:

1.名字中包含雨并且年龄小于40   name like '%雨%' and age<40

 @Test
    public void selectByWrapper(){
        QueryWrapper queryWrapper = new QueryWrapper();
        //QueryWrapper query = Wrappers.query();
        queryWrapper.like("name","雨").lt("age",40);
        List userList = userMapper.selectList(queryWrapper);
        userList.forEach(System.out::println);
    }

结果展示:

SpringBoot整合MybatisPlus有关条件构造器的相关查询方法_第1张图片

2.名字中包含雨年并且龄大于等于20且小于等于40并且email不为空   name like '%雨%' and age between 20 and 40 and email is not null

@Test
    public void selectByWrapper6(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("name","王").or(qw->qw.lt("age",40).gt("age",20).isNotNull("email"));
        List userList = userMapper.selectList(queryWrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

结果展示:

SpringBoot整合MybatisPlus有关条件构造器的相关查询方法_第2张图片

3.名字为王姓或者年龄大于等于25,按照年龄降序排列,年龄相同按照id升序排列     name like '王%' or age>=25 order by age desc,id asc

  @Test
    public void selectByWrapper3(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.likeRight("name","王").or().ge("age",25).orderByDesc("age").orderByAsc("id");
        List userList = userMapper.selectList(queryWrapper);
        userList.forEach(System.out::println);
    }

结果展示:

SpringBoot整合MybatisPlus有关条件构造器的相关查询方法_第3张图片

4.创建日期为2019年2月14日并且直属上级为名字为王姓    date_format(create_time,'%Y-%m-%d')='2019-02-14' and manager_id in (select id from user where name like '王%')

 @Test
    public void selectByWrapper4(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}","2019-02-14")
                .inSql("manager_id","select id from mp_user where name like '王%'");
        List userList = userMapper.selectList(queryWrapper);
        userList.forEach(System.out::println);
    }

结果展示:

5.名字为王姓并且(年龄小于40或邮箱不为空)     name like '王%' and (age<40 or email is not null)

 @Test
    public void selectByWrapper5(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("name","王").and(wq->wq.lt("age",40).or().isNotNull("email"));
        List userList = userMapper.selectList(queryWrapper);
        userList.forEach(System.out::println);
    }

结果展示:

6.名字为王姓或者(年龄小于40并且年龄大于20并且邮箱不为空)     name like '王%' or (age<40 and age>20 and email is not null)

 @Test
    public void selectByWrapper6(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("name","王").or(qw->qw.lt("age",40).gt("age",20).isNotNull("email"));
        List userList = userMapper.selectList(queryWrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

结果展示:

SpringBoot整合MybatisPlus有关条件构造器的相关查询方法_第4张图片

7.(年龄小于40或邮箱不为空)并且名字为王姓            (age<40 or email is not null) and name like '王%'

@Test
    public void selectByWrapper7(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.nested(wq->wq.lt("age",40).or().isNotNull("email")).likeRight("name","王");
        List userList = userMapper.selectList(queryWrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

结果展示:

8.年龄为30、31、34、35            age in (30、31、34、35)

 @Test
    public void selectByWrapper8(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.in("age",Arrays.asList(30,31,34,35));
        //queryWrapper.in("age",30,31,34,35);
        List userList = userMapper.selectList(queryWrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

结果展示:

9、只返回满足条件的其中一条语句即可       limit 1

@Test
    public void selectByWrapper9(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.in("age",Arrays.asList(30,31,34,35)).last("limit 1");
        List userList = userMapper.selectList(queryWrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

结果展示:

以上仅为一些简单的需求,后续学习使用复杂的用法再进行记录整理。。。。。。。

你可能感兴趣的:(SpringBoot整合MybatisPlus有关条件构造器的相关查询方法)