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

