MyBatis-plus: fuzzy query

When using MyBatis-plus, some basic additions, deletions, changes, and queries can be done without writing sql by yourself:

public interface UserDao extends BaseMapper<FykUser>{
    
    

}

In this way, the addition, deletion, modification and query of the user table can be realized.

fuzzy query

Use the userDao.selectList(queryWrapper) method to query a list of users.
If fuzzy query is required, the code is as follows:

//条件封装
QueryWrapper<FykUser> queryWrapper = new QueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(user.getName()), "NAME", user.getName());
queryWrapper.like(user.getEnable() != null, "ENABLE", user.getEnable());
List<FykUser> userList = userDao.selectList(queryWrapper);

In other words, just call the like method of queryWrapper.
Here, the like method has three parameters:

  • The first parameter: This parameter is a Boolean type. Only when the parameter is true, the like condition will be spliced ​​into the sql; in this example, if the name field is not empty, the like query condition of the name field will be spliced;
  • The second parameter: this parameter is the field name in the database;
  • The third parameter: the parameter value field value;

It should be noted that the like query here is the default method used, that is to say, there are % on both sides of the query condition: NAME = '%王%'; if you only need to
splice % on the left or right, you can use likeLeft or likeRight method.

other

In the QueryWrapper class, you can see that there are many conditional query methods, such as ge, le, lt, between, etc., and their parameter passing methods are similar to those described above.

Guess you like

Origin blog.csdn.net/fyk844645164/article/details/100892144