• Mybatis-Plus 条件构造器


    目录

    1. MyBatis-Plus 条件构造器

    1.1 数据导入

    1.2 构造器简介

    1.3 构造器使用(1) 

    1.3.1 带条件的查询

    1.3.2 带条件的修改

    1.3.3 带条件的删除

    1.4 构造器使用(2)

    1.4.1 allEq 

    1.4.2 eq

    1.4.3 ne 

    1.4.4 gt 

    1.4.5 ge 

    1.4.6 lt 

    1.4.7 le 

    1.4.8 between 

    1.4.9 notBetween

    1.4.10 like

    1.4.11 notLike

    1.4.12 likeLeft

    1.4.13 likeRight 

    1.4.14 isNull

    1.4.15 isNotNull

    1.4.16 in 

    1.4.17  notIn

    1.4.18 inSql

    1.4.19 notInSql

    1.4.20 groupBy

    1.4.21 orderByAsc

    1.4.22 orderByDesc

    1.4.23 orderBy

    1.4.24 having

    1.4.25 func

    1.4.26 or 

    1.4.27 and 

    1.4.28 nested

    1.4.29 apply

    1.4.30 last

    1.4.31 exists

    1.4.32 notExists


    前言

      Mybatis-Plus是基于sping-boot项目 如果还没有配置的先去看下面这篇博客 

      Mybatis-Plus入门 

    1. MyBatis-Plus 条件构造器

      1.1 数据导入

    1. ## 使用库
    2. USE mp;
    3. ## 清空表
    4. TRUNCATE TABLE tbl_employee;
    5. ## 导入数据
    6. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan0','123@qq.com',0,21);
    7. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan1','123@qq.com',0,22);
    8. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan2','123@qq.com',0,23);
    9. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan3','123@qq.com',0,24);
    10. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan4','123@qq.com',0,25);
    11. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan5','123@qq.com',0,26);
    12. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan6','123@qq.com',0,27);
    13. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan7','123@qq.com',0,28);
    14. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan8','123@qq.com',0,29);
    15. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan9','123@qq.com',0,30);
    16. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby0','123@qq.com',1,21);
    17. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby1','123@qq.com',0,22);
    18. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby2','123@qq.com',1,23);
    19. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby3','123@qq.com',0,24);
    20. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby4','123@qq.com',1,25);
    21. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby5','123@qq.com',0,26);
    22. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby6','123@qq.com',1,27);
    23. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby7','123@qq.com',0,28);
    24. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby8','123@qq.com',1,29);
    25. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby9','123@qq.com',0,30);
    26. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom0','123@qq.com',1,21);
    27. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom1','123@qq.com',0,22);
    28. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom2','123@qq.com',1,23);
    29. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom3','123@qq.com',0,24);
    30. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom4','123@qq.com',1,25);
    31. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom5','123@qq.com',0,26);
    32. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom6','123@qq.com',1,27);
    33. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom7','123@qq.com',0,28);
    34. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom8','123@qq.com',1,29);
    35. INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom9','123@qq.com',0,30);
    36. ## 查询数据
    37. SELECT * FROM tbl_employee;

    1.2 构造器简介

    MyBatis-Plus 通过 EntityWrapper(简称 EW,MP 封装的一个查询条件构造器)或者 Condition(与 EW 类似) 来让用户自由的构建查询条件,简单便捷,没有额外的负担, 能够有效提高开发效率,它主要用于处理 sql 拼接,排序,实体参数查询等。 

    注意:使用的是数据库字段,不是 Java 属性! 

    警告:MyBatis-Plus不支持以及不赞成在 RPC 调用中把 Wrapper 进行传输,Wrapper 很重,传输 Wrapper 可以类比为你的 controller 用 map 接收值(开发一时爽,维护火葬场),正确的 RPC 调用姿势是写一个 DTO 进行传输,被调用方再根据 DTO 执行相应的操作。

    1.3 构造器使用(1) 

    1.3.1 带条件的查询

    需求描述:查询所有姓名的包含B、且姓名为女(1)、且年龄大于24岁的员工信息 

    1. @Test
    2. void testSelectList1() {
    3. QueryWrapper queryWrapper = new QueryWrapper<>();
    4. queryWrapper
    5. .like("last_name","B")
    6. .eq("gender",1)
    7. .gt("age",24);
    8. List employees = employeeMapper.selectList(queryWrapper);
    9. employees.forEach(System.out::println);
    10. }

     需求描述:查询所有员工信息

    1. @Test
    2. void testSelectList2() {
    3. List employees = employeeMapper.selectList(null);
    4. employees.forEach(System.out::println);
    5. }

     需求描述:查询所有女生的数量(1)

    1. @Test
    2. void testSelectList3() {
    3. QueryWrapper queryWrapper = new QueryWrapper<>();
    4. queryWrapper.eq("gender", 1);
    5. Integer count = employeeMapper.selectCount(queryWrapper);
    6. System.out.println(count);
    7. }

    1.3.2 带条件的修改

    需求信息:将年龄大于25岁的女生(1)的性别修改为男生(0) 

    1. @Test
    2. void testUpdate() {
    3. UpdateWrapper updateWrapper = new UpdateWrapper<>();
    4. updateWrapper
    5. .eq("gender", 1)
    6. .gt("age", 25)
    7. ;
    8. Employee employee = new Employee();
    9. employee.setGender(0);
    10. employeeMapper.update(employee, updateWrapper);
    11. }

     1.3.3 带条件的删除

    1. @Test
    2. void testDelete() {
    3. QueryWrapper queryWrapper = new QueryWrapper<>();
    4. queryWrapper.like("last_name", "Tom");
    5. int result = employeeMapper.delete(queryWrapper);
    6. System.out.println(result);
    7. }

    1.4 构造器使用(2)

    参数说明: 

    • 以下出现的第一个入参boolean condition表示该条件是否加入最后生成的sql中
    • 以下代码块内的多个方法均为从上往下补全个别boolean类型的入参,默认为true
    • 以下出现的泛型Param均为Wrapper的子类实例(均具有AbstractWrapper的所有方法)
    • 以下方法在入参中出现的为R泛型,在普通wrapper中是String,在LambdaWrapper中是函数(例:Entity::getId,Entity为实体类,getId为字段idgetMethod)
    • 以下方法入参中的R column均表示数据库字段,当R具体类型为String时则为数据库字段名(字段名是数据库关键字的自己用转义符包裹)!而不是实体类数据字段名,另当R具体类型为SFunction时项目runtime不支持eclipse自家的编译器
    • 以下举例均为使用普通wrapper,入参为MapList的均以json形式表现
    • 使用中如果入参的Map或者List为空,则不会加入最后生成的sql中

     AbstractWrapper

    说明:AbstractWrapper 是 QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类用于生成 sql 的 where 条件,entity 属性也用于生成 sql 的 where 条件,注意 entity 生成的 where 条件与使用各个 api 生成的 where 条件没有任何关联行为

    1.4.1 allEq 

    1. allEq(Map params)
    2. allEq(Map params, boolean null2IsNull)
    3. allEq(boolean condition, Map params, boolean null2IsNull)
    • 全部 eq (或个别 isNull

     个别参数说明:

    paramskey为数据库字段名,value为字段值
    null2IsNull:为true则在mapvaluenull时调用 isNull方法为false时则忽略valuenull

    •  例1: allEq({id:1,name:"老王",age:null})—>id = 1 and name = '老王' and age is null
    •  例2: allEq({id:1,name:"老王",age:null}, false)—>id = 1 and name = '老王
    1. allEq(BiPredicate filter, Map params)
    2. allEq(BiPredicate filter, Map params, boolean null2IsNull)
    3. allEq(boolean condition, BiPredicate filter, Map params, boolean null2IsNull)

    个别参数说明:

    filter : 过滤函数,是否允许字段传入比对条件中
    params 与 null2IsNull : 同上 

    • 例1: allEq((k,v) -> k.indexOf("a") >= 0, {id:1,name:"老王",age:null})—>name = '老王' and age is null
    • 例2: allEq((k,v) -> k.indexOf("a") >= 0, {id:1,name:"老王",age:null}, false)—>name = '老王'

     1.4.2 eq

    1. eq(R column, Object val)
    2. eq(boolean condition, R column, Object val)
    • 等于 =
    • 例: eq("name", "老王")—>name = '老王'

    1.4.3 ne 

    1. ne(R column, Object val)
    2. ne(boolean condition, R column, Object val)
    • 不等于 <>
    • 例: ne("name", "老王")—>name <> '老王'

    1.4.4 gt 

    1. gt(R column, Object val)
    2. gt(boolean condition, R column, Object val)
    • 大于 >
    • 例: gt("age", 18)—>age > 18

    1.4.5 ge 

    1. ge(R column, Object val)
    2. ge(boolean condition, R column, Object val)
    • 大于等于 >=
    • 例: ge("age", 18)—>age >= 18

    1.4.6 lt 

    1. lt(R column, Object val)
    2. lt(boolean condition, R column, Object val)
    • 小于 <
    • 例: lt("age", 18)—>age < 18

    1.4.7 le 

    1. le(R column, Object val)
    2. le(boolean condition, R column, Object val)
    • 小于等于 <=
    • 例: le("age", 18)—>age <= 18

    1.4.8 between 

    1. between(R column, Object val1, Object val2)
    2. between(boolean condition, R column, Object val1, Object val2)
    • BETWEEN 值1 AND 值2
    • 例: between("age", 18, 30)—>age between 18 and 30

    1.4.9 notBetween

    1. notBetween(R column, Object val1, Object val2)
    2. notBetween(boolean condition, R column, Object val1, Object val2)
    • NOT BETWEEN 值1 AND 值2
    • 例: notBetween("age", 18, 30)—>age not between 18 and 30

    1.4.10 like

    1. like(R column, Object val)
    2. like(boolean condition, R column, Object val)
    • LIKE ‘%值%’
    • 例: like("name", "王")—>name like '%王%'

    1.4.11 notLike

    1. notLike(R column, Object val)
    2. notLike(boolean condition, R column, Object val)
    • NOT LIKE ‘%值%’
    • 例: notLike("name", "王")—>name not like '%王%'

    1.4.12 likeLeft

    1. likeLeft(R column, Object val)
    2. likeLeft(boolean condition, R column, Object val)
    • LIKE ‘%值’
    • 例: likeLeft("name", "王")—>name like '%王'

    1.4.13 likeRight 

    1. likeRight(R column, Object val)
    2. likeRight(boolean condition, R column, Object val)
    • LIKE ‘值%’
    • 例: likeRight("name", "王")—>name like '王%'

    1.4.14 isNull

    1. isNull(R column)
    2. isNull(boolean condition, R column)
    • 字段 IS NULL
    • 例: isNull("name")—>name is null

    1.4.15 isNotNull

    1. isNotNull(R column)
    2. isNotNull(boolean condition, R column)
    • 字段 IS NOT NULL
    • 例: isNotNull("name")—>name is not null

    1.4.16 in 

    1. in(R column, Collection<?> value)
    2. in(boolean condition, R column, Collection<?> value)
    • 字段 IN (value.get(0), value.get(1), …)
    • 例: in("age",{1,2,3})—>age in (1,2,3)
    1. in(R column, Object... values)
    2. in(boolean condition, R column, Object... values)
    • 字段 IN (v0, v1, …)
    • 例: in("age", 1, 2, 3)—>age in (1,2,3)

    1.4.17  notIn

    1. notIn(R column, Collection<?> value)
    2. notIn(boolean condition, R column, Collection<?> value)
    • 字段 NOT IN (value.get(0), value.get(1), …)
    • 例: notIn("age",{1,2,3})—>age not in (1,2,3)

    1.4.18 inSql

    1. inSql(R column, String inValue)
    2. inSql(boolean condition, R column, String inValue)
    • 字段 IN ( sql语句 )
    • 例: inSql("age", "1,2,3,4,5,6")—>age in (1,2,3,4,5,6)
    • 例: inSql("id", "select id from table where id < 3")—>id in (select id from table where id < 3)

    1.4.19 notInSql

    1. notInSql(R column, String inValue)
    2. notInSql(boolean condition, R column, String inValue)
    • 字段 NOT IN ( sql语句 )
    • 例: notInSql("age", "1,2,3,4,5,6")—>age not in (1,2,3,4,5,6)
    • 例: notInSql("id", "select id from table where id < 3")—>id not in (select id from table where id < 3)

    1.4.20 groupBy

    1. groupBy(R... columns)
    2. groupBy(boolean condition, R... columns)
    • 分组:GROUP BY 字段, …
    • 例: groupBy("id", "name")—>group by id,name

    1.4.21 orderByAsc

    1. orderByAsc(R... columns)
    2. orderByAsc(boolean condition, R... columns)
    • 排序:ORDER BY 字段, … ASC
    • 例: orderByAsc("id", "name")—>order by id ASC,name ASC

    1.4.22 orderByDesc

    1. orderByDesc(R... columns)
    2. orderByDesc(boolean condition, R... columns)
    • 排序:ORDER BY 字段, … DESC
    • 例: orderByDesc("id", "name")—>order by id DESC,name DESC

    1.4.23 orderBy

    orderBy(boolean condition, boolean isAsc, R... columns)
    
    • 排序:ORDER BY 字段, …
    • 例: orderBy(true, true, "id", "name")—>order by id ASC,name ASC

    1.4.24 having

    1. having(String sqlHaving, Object... params)
    2. having(boolean condition, String sqlHaving, Object... params)
    • HAVING ( sql语句 )
    • 例: having("sum(age) > 10")—>having sum(age) > 10
    • 例: having("sum(age) > {0}", 11)—>having sum(age) > 11

    1.4.25 func

    1. func(Consumer<Children> consumer)
    2. func(boolean condition, Consumer<Children> consumer)
    • func 方法(主要方便在出现if…else下调用不同方法能不断链)
    • 例: func(i -> if(true) {i.eq("id", 1)} else {i.ne("id", 1)})

    1.4.26 or 

    1. or()
    2. or(boolean condition)
    • 拼接 OR

    注意事项:

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

    • 例: eq("id",1).or().eq("name","老王")—>id = 1 or name = '老王'
    1. or(Consumer<Param> consumer)
    2. or(boolean condition, Consumer<Param> consumer)
    • OR 嵌套
    • 例: or(i -> i.eq("name", "李白").ne("status", "活着"))—>or (name = '李白' and status <> '活着')

    1.4.27 and 

    1. and(Consumer<Param> consumer)
    2. and(boolean condition, Consumer<Param> consumer)
    • AND 嵌套
    • 例: and(i -> i.eq("name", "李白").ne("status", "活着"))—>and (name = '李白' and status <> '活着')

    1.4.28 nested

    1. nested(Consumer<Param> consumer)
    2. nested(boolean condition, Consumer<Param> consumer)
    • 正常嵌套 不带 AND 或者 OR
    • 例: nested(i -> i.eq("name", "李白").ne("status", "活着"))—>(name = '李白' and status <> '活着')

    1.4.29 apply

    1. apply(String applySql, Object... params)
    2. apply(boolean condition, String applySql, Object... params)
    • 拼接 sql

    注意事项:

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

    • 例: apply("id = 1")—>id = 1
    • 例: apply("date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")—>date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
    • 例: apply("date_format(dateColumn,'%Y-%m-%d') = {0}", "2008-08-08")—>date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")

    1.4.30 last

    1. last(String lastSql)
    2. last(boolean condition, String lastSql)
    • 无视优化规则直接拼接到 sql 的最后

    注意事项:

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

    • 例: last("limit 1")

    1.4.31 exists

    1. exists(String existsSql)
    2. exists(boolean condition, String existsSql)
    • 拼接 EXISTS ( sql语句 )
    • 例: exists("select id from table where age = 1")—>exists (select id from table where age = 1)

    1.4.32 notExists

    1. notExists(String notExistsSql)
    2. notExists(boolean condition, String notExistsSql)
    • 拼接 NOT EXISTS ( sql语句 )
    • 例: notExists("select id from table where age = 1")—>not exists (select id from table where age = 1)

  • 相关阅读:
    opencv跨平台arm交叉编译之ubuntu
    八股文(Web篇——网络通讯部分)第十二天
    jstack(查看线程)、jmap(查看内存)和jstat(性能分析)
    安卓恶意应用识别(二)(安卓APK反编译)
    翻译文本的软件有哪些?这几个翻译工具你可以试试看
    【java】Java中的异步实现方式
    Java多线程同步工具类:Semaphore原理剖析
    基于NPN公专网互通的智慧交通专网方案和关键技术
    Linux入门之管理 user 和 group
    2023-9-10 集合-Nim游戏
  • 原文地址:https://blog.csdn.net/m0_63300795/article/details/126953915