How to write mybatis fuzzy query like statement?

Implementation

In MyBatis, fuzzy query can be realized by using like keyword in SQL statement. As follows:

<select id="findUsersByKeyword" parameterType="String" resultType="User">
  select * from user
  where username like concat('%', #{keyword}, '%')
</select>

Among them, #{keyword} is the incoming parameter, the concat function is used to concatenate strings, and % represents any character.

Call this method in Java code:

List<User> users = sqlSession.selectList("findUsersByKeyword", "Tom");

In this way, you can query all users whose username contains "Tom".

This MyBatis SQL statement can be parsed into an SQL statement similar to the following:

Why did you do this

select * from user where username like '%Tom%'This SQL statement uses the LIKE keyword for fuzzy query. In MyBatis, use the concat function to pass the splicing operation of the SQL statement through the Mapper configuration file, and pass the keywords to be queried into the SQL statement as parameters, and then you can use LIKE to perform fuzzy queries.

Specifically, the SQL statement using the #{keyword} placeholder symbol will be passed as a parameter to the implementation method of the corresponding Mapper interface, that is

List<User> users = sqlSession.selectList("findUsersByKeyword", "Tom");

Finally, MyBatis will replace #{keyword} with "Tom", generate a complete SQL statement and execute the query operation, and return a list of eligible results.

myabtis-plus

In MyBatis-Plus, fuzzy queries can also be implemented using the Like function. Suppose we have a User entity class and need to perform fuzzy query based on the user name. The implementation method is as follows:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public List<User> findUsersByKeyword(String keyword) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getUsername, keyword);
        return baseMapper.selectList(queryWrapper);
    }
}

Note that here we use LambdaQueryWrapperthe class to build the query condition, which calls likethe method, which is similar to the LIKE in the SQL statement. User::getUsernameis a method reference in Java 8, used to obtain the username attribute in the User entity, keyword is the query keyword, that is, the fuzzy matching string to be queried.

Finally, calling selectListthe method queryWrapperwill parse the query conditions built in through Mybatis-plus, automatically generate the corresponding SQL statement, perform database query according to the SQL statement automatically generated by Mybatis-plus, and return a list of qualified results.

It should be noted that when using LambdaQueryWrapperto perform fuzzy query, different types of like can be used, such as likeLeft, likeRight, etc., to specify the position of fuzzy matching during fuzzy matching. In the actual implementation, it should be selected according to the actual needs.

Summarize

In MyBatis, using likethe keyword to perform fuzzy query can concat()be realized by using the function in the SQL statement to splice the keyword to be queried into the SQL statement. In MyBatis-Plus, use LambdaQueryWrapperthe class to build query conditions, and call likethe method to perform fuzzy query, which is used to obtain the attributes of the corresponding entity and specify the query keywords. In general, fuzzy matching is a commonly used method in SQL query. Both MyBatis and MyBatis-Plus can easily support fuzzy query, and the specific implementation can be selected according to the needs of the project.

Guess you like

Origin blog.csdn.net/weixin_39570751/article/details/129921646
Recommended