Master MyBatis-Plus with Spring Boot: CRUD, Pagination & Multi-DataSource Guide
A comprehensive step‑by‑step tutorial shows how to integrate MyBatis‑Plus into a Spring Boot project, covering dependencies, configuration, entity annotations, CRUD operations, pagination, logical deletion, enum handling, automatic field filling, multi‑datasource support, and testing.
MyBatis-Plus Quick Start with Spring Boot
This article provides a step‑by‑step guide to integrate MyBatis‑Plus into a Spring Boot project, covering environment preparation, Maven dependencies, configuration of the data source, mapper scanning, and application startup.
Dependency and Configuration
<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>Configure the primary MySQL datasource and enable the dynamic‑datasource starter for master‑slave setups.
Entity Annotations
@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;
}Common MyBatis‑Plus annotations such as @TableName, @TableId, @TableField, and @TableLogic are explained.
CRUD Operations
Service interfaces extend IService<UserDO> and implementations extend ServiceImpl<UserMapper,UserDO>. Mapper interfaces extend BaseMapper<UserDO>, providing out‑of‑the‑box CRUD methods.
Pagination
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}Logical Delete
mybatis-plus:
global-config:
db-config:
logic-delete-field: isDelete
logic-delete-value: 1
logic-not-delete-value: 0Enum Mapping
public enum SexEnum implements IEnum<Integer> {
MAN(1, "男"),
WOMAN(2, "女");
private final Integer code;
private final String name;
@Override public Integer getValue() { return code; }
public String getName() { return name; }
}Automatic Field Filling
@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());
}
}Multi‑DataSource Support
Configuration examples for master‑slave, multiple databases, and mixed setups are shown, and the @DS annotation can be placed on classes or methods to select a datasource.
Testing
A sample controller demonstrates create, update, delete, list and paginated queries, confirming that the integration works as expected.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.
