当前位置:网站首页>数据库公共字段自动填充
数据库公共字段自动填充
2022-07-04 12:44:00 【YXXYX】
引言
很多项目的数据库表都会设置create_time、update_time等公共字段,这些公共字段都是在数据库创建或更新时需要设置值;如果自己设置还需要额外写set代码,一旦这样的表多了之后就很麻烦;所以我们可以想办法让这些字段可以自动填充;而mybatis-plus正好提供了这样的功能;
mysql中可以通过设置字段默认值CURRENT_TIMESTAMP实现每次插入数据添加当前时间,更新可以设置on update CURRENT_TIMESTAMP实现更新操作更新该时间:
但是我并不是特别推荐这种方法,如果让数据库完成这些逻辑操作职责就不太明确,并且不利于他人从代码中理解;
下面演示一下如何使用mybatis-plus实现公共字段自动填充
代码操作
首先需要创建一个填充数据处理器,实现MetaObjectHandler:
/** * mybatis-plus提供的属性自动填充功能 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
// 插入数据自动填充
@Override
public void insertFill(MetaObject metaObject) {
log.info("新增数据自动填充...");
this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
}
// 更新数据自动填充
@Override
public void updateFill(MetaObject metaObject) {
log.info("更新数据自动填充...");
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
}
}
该处理器需要交给Sping管理,加上@Component注解;
有时会有createUser什么的字段,灵活添加即可:
这一步完成后,接下来只需要在公共填充的字段上加上 @TableField 注解:
@TableField(value = "create_time", fill = FieldFill.INSERT)
private Date createTime;
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
主要就是@TableField注解的fill属性:
- INSERT插入时填充
- UPDATE更新时填充
- INSERT_UPDATE更新和插入时都填充
- DEFAULT默认情况,什么都不填充
这样自动填充就设置好了,可以测试一下:
新增数据
更新数据
数据库中create_time和update_time也都有了对应值:
这样整体的自动填充操作即配置就完成了;
踩坑日记:自动填充原理
这次写项目中遇到了一个问题:更新操作update时自动填充没有生效;
经过排查发现前端传来更新的数据时update_time字段已经存在一个时间值(这个值是我从数据库中查出来的);
所以我猜测如果需要自动填充的字段已经存在值的话,那么自动填充机制就不会再给该字段填充值;
因为自动填充生效和strictUpdateFill()方法有关系(以更新为例),所以就debug了一下它的源码:
首先不论填充自动是否存在数值都会进入该方法:
然后进入该方法:
然后通过了一番来回调用(可以自己尝试看看,不重要就不展示了),来到了如下方法:
这个方法就可以看出来自动填充的策略了:只有公共填充字段为空时才会自动填充;简单解释一下关键代码:
- fieldName:公共填充字段名
- fieldVal:生成的公共填充字段值
if (metaObject.getValue(fieldName) == null) {
// 判断公共填充字段的值是否为空,如果为空才自动填充,不为空直接结束
Object obj = fieldVal.get(); // 获取自动生成的填充值
if (Objects.nonNull(obj)) {
// 判断填充值是否为空
metaObject.setValue(fieldName, obj); // 将填充值赋给该字段,实现了公共字段自动填充
}
}
当然后面还有一些步骤,这里只是把关键一步列了出来;
然后我在前端把update_time字段传给后端之前手动赋了空值,自动填充就生效了;
总结
总的来说,公共字段自动填充还是很好用的,省去很多代码;当然需要牢记一点:一定要保证需要自动填充的字段为空,这样自动填充机制才生效
个人记录,如果有问题欢迎交流!
边栏推荐
- Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle
- Agile development / agile testing experience
- A data person understands and deepens the domain model
- Comprehensive evaluation of modular note taking software: craft, notation, flowus
- "Tips" to slim down Seurat objects
- C語言宿舍管理查詢軟件
- The only core indicator of high-quality software architecture
- Rsyslog configuration and use tutorial
- Alibaba cloud award winning experience: build a highly available system with polardb-x
- PostgreSQL 9.1 soaring Road
猜你喜欢
After installing vscode, the program runs (an include error is detected, please update the includepath, which has been solved for this translation unit (waveform curve is disabled) and (the source fil
HAProxy高可用解决方案
高效!用虚拟用户搭建FTP工作环境
Valentine's Day confession code
JVM系列——栈与堆、方法区day1-2
Read the BGP agreement in 6 minutes.
Talk about the design and implementation logic of payment process
8 expansion sub packages! Recbole launches 2.0!
提高MySQL深分页查询效率的三种方案
Reptile exercises (I)
随机推荐
请问大佬们有遇到这个情况吗,cdc 1.4 连接MySQL 5.7 无法使用 timestamp
C语言宿舍管理查询软件
在 Apache 上配置 WebDAV 服务器
聊聊支付流程的设计与实现逻辑
HAProxy高可用解决方案
Agile development / agile testing experience
Besides, rsync+inotify realizes real-time backup of data
C basic supplement
读《认知觉醒》
AI painting minimalist tutorial
Xue Jing, director of insight technology solutions: Federal learning helps secure the flow of data elements
Dgraph: large scale dynamic graph dataset
洞见科技解决方案总监薛婧:联邦学习助力数据要素安全流通
Scripy framework learning
PostgreSQL 9.1 soaring Road
Apache server access log access Log settings
室外LED屏幕防水吗?
Alibaba cloud award winning experience: build a highly available system with polardb-x
C#基础深入学习二
Scrapy 框架学习