SoFunction
Updated on 2025-03-04

How to use Mybatis Plus in Spring Boot

Using Mybatis Plus in Spring Boot

In modern enterprise-level development, MyBatis Plus is an enhancement tool for MyBatis, which simplifies many common database operations. With Spring Boot integration with MyBatis Plus, you can quickly build efficient and concise database operation layers. This article will explain how to integrate MyBatis Plus in Spring Boot projects.

1. Add Maven dependencies

In Spring Boot ProjectAdd the following related dependencies to the file:

<dependency>
  <groupId></groupId>
  <artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
  <groupId></groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>3.5.7</version>
</dependency>

2. Add relevant configurations to the configuration file

spring:
  datasource:
    url: jdbc:mysql://:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;useSSL=false&amp;zeroDateTimeBehavior=convertToNull
    username: root
    password: xxxxxx
    hikari: # Database connection pool      minimum-idle: 5 # Minimum number of idle connections in the connection pool      maximum-pool-size: 20 # Maximum number of connections to the connection pool      auto-commit: true # Automatically submit connections returned from the connection pool      idle-timeout: 30000 # The maximum time the connection allows to be idle in the connection pool      pool-name: SpringBootDemo-HikariCP # User-defined name of the connection pool      max-lifetime: 1800000 # The maximum life cycle of connection in the connection pool      connection-timeout: 30000 # Maximum number of milliseconds to wait for a connection from the connection pool      connection-test-query: SELECT 1 # Connection pool connection test statement

3. Create Mybatis configuration class

// Scan the package path where the Mapper is located@MapperScan("")
@Configuration
public class MybatisPlusConfig {
  	/**
      * Add Mybatis Plus paging plugin
      */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        (new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }
}

4. Create entity class

public class User {
    @TableId(type = )
    private Long id;
    private String name;
    private Integer age;
    @TableLogic
    private boolean isDeleted;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
         = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
         = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
         = age;
    }
    public boolean isDeleted() {
        return isDeleted;
    }
    public void setDeleted(boolean deleted) {
        isDeleted = deleted;
    }
}

5. Create a Mapper

MyBatis Plus provides a basic Mapper interface. Inheriting it can have common functions of adding, deleting, modifying and checking.

public interface UserMapper extends BaseMapper<User> {
}

6. Create Service

Mybatis Plus's ServiceImpl is an abstract class that implements the IService interface and enhances the encapsulation of Mapper.

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

Service interface layer

public interface UserService extends IService<User> {
}

7. Conclusion

This article describes how to integrate Mybatis Plus in Spring Boot projects. The integration of Spring Boot with MyBatis Plus is very simple. Through automatic configuration and a simple API, it can greatly reduce the common database operation code in development. MyBatis Plus provides many practical functions, such as pagination query, conditional construction, automatic filling, etc., which can greatly improve development efficiency.

This is all about this article about using Mybatis Plus in Spring Boot. For more related content on using Mybatis Plus, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!