MyBatis-Plus公共字段自动填充
一、准备工作
- 在SpringBoot项目中导入必要的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</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>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
- 配置yml文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/metaobjecthandler?serverTimezone=GMT%2B8
username: root
password: root
- 创建数据库和表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` varchar(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '主键ID',
`name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '姓名',
`age` int(0) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '邮箱',
`gmt_create` datetime(0) DEFAULT NULL COMMENT '创建时间',
`gmt_modified` datetime(0) DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
- 使用MyBatisX插件生成实体类,mapper和server
箭头指向的是生成的代码
在SpringBoot启动类上加上MapperScan注解指定Mapper扫描
- 在User类的公共自动上加入注解
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;
二、编写自动填充规则
创建MyMetaObjectHandler类实现MetaObjectHandler接口,重写insertFill和updateFill方法
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "gmtCreate", Date.class, new Date());
this.strictUpdateFill(metaObject, "gmtModified", Date.class, new Date());
}
@Override
public void updateFill(MetaObject metaObject) {
try {
if(metaObject.hasSetter("gmtModified")){
metaObject.setValue("gmtModified", null);
}
} catch (Exception e) {
e.printStackTrace();
}
this.strictUpdateFill(metaObject, "gmtModified", Date.class, new Date());
}
}
注意点:
- 重写的insertFill()和updateFill()方法分别是在插入和更新时执行的
- strictInsertFill()和strictUpdateFill()方法是在3.3.0+版本中可使用
- 选择填充的字段是实体类的属性,不是数据库字段名
MetaObjectHandler 提供的默认方法的策略均为:如果属性有值则不覆盖,如果填充值为null 则不填充,所以在updateFill()方法中,要先将原先字段设为空,再进行自动填充,否则在该字段有值时执行更新也不能修改该字段的值;方法2通用自动填充方法没有这个问题
三、编写测试类
@SpringBootTest
class MetaobjecthandlerApplicationTests {
@Autowired
private UserService userService;
@Test
public void testInsert() {
User user = new User();
user.setName("张三");
user.setAge(24);
user.setEmail("110@qq.com");
userService.save(user);
User byId = userService.getById(user.getId());
System.out.println(user);
}
@Test
public void testUpdate() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name","张三");
User user1 = userService.getOne(wrapper);
user1.setAge(36);
userService.updateById(user1);
User user2 = userService.getOne(wrapper);
System.out.println(user2);
}
}
|