代码改变世界

MybatisPlus Wrapper方法

2021-11-04 16:45  sunice  阅读(929)  评论(0编辑  收藏  举报

详细见官方文档:条件构造器 | MyBatis-Plus

拼接条件(where)的部分:

1.allEq
全部相等或者部分为空

allEq(键值的Map) //默认使用isNull方法
allEq(键值的Map, 是否使用isNull)

键值的Map: key为数据库字段名,value为字段值
是否使用isNull: 为true则在map的value为null时调用isNull方法,为false时则忽略value为null的,即只匹配不是null的部分

2.eq
匹配与键值相等的数据

eq(键,值)

3.ne
匹配与键值不相等的数据

ne(键,值)

4.gt
匹配大于键值的数据

gt(键,值)

5.ge
匹配大于等于键值的数据

ge(键,值)

6.lt
匹配小于键值的数据

lt(键,值)

7.le
匹配小于等于键值的数据

le(键,值)

8.between
匹配区间内的数据 BETWEEN 值1 AND 值2

between(键,值1,值2)

9.notBetween
匹配区间外的数据 NOT BETWEEN 值1 AND 值2

notBetween(键,值1,值2)

10.like
模糊查询 前后模糊 %值%

like(键,值)

11.notLike
排除值的模糊查询

notLike(键,值)

12.likeLeft
模糊查询 前模糊 %值

likeLeft(键,值)

13.likeRight
模糊查询 后模糊 值%

likeRight(键,值)

14.isNull
匹配键值为空的数据

isNull(键,值)

15.isNotNull
匹配键值不为空的数据

isNotNull(键,值)

16.in
根据匹配的键值批量查询

in(键,值的数组)
in(键,值1,值2,...)

17.notIn
根据不匹配的键值批量查询

notIn(键,值的数组)
notIn(键,值1,值2,...)

18.inSql
子查询

inSql(键,值)

例:

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)

19.notInSql
子查询 与inSql相反

notInSql(键,值)

例:

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)

20.groupBy
分组查询

groupBy(键1,键2,...)

21.orderByAsc
根据键值升序排列

orderByAsc(键1,键2,...)

22.orderByDesc
根据键值降序排列

orderByDesc(键1,键2,...)

23.having
在由GROUP BY子句创建的分组上设置条件

having(sql语句)
having(sql语句,条件1,条件2...)

例:

having("sum(age) > 10")--->having sum(age) > 10
having("sum(age) > {0}", 11)--->having sum(age) > 11

24.or
有两种用法:
第一种表示连接方法采用or的形式,默认是and

or()

例:

eq().or().eq()

第二种表示or嵌套:
例:

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

25.and
and嵌套
例:

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

26.nested
正常嵌套不带AND或者OR

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

27.apply
拼接sql

apply(sql语句,参数1,参数2)

例:

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

28.last
无视优化规则直接拼接到 sql 的最后
只能调用一次,多次调用以最后一次为准

last(sql语句)

29.exists
拼接 EXISTS ( sql语句 )

exists(sql语句)

30.notExists
拼接 NOT EXISTS ( sql语句 )

notExists(sql语句)

拼接查询(select)的部分:

select

select(查询的键1,查询的键2...)

例:

select("id", "name", "age")

拼接更新(update)的部分:
1.set

set(键,值)

2.setSql

setSql(sql语句)

转载:
https://allyixi.blog.csdn.net/article/details/106603783?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link