mybatisPlus條件構(gòu)造器常用方法小結(jié)
首先是.select
在MP查詢中,默認(rèn)查詢所有的字段,如果有需要也可以通過select方法進(jìn)行指定字段。其中要注意的細(xì)節(jié):
wrapper.select("pname")
.eq("pname","張三")
.or().eq("price",300);
List<User> userList = userDao.selectList(wrapper);使用select進(jìn)行sql語句拼接時(shí),不會(huì)識(shí)別在實(shí)體類中屬性對(duì)應(yīng)的操作:
SELECT pname FROM USER WHERE (pname = ? or price =? )
當(dāng)數(shù)據(jù)庫表中的字段名,與實(shí)體類對(duì)象的屬性名不一致時(shí)
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 | 主動(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ù)庫函數(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 | 無視優(yōu)化規(guī)則直接拼接到sql 的最后 | 無視優(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語句) | —> 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 活著’) |
到此這篇關(guān)于mybatisPlus條件構(gòu)造器常用方法的文章就介紹到這了,更多相關(guān)mybatisPlus條件構(gòu)造器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于JavaSwing+mysql開發(fā)一個(gè)學(xué)生社團(tuán)管理系統(tǒng)設(shè)計(jì)和實(shí)現(xiàn)
項(xiàng)目使用Java swing+mysql開發(fā),可實(shí)現(xiàn)基礎(chǔ)數(shù)據(jù)維護(hù)、用戶登錄注冊(cè)、社團(tuán)信息列表查看、社團(tuán)信息添加、社團(tuán)信息修改、社團(tuán)信息刪除以及退出注銷等功能、界面設(shè)計(jì)比較簡(jiǎn)單易學(xué)、適合作為Java課設(shè)設(shè)計(jì)以及學(xué)習(xí)技術(shù)使用,需要的朋友參考下吧2021-08-08
SpringBoot自定義MessageConvert詳細(xì)講解
正在學(xué)習(xí)SpringBoot,在自定義MessageConverter時(shí)發(fā)現(xiàn):為同一個(gè)返回值類型配置多個(gè)MessageConverter時(shí),可能會(huì)發(fā)生響應(yīng)數(shù)據(jù)格式錯(cuò)誤,或406異常(客戶端無法接收相應(yīng)數(shù)據(jù))。在此記錄一下解決問題以及追蹤源碼的過程2023-01-01
Java實(shí)現(xiàn)EasyCaptcha圖形驗(yàn)證碼的具體使用
Java圖形驗(yàn)證碼,支持gif、中文、算術(shù)等類型,可用于Java Web、JavaSE等項(xiàng)目,下面就跟隨小編一起來了解一下2021-08-08
MyBatis-Plus聯(lián)表查詢(Mybatis-Plus-Join)的功能實(shí)現(xiàn)
mybatis-plus作為mybatis的增強(qiáng)工具,簡(jiǎn)化了開發(fā)中的數(shù)據(jù)庫操作,這篇文章主要介紹了MyBatis-Plus聯(lián)表查詢(Mybatis-Plus-Join),需要的朋友可以參考下2022-08-08
Java?中的排序Comparable?與?Comparator?的使用與區(qū)別對(duì)比解析
本文給大家介紹Java中的排序Comparable與Comparator的使用與區(qū)別對(duì)比,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2025-09-09
Java volatile關(guān)鍵字原理剖析與實(shí)例講解
volatile是Java提供的一種輕量級(jí)的同步機(jī)制,Java?語言包含兩種內(nèi)在的同步機(jī)制:同步塊(或方法)和?volatile?變量,本文將詳細(xì)為大家總結(jié)Java volatile關(guān)鍵字,通過詳細(xì)的代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
詳解java封裝實(shí)現(xiàn)Excel建表讀寫操作
這篇文章給大家分享了java封裝實(shí)現(xiàn)Excel建表讀寫操作的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2018-08-08

