Mybatis-plus中Wrapper的用法

Mybatis-plus中Wrapper的用法

一、简介

上一节中,我们提到CRUD操作时,可以通过传入UpdateWrapper或QueryWrapper作为更新或查询的条件,从而获取到相应的操作结果,这一节,我们就来介绍两种Wrapper的具体使用方法;

QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where 条件

二、AbstractWrapper

参数的统一说明:

  • boolean condition:表示该条件是否加入最后生成的sql中。例如:query.eq(StringUtils.isNotBlank(name), Entity::getName, name) ,当name为空时,这个eq不会生成到where条件中;
  • null2IsNull:当参数中Map的value值为空时,是否参添加到where条件:例如:默认情况:allEq({id:1,name:”老王”,age:null})–>id = 1 and name = ‘老王’ and age is nullnull2IsNull == false时:allEq({id:1,name:”老王”,age:null}, false)—>id = 1 and name = ‘老王’

用于生成where条件:

条件函数说明
allEqallEq(Map<R, V> params)
allEq(Map<R, V> params, boolean null2IsNull)
allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
全部 =,map中的参数,生成where时,and相连;
eqeq(R column, Object val)
eq(boolean condition, R column, Object val)
等于 =
nene(R column, Object val)
ne(boolean condition, R column, Object val)
不等于 <>
gtgt(R column, Object val)
gt(boolean condition, R column, Object val)
大于 >
gege(R column, Object val)
ge(boolean condition, R column, Object val)
大于等于 >=
ltlt(R column, Object val)
lt(boolean condition, R column, Object val)
小于 <
lele(R column, Object val)
le(boolean condition, R column, Object val)
小于等于 <=
betweenbetween(R column, Object val1, Object val2)
between(boolean condition, R column, Object val1, Object val2)
BETWEEN 值1 AND 值2
notBetweennotBetween(R column, Object val1, Object val2)
notBetween(boolean condition, R column, Object val1, Object val2)
NOT BETWEEN 值1 AND 值2
likelike(R column, Object val)
like(boolean condition, R column, Object val)
LIKE ‘%值%’
notLikenotLike(R column, Object val)
notLike(boolean condition, R column, Object val)
notLike
likeLeftlikeLeft(R column, Object val) likeLeft(boolean condition, R column, Object val)LIKE ‘%值’
likeRightlikeRight(R column, Object val) likeRight(boolean condition, R column, Object val)LIKE ‘值%’
isNullisNull(R column)
isNull(boolean condition, R column)
字段 IS NULL
isNotNullisNotNull(R column)
isNotNull(boolean condition, R column)
字段 IS NOT NULL
inin(R column, Collection<?> value)
in(boolean condition, R column,Collection< ? >value
in(R column, Object… values)
in(boolean condition, R column, Object… values)
字段 IN (value.get(0), value.get(1), …)
in(“age”,{1,2,3})--->age in (1,2,3)
in(“age”, 1, 2, 3)--->age in (1,2,3)
notInnotIn(R column, Collection value) notIn(boolean condition, R column, Collection value)
notIn(R column, Object… values)
notIn(boolean condition, R column, Object… values)
字段 NOT IN (value.get(0), value.get(1), …)
inSqlinSql(R column, String inValue)
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)
notInSqlnotInSql(R column, String inValue)
notInSql(boolean condition, R column, String inValue)
字段 NOT IN ( sql语句 )
groupBygroupBy(R… columns)
groupBy(boolean condition, R… columns)
分组:GROUP BY
orderByAscorderByAsc(R… columns)
orderByAsc(boolean condition, R… columns)
ORDER BY 字段, … ASC
orderByDescorderByDesc(R… columns)
orderByDesc(boolean condition, R… columns)
排序:ORDER BY 字段, … DESC
orderByorderBy(boolean condition, boolean isAsc, R… columns)排序:ORDER BY 字段
havinghaving(String sqlHaving, Object… params)
having(boolean condition, String sqlHaving, Object… params)
HAVING ( sql语句 )
having(“sum(age) > 10”)->having sum(age) > 10
funcfunc(Consumer consumer)
func(boolean condition, Consumer consumer)
func 方法(主要方便在出现if…else下调用不同方法能不断链) 例: func(i -> if(true) {i.eq("id", 1)} else {i.ne("id", 1)})
oror() or(boolean condition)or(Consumer consumer)
or(boolean condition, Consumer consumer)
拼接 OR
eq(“id”,1).or().eq(“name”,”老王”)->id = 1 or name = ‘老王’
andand(Consumer consumer)
and(boolean condition, Consumer consumer)
AND 嵌套
and(i -> i.eq(“name”, “李白”).ne(“status”, “活着”))--->and (name = ‘李白’ and status <> ‘活着’)
nestednested(Consumer consumer)
nested(boolean condition, Consumer consumer)
正常嵌套 不带 AND 或者 OR
nested(i -> i.eq(“name”, “李白”).ne(“status”, “活着”))--->(name = ‘李白’ and status <> ‘活着’)
applyapply(String applySql, Object… params)
apply(boolean condition, String applySql, Object… params)
拼接 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′”)
lastlast(String lastSql)
last(boolean condition, String lastSql)
无视优化规则直接拼接到 sql 的最后
只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用
last(“limit 1”)
existsexists(String existsSql)
exists(boolean condition, String existsSql)
拼接 EXISTS
例: exists(“select id from table where age = 1”)—>exists (select id from table where age = 1)
notExistsnotExists(String notExistsSql)
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)

三、QueryWrapper

Select设置查询字段

select(String... sqlSelect)
select(Predicate<TableFieldInfo> predicate)
select(Class<T> entityClass, Predicate<TableFieldInfo> predicate)
复制代码

第二种和第三种用法可以针对字段多的情况,用于排除字段的方式,例:

queryWrapper.select(info -> info.getProperty().startsWith("test"));
复制代码
queryWrapper.select(User.class, info->!info.getColumn()
            .equals("email") && !info.getColumn().equals("create_time"));
复制代码

四、UpdateWrapper

  1. 设置SET 字段
set(String column, Object val)
set(boolean condition, String column, Object val)
复制代码
  • 例: set("name", "老李头")
  • 例: set("name", "")—>数据库字段值变为空字符串
  • 例: set("name", null)—>数据库字段值变为null

2.设置SET的SQL

setSql(String sql)
复制代码

例:setSql(“name = ‘老李头'”)

原创文章,作者:睿达君,如若转载,请注明出处:https://zrrd.net.cn/1901.html

发表评论

登录后才能评论
咨询电话
联系电话:0451-81320577

地址:哈尔滨市松北区中小企业总部基地13F

微信咨询
微信咨询
QQ咨询
分享本页
返回顶部