How to Integrate MyBatis-Plus into Spring Boot for CRUD and Pagination
This guide walks through adding MyBatis-Plus to a Spring Boot project, covering Maven dependencies, datasource configuration, mapper scanning, pagination plugin setup, entity and mapper definitions, and a unit test that demonstrates pagination and the rich CRUD methods provided by BaseMapper.
Dependency Setup
Add the MyBatis‑Plus Spring Boot starter and the required libraries to pom.xml. The starter bundles MyBatis‑Plus core, while the other dependencies provide web support, MySQL connectivity, Lombok, and test utilities.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis‑Plus starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>Database Configuration
Configure the datasource in application.properties (or application.yml) so that Spring Boot can obtain a MySQL connection.
spring.datasource.url=jdbc:mysql://localhost:3306/spring?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=genesis_123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.DriverMapper Scanning
Enable automatic detection of MyBatis mapper interfaces by adding @MapperScan to the main application class and specifying the package that contains the mapper interfaces.
@SpringBootApplication
@MapperScan("com.secbro.mapper")
public class SpringBootMainApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMainApplication.class, args);
}
}Pagination Interceptor Configuration
MyBatis‑Plus provides pagination through the PaginationInterceptor. Register it as a Spring bean so that all queries can use the built‑in pagination methods.
@Configuration
@ConditionalOnClass(PaginationInterceptor.class)
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}Entity and Mapper Definition
Define a POJO that maps to a database table. If the class name does not match the table name, use @TableName to specify the table.
@Data
@TableName("tb_order")
public class Order {
private int id;
private String orderNo;
private int amount;
}
public interface OrderMapper extends BaseMapper<Order> {}BaseMapper Overview
BaseMapper<T>is the core MyBatis‑Plus interface that supplies a comprehensive set of CRUD and pagination methods without writing SQL. Key methods include: int insert(T entity) – insert a new record. int deleteById(Serializable id) – delete by primary key. int updateById(T entity) – update a record identified by its primary key. T selectById(Serializable id) – retrieve a single record by primary key. List<T> selectList(Wrapper<T> queryWrapper) – query a list with optional conditions.
<E extends IPage<T>> E selectPage(E page, Wrapper<T> queryWrapper)– perform a paginated query.
Additional batch, map‑based, and count operations are also available.
Unit Test for Pagination
Use @SpringBootTest to load the Spring context, inject OrderMapper, and verify that pagination works as expected.
@Slf4j
@SpringBootTest
class OrderMapperTest {
@Resource
private OrderMapper orderMapper;
@Test
void queryByPage() {
// Create a Page object: first page, 2 records per page
IPage<Order> orderPage = new Page<>(1, 2);
// Execute the paginated query (null means no additional conditions)
orderPage = orderMapper.selectPage(orderPage, null);
List<Order> list = orderPage.getRecords();
for (Order order : list) {
System.out.println(order);
}
// The IPage object also provides total record count, total pages, etc.
}
}Running the test prints the retrieved Order objects and confirms that the pagination interceptor is correctly applied.
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.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
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.
