Master MyBatis-Plus: Quick Start, Core Features, and Best Practices
This guide walks through adding MyBatis-Plus to a Spring Boot project, configuring basic settings, using CRUD operations, condition builders, pagination, advanced features like auto‑fill, logical delete, optimistic locking, code generation, common pitfalls, and practical recommendations for effective usage.
Introduction
MyBatis-Plus (MP) is an enhancement tool for MyBatis that adds features without changing core behavior, greatly simplifying CRUD development.
Quick Start
1. Add Maven Dependency
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.5</version>
</dependency>✅ Pin the version to avoid breaking changes.
2. Basic Configuration (application.yml)
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # SQL log output
global-config:
db-config:
id-type: auto # primary key strategy
logic-delete-field: deleted # logical delete column
logic-delete-value: 1 # value when deleted
logic-not-delete-value: 0 # value when not deletedCore Features
1. Basic CRUD
@Autowired
private UserMapper userMapper;
// Insert
User user = new User();
user.setName("张三").setAge(20);
userMapper.insert(user);
// Select
User u = userMapper.selectById(1L);
// Update
u.setName("李四");
userMapper.updateById(u);
// Delete
userMapper.deleteById(1L);
// Select all
List<User> users = userMapper.selectList(null);2. Condition Builder
QueryWrapper example:
QueryWrapper<User> qw = new QueryWrapper<>();
qw.eq("name","张三")
.gt("age",18)
.orderByDesc("create_time");
List<User> users = userMapper.selectList(qw);LambdaQueryWrapper (recommended):
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.eq(User::getName,"张三")
.gt(User::getAge,18)
.orderByDesc(User::getCreateTime);
List<User> users = userMapper.selectList(lqw);✅ Lambda avoids hard‑coded column names, making refactoring safer.
3. Pagination Plugin
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
Page<User> page = new Page<>(1,10);
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>().gt(User::getAge,20);
IPage<User> result = userMapper.selectPage(page,lqw);
System.out.println("Total pages: "+result.getPages());
System.out.println("Total records: "+result.getTotal());Advanced Features
1. Automatic Field Filling
@Data
public class User {
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject){
this.strictInsertFill(metaObject,"createTime",Date.class,new Date());
this.strictInsertFill(metaObject,"updateTime",Date.class,new Date());
}
@Override
public void updateFill(MetaObject metaObject){
this.strictUpdateFill(metaObject,"updateTime",Date.class,new Date());
}
}2. Logical Delete
@TableLogic
private Integer deleted;3. Optimistic Lock
@Version
private Integer version;
@Bean
public MybatisPlusInterceptor mpInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}4. Code Generator
FastAutoGenerator.create("jdbc:mysql://localhost:3306/db","root","password")
.globalConfig(builder -> builder.author("ring")
.outputDir(System.getProperty("user.dir")+"/src/main/java")
.enableSwagger()
.fileOverride())
.packageConfig(builder -> builder.parent("com.example.mp")
.moduleName("system")
.pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir")+"/src/main/resources/mapper")))
.strategyConfig(builder -> builder.addInclude("user")
.addTablePrefix("t_"))
.execute();Common Issues
Field name does not match database column
@TableField("db_column_name")
private String entityField;Exclude non‑database fields
@TableField(exist = false)
private String tempData;Complex SQL
Use XML mapper
Or annotations @Select, @Update, etc.
Write multi‑table JOIN in XML
Custom SQL
@Select("SELECT * FROM user WHERE age > #{age}")
List<User> selectUsersOlderThan(@Param("age") Integer age);
// XML version in resources/mapper/UserMapper.xmlBest‑Practice Recommendations
Simple CRUD – use MP built‑in methods.
Complex queries – combine Wrapper with custom SQL.
Field safety – prefer Lambda expressions.
Plugins first – enable official pagination, optimistic lock, logical delete.
Regularly upgrade MP and monitor release notes.
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.
Ray's Galactic Tech
Practice together, never alone. We cover programming languages, development tools, learning methods, and pitfall notes. We simplify complex topics, guiding you from beginner to advanced. Weekly practical content—let's grow 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.
