Master MyBatis-Plus with Spring Boot: CRUD, Pagination & Advanced Tips
This comprehensive tutorial demonstrates how to integrate MyBatis-Plus with Spring Boot, covering basic setup, entity and mapper creation, CRUD operations, condition wrappers, pagination, logical deletion, automatic field filling, optimistic locking, and advanced features such as multi‑tenant and dynamic table name handling, complete with code examples.
Introduction
MyBatis-Plus is an enhancement tool for MyBatis that simplifies development and improves efficiency. This guide shows how to use MyBatis-Plus (mp) together with Spring Boot.
Quick Start
Create a Spring Boot project and add the mybatis-plus-boot-starter dependency. Configure the datasource in application.yml and enable the mapper scan with @MapperScan("com.example.mp.mappers").
Entity and Mapper
Define an entity class User with fields such as id, name, age, email, managerId, and createTime. Create a mapper interface that extends BaseMapper<User>.
Basic CRUD
Insert: userMapper.insert(user) Delete by ID: userMapper.deleteById(id) Update by ID: userMapper.updateById(user) Select by ID: userMapper.selectById(id) Select list with wrapper:
userMapper.selectList(wrapper)Condition Wrapper
Use QueryWrapper or LambdaQueryWrapper to build WHERE clauses. Methods such as eq, like, gt, between, or, and nested allow complex conditions. Example:
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper.like(User::getName, "王").or(w -> w.lt(User::getAge, 30).isNotNull(User::getEmail));Pagination
Configure a PaginationInnerInterceptor in MybatisPlusConfig. Use Page<User> page = new Page<>(current, size) and call userMapper.selectPage(page, wrapper). Set isSearchCount to false to skip total count.
Active Record (AR) Mode
Make the entity extend Model<User>. Then call user.insert(), user.selectById(), user.updateById(), or user.deleteById() directly.
Logical Deletion
Configure logical delete fields in application.yml (e.g., logic-delete-field: deleted). Annotate the field with @TableLogic or rely on global settings. Deleting a record updates the flag instead of issuing a DELETE statement.
Automatic Field Filling
Mark fields with @TableField(fill = FieldFill.INSERT) or FieldFill.UPDATE. Implement MetaObjectHandler to set values such as timestamps.
Optimistic Locking
Add OptimisticLockerInnerInterceptor to the interceptor chain and annotate a version field with @Version. Updates include WHERE version = ? to prevent lost updates.
Advanced Plugins
Performance analysis via P6Spy (configure spy.properties and replace the driver with com.p6spy.engine.spy.P6SpyDriver).
Multi‑tenant support using TenantLineInnerInterceptor and a custom TenantLineHandler that appends a tenant condition to every query.
Dynamic table name handling with DynamicTableNameInnerInterceptor and a TableNameHandler that rewrites the table name at runtime.
Conclusion
MyBatis-Plus provides a rich set of features for rapid backend development, including concise CRUD APIs, powerful condition builders, pagination, AR mode, logical deletion, auto‑fill, optimistic locking, and extensible plugins for performance monitoring, multi‑tenant, and dynamic tables.
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 Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
