您好,登錄后才能下訂單哦!
本篇內容介紹了“mybatisplus如何實現條件查詢”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
用mp也可以方便的實現稍復雜點的條件查詢,當然了很復雜的就還是要xml編寫sql了。
先看下mp的條件構造抽象類的結構:
Wrapper: 條件構造抽象類,最頂端父類
AbstractWrapper: 用于查詢條件封裝,生成 sql 的 where 條件
QueryWrapper: Entity 對象封裝操作類,不是用lambda語法
UpdateWrapper: Update 條件封裝,用于Entity對象更新操作
AbstractLambdaWrapper: Lambda 語法使用 Wrapper統一處理解析lambda獲取數據庫字段
LambdaQueryWrapper: 用于Lambda語法使用的查詢Wrapper
LambdaUpdateWrapper: Lambda 更新封裝Wrapper
不過最常用的還是QueryWrapper、UpdateWrapper等這些。
套路還是那樣,先創建QueryWrapper對象,然后再調用各種方法。
// 測試條件查詢 @Test void testQueryWrapper() { //創建對象,泛型里加上實體對象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); // 設置查詢的條件 // ge表示 >= , 這里就是查詢age字段,大于40的數據 wrapperUser.ge("age", 40); // 調用查詢方法中,傳入wrapper對象 List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
這里就會查詢表里age>=40,的數據,看下執行過程的sql語句:
在構造條件的時候,除了上面的ge,還有很多其他的方法,這里簡單介紹下比較常用的,并且貼出執行的sql。
... ... // gt表示 > , 這里就是查詢age字段,大于40的數據 wrapperUser.gt("age", 40); ... ...
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age > ? ==> Parameters: 40(Integer)
... ... // le表示 <=, 這里就是查詢age字段,小于等于40的數據 wrapperUser.le("age", 40); ... ...
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age <= ? ==> Parameters: 40(Integer)
... ... // lt表示 <, 這里就是查詢age字段,小于40的數據 wrapperUser.lt("age", 40); ... ...
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age < ? ==> Parameters: 40(Integer)
... ... // isNull wrapperUser.isNull("name"); ... ...
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND name IS NULL ==> Parameters: <== Total: 0
... ... // isNotNull wrapperUser.isNotNull("name"); ... ...
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND name IS NOT NULL ==> Parameters:
... ... // eq wrapperUser.eq("name", "大周4"); ... ...
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND name = ? ==> Parameters: 大周4(String)
... ... // eq wrapperUser.ne("name", "大周4"); ... ...
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND name <> ? ==> Parameters: 大周4(String)
... ... // between wrapperUser.between("age", 40, 50); ... ...
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age BETWEEN ? AND ? ==> Parameters: 40(Integer), 50(Integer)
... ... // between wrapperUser.notBetween("age", 40, 50); ... ...
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age NOT BETWEEN ? AND ? ==> Parameters: 40(Integer), 50(Integer)
... ... // between wrapperUser.notBetween("age", 40, 50); ... ...
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age NOT BETWEEN ? AND ? ==> Parameters: 40(Integer), 50(Integer)
如果我where后面要加多個條件,可以使用allEq。先創建一個hashmap,然后把多個條件put進去,再調用allEq即可。
@Test void testQueryWrapper() { QueryWrapper<User> wrapperUser = new QueryWrapper<>(); Map<String, Object> map = new HashMap<>(); map.put("id", 5); map.put("name", "wesson5"); map.put("age", 29); wrapperUser.allEq(map); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND name = ? AND id = ? AND age = ? ==> Parameters: wesson5(String), 5(Integer), 29(Integer)
此外,還可以使用鏈式編程,直接在后面繼續.調用別的方法。
@Test void testQueryWrapper() { //創建對象,泛型里加上實體對象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.eq("age", 29) .eq("name", "wesson5") .eq("id", 5); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age = ? AND name = ? AND id = ? ==> Parameters: 29(Integer), wesson5(String), 5(Integer)
默認情況下,在不調撥or()方法的情況下,是使用and()。
@Test void testQueryWrapper() { //創建對象,泛型里加上實體對象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.eq("age", 29) .or() .eq("name", "wesson5") .or() .eq("id", 5); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age = ? OR name = ? OR id = ? ==> Parameters: 29(Integer), wesson5(String), 5(Integer)
查詢sql經常會有嵌套or或者and的情況,可以這樣寫:
@Test void testQueryWrapper() { //創建對象,泛型里加上實體對象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.eq("age", 29) .or( i -> i.eq("name", "wesson5") .or() .eq("id", 5) ); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age = ? OR ( name = ? OR id = ? ) ==> Parameters: 29(Integer), wesson5(String), 5(Integer)
等于sql里的 in和not in。
@Test void testQueryWrapper() { //創建對象,泛型里加上實體對象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.in("id", 1, 2, 3); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND id IN (?,?,?) ==> Parameters: 1(Integer), 2(Integer), 3(Integer)
inSql、notinSql可以用來子查詢,比如 where id in (select * ... ...)
@Test void testQueryWrapper() { //創建對象,泛型里加上實體對象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.in("id", "select id from user where id < 5"); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND id IN (?) ==> Parameters: select id from user where id < '5'(String)
last可以直接拼接sql到最后,只能調用一次,多次調用以最后一次為準。
注意:有sql注入的風險,慎用。
@Test void testQueryWrapper() { //創建對象,泛型里加上實體對象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.last("limit 1"); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp執行的sql:
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 limit 1 ==> Parameters:
只查詢出指定的字段,比如"id", "name", "age"。
@Test void testQueryWrapper() { //創建對象,泛型里加上實體對象 QueryWrapper<User> wrapperUser = new QueryWrapper<>(); wrapperUser.select("id", "name", "age"); List<User> users = userMapper.selectList(wrapperUser); System.out.println(users); }
mp執行的sql:
==> Preparing: SELECT id,name,age FROM user WHERE deleted=0 ==> Parameters:
“mybatisplus如何實現條件查詢”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。