mybatis plus 条件构造器
阅读数:272 评论数:0
跳转到新版页面分类
python/Java
正文
一、AbstractWrapper
用于生成sql的where条件,当第一个入参数为boolean condition时,表示该条件是否加入最后成的sql中。
1、allEq
allEq(Map<R, V> params)
allEq(Map<R, V> params, boolean null2IsNull)
allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
(1)params
key为数据库字段名,value为字段值。
(2)null2IsNull
为true,则在map的value为null时调用isNull方法,false时则忽略value为null的。
allEq(BiPredicate<R, V> filter, Map<R, V> params)
allEq(BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull)
allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull)
(1)filter
过滤函数,是否允许字段传入对比条件中。
2、eq
eq(R column, Object val)
eq(boolean condition, R column, Object val)
3、ne
ne(R column, Object val)
ne(boolean condition, R column, Object val)
4、gt
gt(R column, Object val)
gt(boolean condition, R column, Object val)
5、ge
ge(R column, Object val)
ge(boolean condition, R column, Object val)
6、lt
lt(R column, Object val)
lt(boolean condition, R column, Object val)
7、le
le(R column, Object val)
le(boolean condition, R column, Object val)
8、between
between(R column, Object val1, Object val2)
between(boolean condition, R column, Object val1, Object val2)
9、notBetween
notBetween(R column, Object val1, Object val2)
notBetween(boolean condition, R column, Object val1, Object val2)
10、like
like(R column, Object val)
like(boolean condition, R column, Object val)
11、notLike
notLike(R column, Object val)
notLike(boolean condition, R column, Object val)
12、likeLeft
likeLeft(R column, Object val)
likeLeft(boolean condition, R column, Object val)
13、likeRight
likeRight(R column, Object val)
likeRight(boolean condition, R column, Object val)
14、isNull
isNull(R column)
isNull(boolean condition, R column)
15、isNotNull
isNotNull(R column)
isNotNull(boolean condition, R column)
16、in
in(R column, Collection<?> value)
in(boolean condition, R column, Collection<?> value)
in(R column, Object... values)
in(boolean condition, R column, Object... values)
17、notIn
notIn(R column, Collection<?> value)
notIn(boolean condition, R column, Collection<?> value)
notIn(R column, Object... values)
notIn(boolean condition, R column, Object... values)
18、inSql
inSql(R column, String inValue)
inSql(boolean condition, R column, String inValue)
//示例
inSql("id", "select id from table where id < 3")--->id in (select id from table where id < 3)
19、notInSql
notInSql(R column, String inValue)
notInSql(boolean condition, R column, String inValue)
20、groupBy
groupBy(R... columns)
groupBy(boolean condition, R... columns)
21、orderByAsc
orderByAsc(R... columns)
orderByAsc(boolean condition, R... columns)
22、orderByDesc
orderByDesc(R... columns)
orderByDesc(boolean condition, R... columns)
23、orderBy
orderBy(boolean condition, boolean isAsc, R... columns)
24、having
having(String sqlHaving, Object... params)
having(boolean condition, String sqlHaving, Object... params)
//示例
having("sum(age) > {0}", 11)--->having sum(age) > 11
25、func
func(Consumer<Children> consumer)
func(boolean condition, Consumer<Children> consumer)
//示例
func(i -> if(true) {i.eq("id", 1)} else {i.ne("id", 1)})
26、or
or()
or(boolean condition)
or(Consumer<Param> consumer)
or(boolean condition, Consumer<Param> consumer)
//示例
or(i -> i.eq("name", "李白").ne("status", "活着"))--->or (name = '李白' and status <> '活着')
27、and
and(Consumer<Param> consumer)
and(boolean condition, Consumer<Param> consumer)
// 示例
and(i -> i.eq("name", "李白").ne("status", "活着"))--->and (name = '李白' and status <> '活着')
28、nested
nested(Consumer<Param> consumer)
nested(boolean condition, Consumer<Param> consumer)
// 示例,生成代码中不带and或or
nested(i -> i.eq("name", "李白").ne("status", "活着"))--->(name = '李白' and status <> '活着')
29、apply
apply(String applySql, Object... params)
apply(boolean condition, String applySql, Object... params)
//动态入参数,可以防止sql注入
apply("date_format(dateColumn,'%Y-%m-%d') = {0}", "2008-08-08")--->date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
30、last
last(String lastSql)
last(boolean condition, String lastSql)
// 使用last添加limit条件
last("limit 3")
无视优化规则,直接拼接到sql最后,只能调用一次,多次调用 以最后一次为准。
31、exist
exists(String existsSql)
exists(boolean condition, String existsSql)
32、notExist
notExists(String notExistsSql)
notExists(boolean condition, String notExistsSql)
二、QueryWrapper
继承自AbstractWrapper,自身内部属性entity可用于生成where条件。
(1)select
select(String... sqlSelect)
select(Predicate<TableFieldInfo> predicate)
select(Class<T> entityClass, Predicate<TableFieldInfo> predicate)
// 示例
select("id", "name", "age")
select(i -> i.getProperty().startsWith("test"))
三、UpdateWrapper
继承自AbstractWrapper,自身的内部属性entity也用于生成where条件。
1、set
set(String column, Object val)
set(boolean condition, String column, Object val)
2、setSql
setSql(String sql)
// 示例
setSql("name = '老李头'")
四、lambda
在QueryWrapper中是获取LambdaQueryWrapper
在UpdateWrapper中获取LambdaUpdateWrapper
五、自定义SQL
需要版本不低于3.0.7,param参数名要么叫ew,要么加上注解@Param(Constants.WRAPPER)使用${ew.customSqlSegment}不支持Wrapper内的entity生成where语句。
1、用注解
@Select("select * from mysql_data ${ew.customSqlSegment}")
List<MysqlData> getAll(@Param(Constants.WRAPPER) Wrapper wrapper);
2、用XML
List<MysqlData> getAll(Wrapper ew);
<select id="getAll" resultType="MysqlData">
SELECT * FROM mysql_data ${ew.customSqlSegment}
</select>