mybatis plus實現條件查詢

一、wapper介紹

用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。

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表示 &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&lt;User&gt; wrapperUser = new QueryWrapper&lt;&gt;();
        Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
        map.put("id", 5);
        map.put("name", "wesson5");
        map.put("age", 29);
        wrapperUser.allEq(map);
        List&lt;User&gt; 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&lt;User&gt; wrapperUser = new QueryWrapper&lt;&gt;();
        wrapperUser.in("id", 1, 2, 3); 
        List&lt;User&gt; 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: 

以上是一些在業務開發中常用的,稍復雜些的條件查詢,實際情況可能還有其他組合變化,更多關於mybatis plus條件查詢的資料請關註WalkonNet其它相關文章!

推薦閱讀: