mybatisPlus條件構(gòu)造器常用方法小結(jié)
首先是.select
在MP查詢中,默認(rèn)查詢所有的字段,如果有需要也可以通過(guò)select方法進(jìn)行指定字段。其中要注意的細(xì)節(jié):
wrapper.select("pname") .eq("pname","張三") .or().eq("price",300); List<User> userList = userDao.selectList(wrapper);
使用select進(jìn)行sql語(yǔ)句拼接時(shí),不會(huì)識(shí)別在實(shí)體類中屬性對(duì)應(yīng)的操作:
SELECT pname FROM USER WHERE (pname = ? or price =? )
當(dāng)數(shù)據(jù)庫(kù)表中的字段名,與實(shí)體類對(duì)象的屬性名不一致時(shí)
wrapper.select("pname as name") .eq("pname","張三") .or().eq("price",300); List<User> userList = userDao.selectList(wrapper);
這樣拼接出來(lái)的sql語(yǔ)句:
SELECT pname as name FROM user WHERE (pname = ? OR price = ? )
其他條件
函數(shù)名 | 說(shuō)明 | 例子 |
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語(yǔ)句) | inSql(“id”, “select id from table where id < 3”) —–> id in (select id from table where id < 3) |
notInSql | 字段NOT IN ( sql語(yǔ)句) | 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語(yǔ)句) | having(“sum(age) >{0}”,11) —> having sum(age) > 11 |
or | 拼接OR | 主動(dòng)調(diào)用or表示緊接著下一個(gè)方法不是用and連接!(不調(diào)用or則默認(rèn)為使用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ù)庫(kù)函數(shù)動(dòng)態(tài)入?yún)⒌膒arams對(duì)應(yīng)前面sqlHaving內(nèi)部的{index}部分.這樣是不會(huì)有sql注入風(fēng)險(xiǎn)的,反之會(huì)有! 例: apply(“date_format(dateColumn, ‘%Y一%m-%d’) ={0}”, “2008-08-08”) —> date_format(dateColumn,’%Y一%m-%d’) = ‘2008-08-08’") |
last | 無(wú)視優(yōu)化規(guī)則直接拼接到sql 的最后 | 無(wú)視優(yōu)化規(guī)則直接拼接到sql 的最后只能調(diào)用一次,多次調(diào)用以最后一次為準(zhǔn)有sql注入的風(fēng)險(xiǎn),請(qǐng)謹(jǐn)慎使用例: last(“limit 1”) |
exists | 拼接EXISTS ( sql語(yǔ)句) | —> 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語(yǔ)句) | 例: 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 活著’) |
到此這篇關(guān)于mybatisPlus條件構(gòu)造器常用方法的文章就介紹到這了,更多相關(guān)mybatisPlus條件構(gòu)造器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 關(guān)于String字符串原理上的問(wèn)題
字符串廣泛應(yīng)用 在 Java 編程中,在 Java 中字符串屬于對(duì)象,Java 提供了 String 類來(lái)創(chuàng)建和操作字符串,讓我們一起來(lái)了解它2022-04-04Java設(shè)計(jì)模式之橋接模式的實(shí)現(xiàn)
今天給大家?guī)?lái)的文章是Java設(shè)計(jì)模式的相關(guān)知識(shí)點(diǎn),文中對(duì)橋接模式作了非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06Java中網(wǎng)絡(luò)IO的實(shí)現(xiàn)方式(BIO、NIO、AIO)介紹
這篇文章主要介紹了Java中網(wǎng)絡(luò)IO的實(shí)現(xiàn)方式(BIO、NIO、AIO)介紹的相關(guān)資料,需要的朋友可以參考下2017-03-03一篇文章帶你使用SpringBoot基于WebSocket的在線群聊實(shí)現(xiàn)
這篇文章主要介紹了一篇文章帶你使用SpringBoot基于WebSocket的在線群聊實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10SpringBoot實(shí)現(xiàn)excel文件生成和下載
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)excel文件生成和下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02Spring?Boot自動(dòng)配置的原理及@Conditional條件注解
這篇文章主要介紹了Spring?Boot自動(dòng)配置的原理及@Conditional條件注解,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2022-07-07