您现在的位置是: 首页 > 后端开发 记一次 Mybatis-Plus 自动填充无效问题解决
记一次 Mybatis-Plus 自动填充无效问题解决
2020-05-27 【后端开发】 8242人已围观 15271次浏览
简介记一次 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)
相关文章
- SpringBoot 2.x Security security.basic.enabled=false 失效问题解决
- SpringBoot @NotBlank 不生效问题
- 获取阿里云CDN真实IP
- mac idea spring boot 启动慢
- SqlServer 优化技巧
- start.spring.io访问太慢,构建SpringBoot项目失败
- PHP 中list()出现Undefined offset: 0错误
- springboot引入mybatis-plus后出现ClassNotFoundException: org.mybatis.logging.LoggerFactory
- SpringBoot 定时任务 多线程
- SpringBoot 2.x 文件上传出现 The field file exceeds its maximum permitted size of 1048576 bytes
点击排行
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR DISABLE You can't write or read against a disable instance
- Debian apt 使用国内镜像
- RocketMQ 出现 sendDefaultImpl call timeout 问题
- 类 BASE64Decoder 程序包 sun.misc 找不到符号
- SpringBoot @NotBlank 不生效问题
- 记一次 Mybatis-Plus 自动填充无效问题解决
- SpringBoot 2.x 文件上传出现 The field file exceeds its maximum permitted size of 1048576 bytes
- nuxt 项目完整部署方案
站长推荐
猜你喜欢
- SpringBoot 集成 Elasticsearch 7.8.1 出现错误 Factory method 'elasticsearchRestHighLevelClient' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS
- SpringBoot 启动测试时出现提示 Test class should have exactly one public zero-argument constructor
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR DISABLE You can't write or read against a disable instance
- win10 cmd下运行python命令弹出windows应用商店问题
- Python 钉钉加签 HmacSHA256 算法签名
- Vue 报错 Avoid using non-primitive value as key, use string/number value instead
- 【Docker】unauthorized: incorrect username or password
- CentOS 安装Python3
- Oracle ORA-12541:TNS:no listener错误解决方法
- 自建Ngrok服务端