Comprehensive Guide to Integrating MyBatis‑Plus with Spring Boot
This article provides a step‑by‑step tutorial on adding MyBatis‑Plus to a Spring Boot project, covering dependency setup, configuration, entity annotations, CRUD operations, pagination, logical deletion, enum handling, automatic field filling, multi‑datasource support, and testing with full code examples.
This tutorial introduces MyBatis‑Plus, a powerful ORM plugin for Spring Boot users, and walks through a complete integration process.
1. Quick Start – Prepare a Spring Boot, Maven, JDK 1.8, MySQL environment and create the required database (UTF‑8 mb4 charset is recommended).
1.1 Dependency Preparation
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!-- mybatis‑plus multi‑datasource -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>1.2 Configuration Preparation
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@MapperScan("com.wjbgn.user.mapper")
@SpringBootApplication
public class RobNecessitiesUserApplication {
public static void main(String[] args) {
SpringApplication.run(RobNecessitiesUserApplication.class, args);
}
}Data source configuration (master‑slave example) and HikariCP pool settings are added under spring.datasource.dynamic in application.yml .
2. Usage
2.1 Entity Annotations – Example entity with @TableName , @TableId , @TableField and logical delete field isDelete .
@TableName("user")
public class UserDO {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@TableField("nickname")
private String nickname;
@TableField("sex")
private SexEnum sex;
@TableField(value = "create_time", fill = FieldFill.INSERT)
private LocalDateTime createTime;
}2.2 CRUD – Service interface extends IService , implementation extends ServiceImpl , and mapper extends BaseMapper .
public interface IUserService extends IService
{}
public class UserServiceImpl extends ServiceImpl
implements IUserService {}
public interface UserMapper extends BaseMapper
{}2.3 Pagination – Register MybatisPlusInterceptor with PaginationInnerInterceptor for MySQL.
@Configuration
@MapperScan("com.wjbgn.*.mapper*")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}2.4 Logical Delete – Global config or @TableLogic on isDelete field.
mybatis-plus:
global-config:
db-config:
logic-delete-field: isDelete
logic-delete-value: 1
logic-not-delete-value: 0or
@TableLogic
private Integer isDelete;2.5 Enum Mapping – Define enum implementing IEnum and use @JsonFormat for proper JSON output.
public enum SexEnum implements IEnum
{
MAN(1, "男"),
WOMAN(2, "女");
private Integer code;
private String name;
// getters omitted
}Configure typeEnumsPackage and a custom MybatisPlusPropertiesCustomizer to use MybatisEnumTypeHandler .
2.6 Automatic Field Filling – Implement MetaObjectHandler to set createTime and updateTime automatically.
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
}
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
}
}2.7 Multi‑Datasource – Show master‑slave, multiple DB types, and mixed configurations using @DS annotation on classes or methods.
@DS("slave_1")
public class UserServiceImpl extends ServiceImpl
implements IUserService {
@DS("master")
public int saveUser(UserDO userDO) { /* ... */ }
}3. Testing – Provide SQL to create the user table, a controller with endpoints for save, update, delete, list, and paginated list, and confirm that the service works as expected.
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
// /save, /update, /delete, /list, /page endpoints omitted for brevity
}The article ends with a link to the full source code repository.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.