Multi-condition query in mybatis-plus uses AND or nesting

MyBatisPlus-QueryWrapper has many query methods for building query conditions
1. Existing methods
gt, ge, lt, le, isNull, isNotNull
eq, ne
between, notBetween
allEq
like, notLike, likeLeft, likeRight
in, notIn, inSql, notinSql, exists, notExists
or, and
nested or, nested and
orderBy, orderByDesc, orderByAsc
last
specifies the column to be queried
set, setSql

2. Use of ge and isNull

@Test
public void queryWrapperOne() {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.
            isNull("name")
            .ge("age", 23)
            .isNotNull("email");
    // 逻辑删除
    int result = userMapper.delete(queryWrapper);
    System.out.println(result);
    // 最终的语句为:UPDATE user SET deleted=1 WHERE deleted=0 AND name IS NULL AND age >= ? AND email IS NOT NULL
}

3.Use of eq and ne

@Test
public void queryWrapperTwo() {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("name", "BNTang");
    // selectOne:返回的是一条记录,当出现多条时会报错
    User user = userMapper.selectOne(queryWrapper);
    System.out.println(user);
}

4.Use of between and notBetween
BETWEEN value 1 AND value 2
Example: between(“age”, 18, 30) → age between 18 and 30

NOT BETWEEN value 1 AND value 2
Example: notBetween(“age”, 18, 30) → age not between 18 and 30

5.Use of allEq
Example 1: allEq({id:1,name:"老王",age:null})→id = 1 and name = '老王' and age is null Example 2: allEq({id
: 1, name: "老王", age: null}, false) → id = 1 and name = '老王' The
key value of the queryParamsMap constructed must be a field in the database, or a field in the query temporary table

@Test
public void queryWrapperFour() {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    Map<String, Object> queryParamsMap = new HashMap<>();
    queryParamsMap.put("id", 1373266771933462530L);
    queryParamsMap.put("name", "BNTang");
    queryParamsMap.put("age", 23);
    queryWrapper.allEq(queryParamsMap);
    List<User> users = userMapper.selectList(queryWrapper);
    users.forEach(System.out::println);
    // sql为:SELECT * FROM user WHERE deleted=0 AND name = 'BNTang' AND id = '1373266771933462530' AND age = 23
}

filter: filter function, whether to allow fields to be passed into the comparison conditions
params and null, IsNull the same as above

Example 1: allEq((k,v) -> k.indexOf("a") >= 0, {id:1,name:"老王",age:null})→name = '老王' and age is null
Example 2: allEq((k,v) -> k.indexOf("i") >= 0, {id:1,name:"老王",age:null})→name = '老王'
Example 3: allEq((k,v) -> k.indexOf("a") >= 0, {id:1,name:"老王",age:null}, false)→name = '老王'

6.in、notIn、inSql、notinSql、exists、notExists的使用
例: in(“age”,{1,2,3})→age in (1,2,3)
queryWrapper.in(“age”, (Object[]) “5,6”.split(“,”));
例: notIn(“age”,{1,2,3})→age not in (1,2,3)

例: inSql(“age”, “1,2,3,4,5,6”)→age in (1,2,3,4,5,6)
例: inSql(“id”, “select id from table where id < 3”)→id in (select id from table where id < 3)

例: exists(“select id from table where age = 1”)→exists (select id from table where age = 1)
例: notExists(“select id from table where age = 1”)→not exists (select id from table where age = 1)

7. Use with or nesting

@Test
public void queryWrapperSeven() {
    // 修改值
    User user = new User();
    user.setAge(99);
    user.setName("BNTang6666");
    // 修改条件
    UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
    userUpdateWrapper
            .like("name", "BNTang")
            .or()
            .between("age", 20, 30);
    int result = userMapper.update(user, userUpdateWrapper);
    System.out.println(result);
    // sql 为:
     /* UPDATE USER SET NAME = 'BNTang6666,age=99,update_time = '2021-03-27 00:40:27'
        WHERE deleted = 0 AND NAME LIKE '%BNTang%'  OR age BETWEEN 20 AND 30*/
}

Example 1: wrapper.eq("id",1).or().eq("name","老王")→id = 1 or name = '老王'

Example 2: wrapper.eq("age",30).or(i -> i.eq("name", "Li Bai").ne("status", "alive"))→age=30 or (name = 'Li Bai' and status <> 'alive')

Example 3: and(i -> i.eq("name", "Li Bai").ne("status", "alive"))→and (name = 'Li Bai' and status <> 'alive')

Example 4: wrapper.like("name", "BNTang").or().between("age", 20, 30); The
result is similar to Example 1

例5:
wrapper.and(wrapper->wrapper.eq(“SEND_USER_ID”,“001”).eq(“RECEIVE_USER_ID”,“002”))
.or(wrapper->wrapper.eq(“SEND_USER_ID”,“003”).eq(“RECEIVE_USER_ID”,“004”));
-> AND ((SEND USER ID = “001” AND RECEIVE USER ID = “002”) OR (SEND USER ID =“003” AND RECEIVE USER ID = “004”))

例6:
queryWrapper.eq(“name”, “wangsf”).nested(w->w.and(wp->wp.in(“contract_mode”, (Object[]) “5,6”.split(“,”)).ge(“actual_completion_time”, now.minusYears(2).format(fmt) + " 23:59:59")).or(wp->wp.notIn(“contract_mode”, (Object[]) “5,6”.split(“,”)).ge(“actual_completion_time”, now.minusYears(5).format(fmt) + " 23:59:59")));
}
->
name = ? AND ((contract_mode IN (?,?) AND actual_completion_time BETWEEN ? AND ?) OR (contract_mode NOT IN (?,?) AND actual_completion_time BETWEEN ? AND ?))

8.Look at the picture example
Insert image description here

9. Note that
during the actual development process of paging query, an object is generally encapsulated as the receiving query field. These conditions generally correspond to the fields in single table A and are queried directly; they may also be the field range in query table A, such as a certain For date range, you need to build a new query field parameter range query; the condition may also be to associate fields in other table B. The common method is to build an intermediate table first and then bring in the paging parameter query (${ew.customSqlSegment} is the constructed paging and Query parameters; t is equivalent to an intermediate table)

Insert image description here

Reference https://www.zhuxianfei.com/java/58478.html

Guess you like

Origin blog.csdn.net/rainAndyou/article/details/132063804