mybatisplus條件構(gòu)造器的方法使用
mybatisplus條件構(gòu)造器
allEq
allEq(Map<R, V> params) allEq(Map<R, V> params, boolean null2IsNull) allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
個別參數(shù)說明:
params : key為數(shù)據(jù)庫字段名,value為字段值null2IsNull : 為true則在map的value為null時調(diào)用 isNull 方法,為false時則忽略value為null的
- 例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 = '老王'
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)
個別參數(shù)說明:
filter : 過濾函數(shù),是否允許字段傳入比對條件中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 = '老王'
eq
eq(R column, Object val) eq(boolean condition, R column, Object val)
- 等于 =
- 例:
eq("name", "老王")—>name = '老王'
ne
ne(R column, Object val) ne(boolean condition, R column, Object val)
- 不等于 <>
- 例:
ne("name", "老王")—>name <> '老王'
gt
gt(R column, Object val) gt(boolean condition, R column, Object val)
大于 >
- 例:
gt("age", 18)—>age > 18
ge
ge(R column, Object val) ge(boolean condition, R column, Object val)
- 大于等于 >=
- 例:
ge("age", 18)—>age >= 18
lt
lt(R column, Object val) lt(boolean condition, R column, Object val)
- 小于 <
- 例:
lt("age", 18)—>age < 18
le
le(R column, Object val) le(boolean condition, R column, Object val)
- 小于等于 <=
- 例:
le("age", 18)—>age <= 18
between
between(R column, Object val1, Object val2) between(boolean condition, R column, Object val1, Object val2)
- BETWEEN 值1 AND 值2
- 例:
between("age", 18, 30)—>age between 18 and 30
notBetween
notBetween(R column, Object val1, Object val2) notBetween(boolean condition, R column, Object val1, Object val2)
- NOT BETWEEN 值1 AND 值2
- 例:
notBetween("age", 18, 30)—>age not between 18 and 30
like
like(R column, Object val) like(boolean condition, R column, Object val)
- LIKE ‘%值%’
- 例:
like("name", "王")—>name like '%王%'
notLike
notLike(R column, Object val) notLike(boolean condition, R column, Object val)
- NOT LIKE ‘%值%’
- 例:
notLike("name", "王")—>name not like '%王%'
likeLeft
likeLeft(R column, Object val) likeLeft(boolean condition, R column, Object val)
- LIKE ‘%值’
- 例:
likeLeft("name", "王")—>name like '%王'
likeRight
likeRight(R column, Object val) likeRight(boolean condition, R column, Object val)
- LIKE ‘值%’
- 例:
likeRight("name", "王")—>name like '王%'
isNull
isNull(R column) isNull(boolean condition, R column)
- 字段 IS NULL
- 例:
isNull("name")—>name is null
isNotNull
isNotNull(R column) isNotNull(boolean condition, R column)
- 字段 IS NOT NULL
- 例:
isNotNull("name")—>name is not null
in
in(R column, Collection<?> value) in(boolean condition, R column, Collection<?> value)
- 字段 IN (value.get(0), value.get(1), …)
- 例:
in("age",{1,2,3})—>age in (1,2,3)
in(R column, Object... values) in(boolean condition, R column, Object... values)
- 字段 IN (v0, v1, …)
- 例:
in("age", 1, 2, 3)—>age in (1,2,3)
notIn
notIn(R column, Collection<?> value) 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)
notIn(R column, Object... values) notIn(boolean condition, R column, Object... values)
- 字段 NOT IN (v0, v1, …)
- 例:
notIn("age", 1, 2, 3)—>age not in (1,2,3)
inSql
inSql(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)
notInSql
notInSql(R column, String inValue) 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)
groupBy
groupBy(R... columns) groupBy(boolean condition, R... columns)
- 分組:GROUP BY 字段, …
- 例:
groupBy("id", "name")—>group by id,name
orderByAsc
orderByAsc(R... columns) orderByAsc(boolean condition, R... columns)
- 排序:ORDER BY 字段, … ASC
- 例:
orderByAsc("id", "name")—>order by id ASC,name ASC
orderByDesc
orderByDesc(R... columns) orderByDesc(boolean condition, R... columns)
- 排序:ORDER BY 字段, … DESC
- 例:
orderByDesc("id", "name")—>order by id DESC,name DESC
orderBy
orderBy(boolean condition, boolean isAsc, R... columns)
- 排序:ORDER BY 字段, …
- 例:
orderBy(true, true, "id", "name")—>order by id ASC,name ASC
having
having(String sqlHaving, Object... params) 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
func
func(Consumer<Children> consumer) func(boolean condition, Consumer<Children> consumer)
- func 方法(主要方便在出現(xiàn)if…else下調(diào)用不同方法能不斷鏈)
- 例:
func(i -> if(true) {i.eq("id", 1)} else {i.ne("id", 1)})
or
or() or(boolean condition)
拼接 OR
注意事項:
主動調(diào)用
or表示緊接著下一個方法不是用and連接!(不調(diào)用or則默認(rèn)為使用and連接)例:
eq("id",1).or().eq("name","老王")—>id = 1 or name = '老王'
or(Consumer<Param> consumer) or(boolean condition, Consumer<Param> consumer)
- OR 嵌套
- 例:
or(i -> i.eq("name", "李白").ne("status", "活著"))—>or (name = '李白' and status <> '活著')
and
and(Consumer<Param> consumer) and(boolean condition, Consumer<Param> consumer)
- AND 嵌套
- 例:
and(i -> i.eq("name", "李白").ne("status", "活著"))—>and (name = '李白' and status <> '活著')
nested
nested(Consumer<Param> consumer) nested(boolean condition, Consumer<Param> consumer)
- 正常嵌套 不帶 AND 或者 OR
- 例:
nested(i -> i.eq("name", "李白").ne("status", "活著"))—>(name = '李白' and status <> '活著')
apply
apply(String applySql, Object... params) apply(boolean condition, String applySql, Object... params)
拼接 sql
注意事項:
該方法可用于數(shù)據(jù)庫函數(shù) 動態(tài)入?yún)⒌?code>params對應(yīng)前面
applySql內(nèi)部的{index}部分.這樣是不會有sql注入風(fēng)險的,反之會有!- 例:
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'")
last
last(String lastSql) last(boolean condition, String lastSql)
無視優(yōu)化規(guī)則直接拼接到 sql 的最后
注意事項:
只能調(diào)用一次,多次調(diào)用以最后一次為準(zhǔn) 有sql注入的風(fēng)險,請謹(jǐn)慎使用
例:
last("limit 1")
exists
exists(String existsSql) exists(boolean condition, String existsSql)
- 拼接 EXISTS ( sql語句 )
- 例:
exists("select id from table where age = 1")—>exists (select id from table where age = 1)
notExists
notExists(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)
到此這篇關(guān)于mybatisplus條件構(gòu)造器的方法使用的文章就介紹到這了,更多相關(guān)mybatisplus條件構(gòu)造器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用線程池實現(xiàn)socket編程的方法詳解
這篇文章主要為大家詳細(xì)介紹了Java使用線程池實現(xiàn)socket編程的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
解決Spring Batch框架job任務(wù)只跑一次的問題
這篇文章主要介紹了解決Spring Batch框架job任務(wù)只跑一次的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
idea中打開項目時import project和open區(qū)別詳解
本文主要介紹了idea中打開項目時import project和open區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

