当前位置:网站首页>Automatic filling of database public fields
Automatic filling of database public fields
2022-07-04 13:51:00 【YXXYX】
introduction
Database tables of many projects will be set create_time、update_time Other public fields , These public fields need to be set when the database is created or updated ; If you set it yourself, you need to write extra set Code , Once there are many such watches, it will be troublesome ; So we can find a way to make these fields automatically filled ; and mybatis-plus It just provides such a function ;
mysql You can set the default value of the field CURRENT_TIMESTAMP Add the current time every time you insert data , Update can be set on update CURRENT_TIMESTAMP Implement the update operation to update the time :
But I don't particularly recommend this method , If you let the database complete these logical operations, the responsibility is not clear , And it is not conducive to others to understand from the code ;
Here's how to use mybatis-plus Realize the automatic filling of public fields
Code operation
First you need to create a population data processor , Realization MetaObjectHandler:
/** * mybatis-plus Automatic attribute filling function provided */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
// Insert data auto fill
@Override
public void insertFill(MetaObject metaObject) {
log.info(" New data is automatically filled ...");
this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
}
// Update data auto fill
@Override
public void updateFill(MetaObject metaObject) {
log.info(" Update data auto fill ...");
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
}
}
The processor needs to be handed over to Sping management , add @Component annotation ;
Sometimes there will be createUser What fields , Add flexibly :
After this step is completed , Next, just add @TableField annotation :
@TableField(value = "create_time", fill = FieldFill.INSERT)
private Date createTime;
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
Mainly @TableField Annotated fill attribute :
- INSERT Fill in when inserting
- UPDATE Fill in on update
- INSERT_UPDATE Both update and insert are populated
- DEFAULT By default , Nothing is filled
In this way, the automatic filling is set , You can test :
The new data
Update data
In the database create_time and update_time They also have corresponding values :
In this way, the overall automatic filling operation, that is, configuration, is completed ;
Step on the pit diary : Automatic filling principle
There is a problem in writing this project : update operation update Auto fill does not take effect when ;
After investigation, it is found that the updated data comes from the front end update_time Field Already exist A time value ( I found this value from the database );
So I guess If the field to be automatically filled already has a value , Then the automatic filling mechanism will no longer fill the field with values ;
Because auto fill takes effect and strictUpdateFill() Methods matter ( Take update as an example ), So I debug Check its source code :
First of all, no matter whether there is a value in the automatic filling, it will enter the method :
Then enter the method :
Then through a round-trip call ( You can try it yourself , Don't show if it's not important ), Came to the following method :
This method can show the strategy of automatic filling : Only when the public fill field is empty, it will be automatically filled ; Briefly explain the key code :
- fieldName: Public fill field name
- fieldVal: Generated public fill field values
if (metaObject.getValue(fieldName) == null) {
// Judge whether the value of the public fill field is empty , If it is empty, it will be filled automatically , Not empty, end directly
Object obj = fieldVal.get(); // Get the automatically generated fill value
if (Objects.nonNull(obj)) {
// Judge whether the filling value is empty
metaObject.setValue(fieldName, obj); // Assign the filling value to this field , Realize the automatic filling of public fields
}
}
Of course, there are still some steps to follow , Here is just a list of the key steps ;
Then I put update_time The field is manually assigned a null value before being passed to the backend , Auto fill takes effect ;
summary
in general , Automatic filling of public fields is still very easy to use , Save a lot of code ; Of course, one thing to keep in mind : Make sure that the fields that need to be automatically filled are empty , Only in this way can the automatic filling mechanism take effect
Personal records , If you have any questions, please contact us !
边栏推荐
- 聊聊支付流程的设计与实现逻辑
- CANN算子:利用迭代器高效实现Tensor数据切割分块处理
- ViewBinding和DataBinding的理解和区别
- Haproxy high availability solution
- Samsung's mass production of 3nm products has attracted the attention of Taiwan media: whether it can improve the input-output rate in the short term is the key to compete with TSMC
- .Net之延迟队列
- C语言职工管理系统
- SQL language
- Introduction to XML II
- Node mongodb installation
猜你喜欢
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 06 | 全局锁和表锁_给表加个字段怎么有这么多阻碍
OpenHarmony应用开发之如何创建DAYU200预览器
Haproxy high availability solution
分布式BASE理论
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)
基于STM32+华为云IOT设计的酒驾监控系统
HAProxy高可用解决方案
ASP. Net core introduction I
2022kdd pre lecture | 11 first-class scholars take you to unlock excellent papers in advance
随机推荐
分布式BASE理论
"Tips" to slim down Seurat objects
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
Talk about the design and implementation logic of payment process
三星量产3纳米产品引台媒关注:能否短期提高投入产出率是与台积电竞争关键
实战:fabric 用户证书吊销操作流程
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
2022年起重机械指挥考试模拟100题模拟考试平台操作
OpenHarmony应用开发之如何创建DAYU200预览器
Haproxy high availability solution
2022年山东省安全员C证考试题库及在线模拟考试
unity不识别rider的其中一种解决方法
舔狗舔到最后一无所有(状态机)
js中的变量提升和函数提升
Xue Jing, director of insight technology solutions: Federal learning helps secure the flow of data elements
DGraph: 大规模动态图数据集
30:第三章:开发通行证服务:13:开发【更改/完善用户信息,接口】;(使用***BO类承接参数,并使用了参数校验)
Getting started with the go language is simple: go implements the Caesar password
Go zero micro service practical series (IX. ultimate optimization of seckill performance)