首頁 > 軟體

Mybatis-Plus中and()和or()的使用與原理詳解

2022-09-08 18:04:00

一. 簡單無優先順序連線(即無括號的sql語句)

簡單來說,兩個子條件間預設and與連線,若兩個之間顯式寫出or()則or或連線.

1. 與連線 and()

當需要簡單的將兩個條件與連線,則最直接的寫法為:

QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().
eq("attr_id",key).
eq("catelog_id",catelogId);

當然也可以顯式地寫出and()如下,但沒必要:

QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().
eq("attr_id",key);
 
queryWrapper.and(qr -> qr.eq("catelog_id", catelogId));

2. 或連線 or()

當需要簡單的將兩個條件或連線,則最直接的寫法為:

QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().
eq("attr_id",key).
or().
eq("catelog_id",catelogId);

當然也可以如下,但不那麼直觀:

QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().
eq("attr_id",key);
 
queryWrapper.or(qr -> qr.eq("catelog_id", catelogId));

二. 複雜有優先順序的的連線

上面有2個不推薦的做法,是因為sql語句為A or B , A and B這種簡單連線.當涉及到諸如 A and ( B or C) and D 這類的複雜有優先順序的的連線,直接拼接會導致成為 A and B or C and D.所以這時候需要需要or(Consumer consumer),and(Consumer consumer)這兩個方法.範例如下:

        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type", "base".equalsIgnoreCase(type) ? 1 : 0);
        queryWrapper.and(qr -> 
                qr.eq("attr_id", key).
                   or().
                   like("attr_name", key)
        );
        queryWrapper.and(qr -> qr.eq("catelog_id", catelogId));

生成的sql語句如下:

select ...
WHERE (attr_type = ? AND ( (attr_id = ? OR attr_name LIKE ?) ) AND ( (catelog_id = ?) ))
...;

由此還可見or(Consumer consumer),and(Consumer consumer)這兩個方法引數為Consumer時,會在連線處生成2對括號,以此提高優先順序.

補充:MybatisPlus中and和or的組合使用

案例1:where   A=? and B=? 

        //SELECT id,name,age,sex FROM student WHERE (name = ? AND age = ?)
        List<Student> list = studentService.lambdaQuery().eq(Student::getName, "1").eq(Student::getAge, 1).list();     

案例2:where A=? or B=? 

        //SELECT id,name,age,sex FROM student WHERE (name = ? OR age = ?)
        List<Student> list = studentService.lambdaQuery().eq(Student::getName, "1").or().eq(Student::getAge, 12).list();

案例3:where A=? or(C=? and D=?) 

        //SELECT id,name,age,sex FROM student WHERE (name = ? OR (name = ? AND age = ?)) 
      List<Student> list =
          studentService
              .lambdaQuery()
              .eq(Student::getName, "1")
              .or(wp -> wp.eq(Student::getName, "1").eq(Student::getAge, 12))
              .list();      

案例4:where (A=?andB=?)or(C=?andD=?) 

    // SELECT id,name,age,sex FROM student WHERE ((name = ? AND age = ?) OR (name = ? AND age = ?)) 
    List<Student> list =
        studentService
            .lambdaQuery()
            .and(wp -> wp.eq(Student::getName, "1").eq(Student::getAge, 12))
            .or(wp -> wp.eq(Student::getName, "1").eq(Student::getAge, 12))
            .list();  

案例5:whert  A =? or (B=? and ( C=? or D=?)) 

    // SELECT * FROM student WHERE ((name <> 1) OR (name = 1 AND (age IS NULL OR age >= 11)))
    List<Student> list =
        studentService
            .lambdaQuery()
            .and(wp -> wp.ne(Student::getName, "1"))
            .or(
                wp ->
                    wp.eq(Student::getName, "1")
                        .and(wpp -> wpp.isNull(Student::getAge).or().ge(Student::getAge, 11)))
            .list();

總結

到此這篇關於Mybatis-Plus中and()和or()使用與原理的文章就介紹到這了,更多相關Mybatis-Plus中and()和or()使用內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com