用mp也可以方便的實現稍複雜點的條件查詢,當然了很複雜的就還是要xml編寫sql了。
一、wapper介紹
先看下mp的條件構造抽象類的結構:
Wrapper
: 條件構造抽象類,最頂端父類AbstractWrapper
: 用於查詢條件封裝,生成 sql 的 where 條件QueryWrapper
: Entity 物件封裝操作類,不是用lambda語法UpdateWrapper
: Update 條件封裝,用於Entity物件更新操作AbstractLambdaWrapper
: Lambda 語法使用 Wrapper統一處理解析lambda獲取資料庫欄位LambdaQueryWrapper
: 用於Lambda語法使用的查詢WrapperLambdaUpdateWrapper
: 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。
1. gt
表示 >
... ...
// 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)
2. le
表示 <=
... ...
// 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)
3. lt
表示 <
... ...
// 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)
4. isNull
表示 查詢值為null
... ...
// 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
5. isNotNull
表示 查詢值為不為null
... ...
// 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:
6. eq
表示 =
... ...
// 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)
7. ne
表示 !=
... ...
// 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)
8. between
表示 在範圍之間,包含邊界值
... ...
// 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)
9. notBetween
表示 在範圍之外,不含邊界值
... ...
// 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)
10. notBetween
表示 在範圍之外,不含邊界值
... ...
// 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)
11. allEq
多條件查詢
如果我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)
12. .
鏈式程式設計,多條件查詢
此外,還可以使用鏈式程式設計,直接在後面繼續.
呼叫別的方法。
@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)
13. or、and
預設情況下,在不調撥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)
14. 巢狀or、巢狀and
查詢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)
15. in、notIn
等於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)
16. inSql、notinSql
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)
17. last
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:
18. 指定要查詢的列
只查詢出指定的欄位,比如"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:
以上是一些在業務開發中常用的,稍複雜些的條件查詢,實際情況可能還有其他組合變化。