Application of several conditional constructors in MybatisPlus

version introduction

jdk 17

SpringBoot 3.1.0

druid-spring-boot-starter 1.2.4

mysql-connector 8.0.33

mybatis-plus 3.5.3.1

QueryWrapper

QueryWrapper of MybatisPlus is a tool class for constructing SQL query conditions, which provides a series of methods to facilitate condition construction. The following are commonly used methods of QueryWrapper:

  1. eq(column, value): equal to query, the value of the specified field column is equal to value.
    Example: queryWrapper.eq("name", "Zhang San");

  2. ne(column, value): Not equal to the query, the value of the specified field column is not equal to value.
    Example: queryWrapper.ne("age", 20);

  3. gt(column, value): Greater than query, the value of the specified field column is greater than value.
    Example: queryWrapper.gt("salary", 5000);

  4. ge(column, value): greater than or equal query, the value of the specified field column is greater than or equal to value.
    Example: queryWrapper.ge("create_time", "2021-01-01");

  5. lt(column, value): less than query, the value of the specified field column is less than value.
    Example: queryWrapper.lt(“age”, 30);

  6. le(column, value): less than or equal query, the value of the specified field column is less than or equal to value.
    Example: queryWrapper.le("salary", 10000);

  7. like(column, value): Fuzzy query, the value of the specified field column contains value.
    Example: queryWrapper.like("name", "Zhang");

  8. notLike(column, value): does not contain the query, the value of the specified field column does not contain value.
    Example: queryWrapper.notLike("name", "Lee");

  9. in(column, values): contains the query, and the value of the specified field column is in the values ​​array.
    Example: queryWrapper.in("age", Arrays.asList(20, 30, 40));

  10. notIn(column, values): does not contain the query, and the value of the specified field column is not in the values ​​array.
    Example: queryWrapper.notIn("age", Arrays.asList(50, 60, 70));

  11. between(column, value1, value2): range query, the value of the specified field column is between value1 and value2.
    Example: queryWrapper.between("salary", 5000, 10000);

  12. orderByAsc(column): Sort in ascending order, sort in ascending order according to the specified field column.
    Example: queryWrapper.orderByAsc("age");

  13. orderByDesc(column): Sort in descending order, sort in descending order according to the specified field column.
    Example: queryWrapper.orderByDesc("salary");

  14. select(columns): Specifies the field to be queried, and the parameter columns is a list of fields.
    Example: queryWrapper.select("id", "name", "age");

  15. last(sql): Add a custom SQL query condition, the parameter sql is the SQL condition to be added.
    Example: queryWrapper.last("LIMIT 10");

example

QueryWrapper<User> queryWrapper = new QueryWrapper();
queryWrapper.eq("user_name", user.getUserName());

return selectList(queryWrapper);

LambdaQueryWrapper

// 创建LambdaQueryWrapper对象
LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery();

// 设置查询条件
lambdaQueryWrapper
        .eq(User::getUserName, user.getUserName()) ;

// 执行查询
List<User> userList = userMapper.selectList(lambdaQueryWrapper);
return userList;

UpdateWrapper

// 创建UpdateWrapper对象
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();

// 设置更新条件
updateWrapper
        .eq("user_name", user.getUserName());

// 执行更新操作
User updateUser = new User();
updateUser.setPassword(user.getPassword());
int rows = update(user, updateWrapper);
return rows;

LambdaUpdateWrapper

// 创建UpdateWrapper对象
LambdaUpdateWrapper<User> updateWrapper = Wrappers.lambdaUpdate();

// 设置更新条件
updateWrapper
        .eq(User::getUserName, user.getUserName());

// 执行更新操作
User updateUser = new User();
updateUser.setPassword(user.getPassword());
int rows = update(user, updateWrapper);
return rows;

If you need the complete source code, please pay attention to the official account "Architecture Palace", reply "SpringBoot+mybatisplus+wrapper" to get

write at the end

If you are interested in related articles, you can pay attention to the official account "Architecture Hall", and will continue to update AIGC, java basic interview questions, netty, spring boot, spring cloud and other series of articles, and a series of dry goods will be delivered at any time!

Guess you like

Origin blog.csdn.net/jinxinxin1314/article/details/132258986