您现在的位置是: 首页 > 后端开发 记一次 Mybatis-Plus 自动填充无效问题解决
记一次 Mybatis-Plus 自动填充无效问题解决
2020-05-27 【后端开发】 6169人已围观 10866次浏览
简介记一次 Mybatis-Plus 自动填充无效问题解决
当前使用的版本
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
需要实现的是添加时间和修改时间自动更新维护,参考官方文档,在项目中新增了自定义实现类 MyMetaObjectHandler (部分不需要的代码已经删除)
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
log.info("start insert fill ....");
this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
}
@Override
public void updateFill(MetaObject metaObject) {
log.info("start update fill ....");
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
}
}
然后在实体类中,新增注解
public class User {
// 注意!这里需要标记为填充字段
@TableField(fill = FieldFill.INSERT)
private String createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private String updateTime;
....
}
因为不想数据库中存在 null 值的数据,所以 updateTime 修改为 INSERT_UPDATE,根据官网的说明,该注解应该是在插入和更新时,都会自动填充该字段数据
public enum FieldFill {
/**
* 默认不处理
*/
DEFAULT,
/**
* 插入填充字段
*/
INSERT,
/**
* 更新填充字段
*/
UPDATE,
/**
* 插入和更新填充字段
*/
INSERT_UPDATE
}
但在实际使用的过程中发现,执行 save 方法,并不会自动填充 updateTime 字段,也就是数据库中 updateTime 字段为 null ,执行 update 方法后,updateTime 才自动填充上数据。
一开始以为是源码有问题,后来查看源码和参考官方的示例后发现,如果字段注解设置为 INSERT_UPDATE ,则不仅需要在 updateFill 上添加,同时也需要在 insertFill 上添加,修改如下
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
log.info("start insert fill ....");
this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
}
@Override
public void updateFill(MetaObject metaObject) {
log.info("start update fill ....");
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
}
}
这样才能做到在执行 save 方法时,也自动填充 updateTime 字段。
仅以此文记录自己犯下的错误,吸取教训。
很赞哦! (0)
相关文章
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR DISABLE You can't write or read against a disable instance
- SqlServer 刷新所有视图
- SqlServer 优化技巧
- 删除Git仓库所有历史提交记录,成为一个干净的仓库
- RocketMQ 出现 sendDefaultImpl call timeout 问题
- SpringBoot 2.x Security security.basic.enabled=false 失效问题解决
- PHP 在执行 composer install 时出现提示 PHP Fatal error: Allowed memory size of XXXXXX bytes exhausted <...>
- Laravel Dingo/api 出现 The version given was unknown or has no registered routes.报错
- 类 BASE64Decoder 程序包 sun.misc 找不到符号
- Laravel项目出现could not be opened: failed to open stream: Permission denied
点击排行
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR DISABLE You can't write or read against a disable instance
- RocketMQ 出现 sendDefaultImpl call timeout 问题
- Debian apt 使用国内镜像
- 类 BASE64Decoder 程序包 sun.misc 找不到符号
- SpringBoot @NotBlank 不生效问题
- 记一次 Mybatis-Plus 自动填充无效问题解决
- nuxt 项目完整部署方案
- SpringBoot 2.x 文件上传出现 The field file exceeds its maximum permitted size of 1048576 bytes
站长推荐
猜你喜欢
- 【Docker】unauthorized: incorrect username or password
- 解决Lost connection to MySQL server at 'reading initial communication packet' 的方法
- docker mysql 导入 SQL 脚本
- RocketMQ 出现 sendDefaultImpl call timeout 问题
- Android SDK版本号与API级别的对照表
- PHP 在执行 composer install 时出现提示 PHP Fatal error: Allowed memory size of XXXXXX bytes exhausted <...>
- Linux 安装 Tengine
- 迁移 Docker 目录
- Win10 安装适用于Linux的Windows子系统
- Linux 服务器之间传输大文件(压缩文件、查看MD5、后台传输)