条件构造器
使用MongoPlus快速构建条件
说明:
- 以下出现的第一个入参
boolean condition
表示该条件是否加入最后生成的sql中,例如:query.like(StringUtils.isNotBlank(name), Entity::getName, name) .eq(age!=null && age >= 0, Entity::getAge, age) - 以下代码块内的多个方法均为从上往下补全个别
boolean
类型的入参,默认为true
警告:
不支持以及不赞成在 RPC 调用中把 QueryChainWrapper 进行传输
- QueryChainWrapper 很重
- 传输 QueryChainWrapper 可以类比为你的 controller 用 map 接收值(开发一时爽,维护火葬场)
- 正确的 RPC 调用姿势是写一个 DTO 进行传输,被调用方再根据 DTO 执行相应的操作
custom
java
custom(BasicDBObject basicDBObject);
custom(BSON bson);
- 自定义
- 例:new BasicDBObject("$eq",new BasicDBObject("_id","1"))
combine
java
combine(boolean condition,SFunction<QueryChainWrapper<T,?>,QueryChainWrapper<T,?>> function)
combine(SFunction<QueryChainWrapper<T,?>,QueryChainWrapper<T,?>> function)
combine(QueryChainWrapper<?,?> queryChainWrapper)
combine(boolean condition,QueryChainWrapper<?,?> queryChainWrapper)
- combine中的条件,将会存在同一个对象中,常用于or,and等逻辑操作符中
如构建or条件:
javaor(wrapper -> wrapper.eq(User::getUserName,"张三").like(User::getUserName,"1"))
该操作将会构建语句为:
json{or:[{userName:{"eq":"张三"}},{userName:{"like":"1"}}]}
如果使用
combine
方法构建:javaor(wrapper -> wrapper.combine(combineWrapper -> combineWrapper.eq(User::getUserName,"张三").like(User::getUserName,"1") ) )
则对应语句为:
json{or:[{userName:{"eq":"张三","like":"1"}}]}
注意:
- 在使用
combine
方法时,请保证combine
中条件的字段名一致
- 在使用
eq
java
eq(SFunction<T,Object> column, Object val)
eq(boolean condition, SFunction<T,Object> column, Object val)
- 等于 =
- 例:
eq("name", "老王")
--->name = '老王'
ne
java
ne(SFunction<T,Object> column, Object val)
ne(boolean condition, SFunction<T,Object> column, Object val)
- 不等于 <>
- 例:
ne("name", "老王")
--->name <> '老王'
gt
java
gt(SFunction<T,Object> column, Object val)
gt(boolean condition, SFunction<T,Object> column, Object val)
- 大于 >
- 例:
gt("age", 18)
--->age > 18
gte
java
gte(SFunction<T,Object> column, Object val)
gte(boolean condition, SFunction<T,Object> column, Object val)
- 大于等于 >=
- 例:
gte("age", 18)
--->age >= 18
lt
java
lt(SFunction<T,Object> column, Object val)
lt(boolean condition, SFunction<T,Object> column, Object val)
- 小于 <
- 例:
lt("age", 18)
--->age < 18
lte
java
lte(SFunction<T,Object> column, Object val)
lte(boolean condition, SFunction<T,Object> column, Object val)
- 小于等于 <=
- 例:
lte("age", 18)
--->age <= 18
like
java
like(SFunction<T,Object> column, Object val)
like(boolean condition, SFunction<T,Object> column, Object val)
- LIKE '%值%'
- 例:
like("name", "王")
likeLeft
java
likeLeft(SFunction<T,Object> column, Object val)
likeLeft(boolean condition, SFunction<T,Object> column, Object val)
- LIKE '%值'
- 例:
like("name", "^王")
likeRight
java
likeRight(SFunction<T,Object> column, Object val)
likeRight(boolean condition, SFunction<T,Object> column, Object val)
- LIKE '%值'
- 例:
like("name", "王$")
notLike
java
notLike(SFunction<T,Object> column, Object val)
notLike(boolean condition, SFunction<T,Object> column, Object val)
NOT LIKE '%值%'
例:
notLike("name", "王")
NOT LIKE '值%'
例:
notLikeRight("name", "王")
in
java
in(SFunction<T,Object> column, Collection<?> value)
in(boolean condition, SFunction<T,Object> column, Collection<?> value)
- 字段 IN (value.get(0), value.get(1), ...)
- 例:
in("age",{1,2,3})
--->age in (1,2,3)
java
in(SFunction<T,Object> column, Object... values)
in(boolean condition, SFunction<T,Object> column, Object... values)
- 字段 IN (v0, v1, ...)
- 例:
in("age", 1, 2, 3)
--->age in (1,2,3)
nin
java
nin(SFunction<T,Object> column , Collection<Object> valueList)
nin(boolean condition , String column , Collection<Object> valueList)
- 字段 nin (value.get(0), value.get(1), ...)
- 例:
notIn("age",{1,2,3})
--->age not in (1,2,3)
and
java
and(LambdaQueryChainWrapper<T> lambdaQueryChainWrapper)
and(boolean condition,LambdaQueryChainWrapper<T> lambdaQueryChainWrapper)
- 并且 在or中使用
orderByAsc
java
orderByAsc(SFunction<T,Object> columns)
orderByAsc(boolean condition, SFunction<T,Object> columns)
- 排序:ORDER BY 字段, ASC
- 例:
orderByAsc("id", "name")
orderByDesc
java
orderByDesc(SFunction<T,Object> columns)
orderByDesc(boolean condition, SFunction<T,Object>... columns)
- 排序:ORDER BY 字段, DESC
- 例:
orderByDesc("id", "name")
or
java
or(SFunction<T,Object> column,Object value)
or(boolean condition,String column,Object value)
or(QueryChainWrapper<?,?> queryChainWrapper)
or(boolean condition,QueryChainWrapper<?,?> queryChainWrapper)
- 或者
nor
java
nor(boolean condition , QueryChainWrapper<?,?> queryChainWrapper)
nor(QueryChainWrapper<?,?> queryChainWrapper)
nor(boolean condition , SFunction<T,Object> column,Object value)
nor(SFunction<T,Object> column,Object value)
nor(boolean condition,String column,Object value)
nor(String column , Object value)
- 查询的文档必须不符合所有条件
type
java
type(SFunction<T,Object> column, TypeEnum value)
type(String column, TypeEnum value)
type(SFunction<T,Object> column, String value)
type(String column, String value)
type(SFunction<T,Object> column, Integer value)
type(String column, Integer value)
- 指定查询的字段类型
exists
java
exists(SFunction<T,Object> column,Boolean value)
exists(boolean condition,SFunction<T,Object> column,Boolean value)
- 字段是否存在
not
java
not(CompareCondition compareCondition)
not(boolean condition,CompareCondition compareCondition)
expr
java
expr(CompareCondition compareCondition)
expr(boolean condition,CompareCondition compareCondition)
- 运行计算的表达式
mod
java
mod(boolean condition,SFunction<T,Object> column,long divide,long remain)
mod(SFunction<T,Object> column,long divide,long remain)
mod(boolean condition,SFunction<T,Object> column,Collection<Long> value)
mod(SFunction<T,Object> column,Collection<Long> value)
mod(boolean condition,String column , long divide,long remain)
mod(String column , long divide,long remain)
mod(boolean condition,String column,Collection<Long> value)
mod(String column,Collection<Long> value)
- 字段值符合余数
elemMatch
java
elemMatch(boolean condition,SFunction<T,Object> column , QueryChainWrapper<?,?> queryChainWrapper)
elemMatch(SFunction<T,Object> column , QueryChainWrapper<?,?> queryChainWrapper)
elemMatch(boolean condition,String column , QueryChainWrapper<?,?> queryChainWrapper)
elemMatch(String column , QueryChainWrapper<?,?> queryChainWrapper)
- 匹配数组中的值
between
java
Children between(boolean condition , SFunction<T,Object> column,Object gte,Object lte,boolean convertGtOrLt);
Children between(SFunction<T,Object> column,Object gte,Object lte,boolean convertGtOrLt);
Children between(boolean condition,String column,Object gte,Object lte,boolean convertGtOrLt);
Children between(String column,Object gte,Object lte,boolean convertGtOrLt);
- 在...之间
- 布尔类型的参数convertGtOrLt为是否转换为大写和小写,因为需要的两个值,默认取的是大于等于XXX,小于等于XXX
all
java
all(boolean condition,SFunction<T,Object> column,Collection<Object> value)
all(SFunction<T,Object> column,Collection<Object> value)
all(boolean condition,String column,Collection<Object> value)
all(String column,Collection<Object> value)
- 匹配数组中的值 必须同时包含指定的多个元素
regex
java
regex(boolean condition, SFunction<T,Object> column, Object value)
regex(SFunction<T,Object> column,Object value)
regex(boolean condition,String column,Object value)
regex(String column,Object value)
- 正则表达式查询
text
java
text(boolean condition, Object value)
text(Object value)
text(boolean condition, Object value,TextSearchOptions textSearchOptions)
text(Object value,TextSearchOptions textSearchOptions)
- 文本查询
size
java
size(String fieldName, int size);
- 匹配所有字段值为指定大小的数组的文档
bitsAllClear
java
bitsAllClear(SFunction<T,?> fieldName, long bitmask);
- 匹配字段中所有位位置均清晰的所有文档
bitsAllSet
java
bitsAllSet(SFunction<T,?> fieldName, long bitmask);
- 匹配所有位位置均在字段中设置的所有文档
bitsAnyClear
java
bitsAnyClear(SFunction<T,?> fieldName, long bitmask);
- 匹配字段中任何位位置清晰的所有文档
bitsAnySet
java
bitsAnySet(SFunction<T,?> fieldName, long bitmask);
- 匹配在字段中设置任何位位置的所有文档
QueryWrapper
说明:
继承自 AbstractWrapper ,自身的内部属性 entity 也用于生成 where 条件
及 LambdaQueryWrapper, 可以通过 new QueryWrapper().lambda() 方法获取
project
java
project(Projection... projection)
project(List<Projection> projectionList)
select(boolean displayId, Projection... projection)
- 设置查询字段
说明:
以上方法分为两类.
第二类方法为:过滤查询字段(主键除外),入参不包含 class 的调用前需要wrapper
内的entity
属性有值! 这两类方法重复调用以最后一次为准 - 例:
select("id", "name", "age")
- 例:
select(i -> i.getProperty().startsWith("test"))
UpdateWrapper
说明:
继承自 AbstractWrapper
,自身的内部属性 entity
也用于生成 where 条件
及 LambdaUpdateWrapper
, 可以通过 new UpdateWrapper().lambda()
方法获取!
set
java
set(String column, Object val)
set(boolean condition, String column, Object val)
- SQL SET 字段
- 例:
set("name", "老李头")
- 例:
set("name", "")
--->数据库字段值变为空字符串 - 例:
set("name", null)
--->数据库字段值变为null
setOnInsert
java
setOnInsert(boolean condition, SFunction<T,Object> column, Object value);
setOnInsert(SFunction<T,Object> column, Object value);
setOnInsert(boolean condition, String column, Object value);
setOnInsert(String column, Object value);
- 将指定值分配给文档中的字段
- 例:
setOnInsert(User::getUserName,"张三")
push
java
push(boolean condition,SFunction<T,Object> column,Object value);
push(SFunction<T,Object> column,Object value);
push(boolean condition,String column,Object value);
push(String column,Object value);
push(boolean condition,SFunction<T,Object> column,Object ... value);
push(SFunction<T,Object> column,Object ... value);
push(boolean condition,String column,Object ... value);
push(String column,Object ... value);
push(boolean condition, SFunction<T,Object> column, List<?> value);
push(SFunction<T,Object> column, List<?> value);
push(boolean condition, String column, List<?> value);
push(String column, List<?> value);
- 将指定值push到数组中
- 例:
push(User::getRoleList,new ArrayList<>())
inc
java
inc(boolean condition,SFunction<T,Object> column,Number value);
inc(SFunction<T,Object> column,Number value);
inc(boolean condition,String column,Number value);
inc(String column,Number value);
- 对指定值原子性的递增
- 例:
inc(User::getAge,1)
currentDate
java
currentDate(boolean condition,SFunction<T,Object> column);
currentDate(SFunction<T,Object> column);
currentDate(boolean condition,String column);
currentDate(String column);
currentDate(boolean condition,SFunction<T,Object> column, CurrentDateType currentDateType);
currentDate(SFunction<T,Object> column, CurrentDateType currentDateType);
currentDate(boolean condition,String column, CurrentDateType currentDateType);
currentDate(String column, CurrentDateType currentDateType);
- 将字段的值设置为当前日期或当前时间戳
- 例:
currentDate(User::getCreateTime,CurrentDateType.DATE)
min
java
min(boolean condition,SFunction<T,Object> column, Object value);
min(SFunction<T,Object> column, Object value);
min(boolean condition,String column, Object value);
min(String column, Object value);
- 指定字段值小于输入值则更新
- 例:
min(User::getSalt,100)
- 例:
min(User::getCreateTime,LocalDateTime.now())
max
java
max(boolean condition,SFunction<T,Object> column, Object value);
max(SFunction<T,Object> column, Object value);
max(boolean condition,String column, Object value);
min(String column, Object value);
- 指定字段值大于输入值则更新
- 例:
max(User::getSalt,100)
- 例:
max(User::getCreateTime,LocalDateTime.now())
mul
java
mul(boolean condition,SFunction<T,Object> column,Number value);
mul(SFunction<T,Object> column,Number value);
mul(boolean condition,String column,Number value);
mul(String column,Number value);
- 将指定字段的值乘以value
- 例:
mul(User::getAge,21)
rename
java
rename(boolean condition, String oldFieldName,String newFieldName);
rename(String oldFieldName,String newFieldName);
rename(boolean condition, SFunction<O,Object> oldFieldName,String newFieldName);
rename(SFunction<O,Object> oldFieldName,String newFieldName);
rename(boolean condition, SFunction<O,Object> oldFieldName,SFunction<N,Object> newFieldName);
rename(SFunction<O,Object> oldFieldName,SFunction<N,Object> newFieldName);
- 更新字段名称
- 例:
rename(User::getUserName,"user_name")
- 例:
rename(User::getUserName,User::getUserName)
unset
java
unset(SFunction<T,Object>... columns);
unset(boolean condition,SFunction<T,Object>... columns);
unset(String... columns);
unset(boolean condition,String... columns);
unset(List<String> columns);
unset(boolean condition,List<String> columns);
- 删除特定字段
- 例:
unset(User::getUserName,User::getAge)
addToSet
java
addToSet(boolean condition,SFunction<T,Object> column,Object value,boolean each);
addToSet(SFunction<T,Object> column,Object value,boolean each);
addToSet(boolean condition,String column,Object value,boolean each);
addToSet(String column,Object value,boolean each);
- 将值添加到数组中
- 例:
addToSet(User::getRoleList,new ArrayList<>(),true)
pop
java
pop(boolean condition,SFunction<T,Object> column, PopType popType);
pop(SFunction<T,Object> column, PopType popType);
pop(boolean condition,String column, PopType popType);
pop(String column, PopType popType);
- 删除数组中第一个或最后一个元素
- 例:
pop(User::getRoleList,PopType.FIRST)
pull
java
pull(boolean condition,SFunction<T,Object> column,Object value);
pull(SFunction<T,Object> column,Object value);
pull(boolean condition,QueryChainWrapper<?,?> wrapper);
pull(boolean condition,SFunction<QueryChainWrapper<?,?>,QueryChainWrapper<?,?>> function);
pull(QueryChainWrapper<?,?> wrapper);
pull(SFunction<QueryChainWrapper<?,?>,QueryChainWrapper<?,?>> function);
pull(boolean condition,String column,Object value);
pull(String column,Object value);
- 删除数组中符合条件或符合指定值的实例
- 例:
pull(new QueryWrapper<User>().eq(User::getUserName,"张三"))
pullAll
java
pullAll(boolean condition,SFunction<T,Object> column, Collection<?> values);
pullAll(SFunction<T,Object> column, Collection<?> values);
pullAll(boolean condition,SFunction<T,Object> column, Object... values);
pullAll(SFunction<T,Object> column, Object... values);
pullAll(boolean condition,String column, Collection<?> values);
pullAll(String column, Collection<?> values);
pullAll(boolean condition,String column, Object... values);
pullAll(String column, Object... values);
pullAll(boolean condition,MutablePair<String, Collection<?>>... pullAllPair);
pullAll(MutablePair<String, Collection<?>>... pullAllPair);
- 删除数组中符合条件或符合指定值的实例
- 例:
pullAll(User::getId, Arrays.asList(1,2,3))
- 例:
pullAll(MutablePair.of(User::getId, Arrays.asList(1,2,3)))
lambda
- 获取
LambdaWrapper
在QueryWrapper
中是获取LambdaQueryWrapper
在UpdateWrapper
中是获取LambdaUpdateWrapper
链式调用 lambda 式
java
// 区分:
// 链式调用 普通
UpdateChainWrapper<T> update();
// 链式调用 lambda 式。注意:不支持 Kotlin
LambdaUpdateChainWrapper<T> lambdaUpdate();
// 等价示例:
query().eq("id", value).one();
lambdaQuery().eq(Entity::getId, value).one();
// 等价示例:
update().eq("id", value).remove();
lambdaUpdate().eq(Entity::getId, value).remove();