MyBatis Basic Usage - Conditional Constructor Wrapper

The conditional constructor Wrapper of the MyBatisPlus framework constructs complex dynamic mosaic queries

The condition constructor Wrapper of MyBatisPlus can not only construct simple query conditions, but also dynamically splicing query conditions to perform flexible queries according to business needs. Below are detailed code samples, configuration files, and reference instructions.

1. Code example

First, we need to import the dependency library of MyBatisPlus and configure the database connection information. Next, we use Wrapper to dynamically stitch query conditions and perform query operations.

// 引入MyBatisPlus的依赖库
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    
    

    @Autowired
    private UserMapper userMapper;

    public List<User> getUsersByCondition(String name, Integer age, String email) {
    
    
        // 创建一个QueryWrapper对象,用于构建查询条件
        QueryWrapper<User> queryWrapper = Wrappers.query();
        
        // 判断查询条件是否为空,并动态拼接查询条件
        if (name != null && !name.isEmpty()) {
    
    
            queryWrapper.like("name", name);
        }
        if (age != null) {
    
    
            queryWrapper.ge("age", age);
        }
        if (email != null && !email.isEmpty()) {
    
    
            queryWrapper.like("email", email);
        }
        
        // 执行查询操作
        List<User> userList = userMapper.selectList(queryWrapper);
        
        return userList;
    }
}

In the above code, we use Wrapper's like and ge methods to dynamically stitch query conditions. According to the parameters passed in, it is judged whether it is empty, and the query is performed according to the conditions.

2. Configuration file

In the configuration file, we need to configure the database connection information and the related configuration of MyBatisPlus, which is the same as the previous example.

# 数据库连接信息
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: password
    driver-class-name: com.mysql.jdbc.Driver

# MyBatisPlus相关配置
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.model

3. Quote

When using MyBatisPlus's conditional builder Wrapper to construct a complex dynamic splicing query, you need to introduce the MyBatisPlus dependency library in the pom.xml file, which is the same as the previous example.

<!-- MyBatisPlus依赖 -->
<dependencies>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.1</version>
    </dependency>
</dependencies>

The above are the complete code examples, configuration files and references for constructing complex dynamic mosaic queries using the conditional constructor Wrapper of the MyBatisPlus framework. By using Wrapper, we can dynamically stitch query conditions according to business needs to achieve flexible query functions.

Guess you like

Origin blog.csdn.net/qq_41177135/article/details/131921486