mybatisPlus條件構造器常用方法小結
首先是.select
在MP查詢中,默認查詢所有的字段,如果有需要也可以通過select方法進行指定字段。其中要注意的細節(jié):
wrapper.select("pname") .eq("pname","張三") .or().eq("price",300); List<User> userList = userDao.selectList(wrapper);
使用select進行sql語句拼接時,不會識別在實體類中屬性對應的操作:
SELECT pname FROM USER WHERE (pname = ? or price =? )
當數(shù)據(jù)庫表中的字段名,與實體類對象的屬性名不一致時
wrapper.select("pname as name") .eq("pname","張三") .or().eq("price",300); List<User> userList = userDao.selectList(wrapper);
這樣拼接出來的sql語句:
SELECT pname as name FROM user WHERE (pname = ? OR price = ? )
其他條件
函數(shù)名 | 說明 | 例子 |
eq | 等于 = | 例:eq(“name”,“張三”) :name = ‘張三’ |
ne | 不等于<> | 例: eq(“name”,“老王”) —> name <> ‘老王’ |
gt | 大于> | 例:gt(“age”,18) —> age > 18 |
ge | 大于等于>= | 例:ge(“age”,18) —> age >= 18 |
lt | 小于< | 例:lt(“age”,18) —> age < 18 |
le | 小于<= | 例:le(“age”,18) —> age <= 18 |
between | BETWEEN值1 AND值2 | 例:between(“age”,18,30) —> age between 18 and 30 |
notBetween | NOT BETWEEN值1 AND值2 | 例: notBetween(“age”,18,30) —> age not between 18 and 30 |
like | LIKE ‘%值%’ | 例: like(“name”,“王”) —–> name like '%王%’ |
notLike | NOT LIKE ‘%值%’ | 例: notLike (“name”,“王”) —> name not like '%王%’ |
likeLeft | LIKE '%值’ | 例:likeLeft (“name”,“王”) —–> name like '%王’ |
likeRight | LIKE’值%’ | 例: likeRight(“name”,“王”) —> name like ‘王%’ |
isNull | 字段IS NULL | 例: isNul1 (“name”) —> name is null |
isNotNull | 字段IS NOT NULL | 例: isNotNull(“name”) —> name is not null |
in | 字段IN (v0, v1,…) | 例: in(“age”,{1,2,3} ) —–> age in (1,2,3) |
notIn | 字段NOT IN (v0, v1,…) | 例: notIn(“age”,1,2,3) —> age not in (1,2,3) |
inSql | 字段IN ( sql語句) | inSql(“id”, “select id from table where id < 3”) —–> id in (select id from table where id < 3) |
notInSql | 字段NOT IN ( sql語句) | notInSql(“id”, “select id from table where id < 3”) —–> age not in (select id from table where id < 3) |
groupBy | 分組:GROUP BY 字段,… | 例: groupBy(“id”, “name”) —> group by id, name |
orderByAsc | 排序:ORDER BY字段,… ASC | 例: orderByAsc(“id”, “name”) —> order by id ASC, name ASC |
orderByDesc | 排序:ORDER BY 字段,…DESC | 例: orderByDesc(“id”, “name”) —> order by id DESC, name DESC |
orderBy | 排序:ORDER BY字段,… | 例: orderBy(true,true,“id”,“name”) —–> order by id ASC,name ASC |
having | HAVING ( sql語句) | having(“sum(age) >{0}”,11) —> having sum(age) > 11 |
or | 拼接OR | 主動調用or表示緊接著下一個方法不是用and連接!(不調用or則默認為使用and連接)例:eq(“id”,1).or().eq(“name”,“老王”) —> id = 1 or name = '老王 |
and | AND嵌套 | 例: and(i -> i.eq(“name”,“李白”).ne(“status”,“活著”)) —> and (name ='李白’ and status ’活著’) |
apply | 拼接sql | 該方法可用于數(shù)據(jù)庫函數(shù)動態(tài)入?yún)⒌膒arams對應前面sqlHaving內部的{index}部分.這樣是不會有sql注入風險的,反之會有! 例: apply(“date_format(dateColumn, ‘%Y一%m-%d’) ={0}”, “2008-08-08”) —> date_format(dateColumn,’%Y一%m-%d’) = ‘2008-08-08’") |
last | 無視優(yōu)化規(guī)則直接拼接到sql 的最后 | 無視優(yōu)化規(guī)則直接拼接到sql 的最后只能調用一次,多次調用以最后一次為準有sql注入的風險,請謹慎使用例: last(“limit 1”) |
exists | 拼接EXISTS ( sql語句) | —> exists (select id from table where age = 1)例: notExists(“select id from table where age = 1”) —>exists (select id from table where age = 1) |
notExists | 拼接NOT EXISTS ( sql語句) | 例: notExists(“select id from table where age = 1”) —–> not exists (select id from table where age = 1) |
nested | 正常嵌套不帶AND或者 OR | 正常嵌套不帶AND或者OR例: nested(i -> i.eq(“name”,“李白”).ne(“status”,“活著”)) —> (name = '李白’and status 活著’) |
到此這篇關于mybatisPlus條件構造器常用方法的文章就介紹到這了,更多相關mybatisPlus條件構造器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java中網(wǎng)絡IO的實現(xiàn)方式(BIO、NIO、AIO)介紹
這篇文章主要介紹了Java中網(wǎng)絡IO的實現(xiàn)方式(BIO、NIO、AIO)介紹的相關資料,需要的朋友可以參考下2017-03-03一篇文章帶你使用SpringBoot基于WebSocket的在線群聊實現(xiàn)
這篇文章主要介紹了一篇文章帶你使用SpringBoot基于WebSocket的在線群聊實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10SpringBoot實現(xiàn)excel文件生成和下載
這篇文章主要為大家詳細介紹了SpringBoot實現(xiàn)excel文件生成和下載,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-02-02Spring?Boot自動配置的原理及@Conditional條件注解
這篇文章主要介紹了Spring?Boot自動配置的原理及@Conditional條件注解,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,感興趣的朋友可以參考一下2022-07-07