当前位置:网站首页>数据库公共字段自动填充
数据库公共字段自动填充
2022-08-10 06:49:00 【JJpZh】
1. 公共字段自动填充
1.1 问题分析
前面我们已经完成了后台系统的员工管理功能的开发,在新增员工时需要设置创建时间、创建人、修改时间、修改人等字段,在编辑员工时需要设置修改时间、修改人等字段。这些字段属于公共字段,也就是也就是在我们的系统中很多表中都会有这些字段
而针对于这些字段,我们的赋值方式为:
A. 在新增数据时, 将createTime、updateTime 设置为当前时间, createUser、updateUser设置为当前登录用户ID。
B. 在更新数据时, 将updateTime 设置为当前时间, updateUser设置为当前登录用户ID。
目前,在我们的项目中处理这些字段都是在每一个业务方法中进行赋值操作,如下:
如果都按照上述的操作方式来处理这些公共字段, 需要在每一个业务方法中进行操作, 编码相对冗余、繁琐,那能不能对于这些公共字段在某个地方统一处理,来简化开发呢?
答案是可以的,我们使用Mybatis Plus提供的公共字段自动填充功能。
1.2 基本功能实现
1.2.1 思路分析
Mybatis Plus公共字段自动填充,也就是在插入或者更新的时候为指定字段赋予指定的值,使用它的好处就是可以统一对这些字段进行处理,避免了重复代码。在上述的问题分析中,我们提到有四个公共字段,需要在新增/更新中进行赋值操作, 具体情况如下:
| 字段名 | 赋值时机 | 说明 |
|---|---|---|
| createTime | 插入(INSERT) | 当前时间 |
| updateTime | 插入(INSERT) , 更新(UPDATE) | 当前时间 |
| createUser | 插入(INSERT) | 当前登录用户ID |
| updateUser | 插入(INSERT) , 更新(UPDATE) | 当前登录用户ID |
实现步骤:
1、在实体类的属性上加入@TableField注解,指定自动填充的策略。
2、按照框架要求编写元数据对象处理器,在此类中统一为公共字段赋值,此类需要实现MetaObjectHandler接口。
1.2.2 代码实现
1). 实体类的属性上加入@TableField注解,指定自动填充的策略。
在员工Employee实体类的公共字段属性上, 加上注解, 指定填充策略。

FieldFill.INSERT: 插入时填充该属性值
FieldFill.INSERT_UPDATE: 插入/更新时填充该属性值
2). 按照框架要求编写元数据对象处理器,在此类中统一为公共字段赋值,此类需要实现MetaObjectHandler接口。
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
/** * 自定义元数据对象处理器 */
@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler {
/** * 插入操作,自动填充 * @param metaObject */
@Override
public void insertFill(MetaObject metaObject) {
log.info("公共字段自动填充[insert]...");
log.info(metaObject.toString());
metaObject.setValue("createTime", LocalDateTime.now());
metaObject.setValue("updateTime",LocalDateTime.now());
metaObject.setValue("createUser",new Long(1));
metaObject.setValue("updateUser",new Long(1));
}
/** * 更新操作,自动填充 * @param metaObject */
@Override
public void updateFill(MetaObject metaObject) {
log.info("公共字段自动填充[update]...");
log.info(metaObject.toString());
metaObject.setValue("updateTime",LocalDateTime.now());
metaObject.setValue("updateUser",new Long(1));
}
}
1.2.3 功能测试
编写完了元数据对象处理器之后,我们就可以将之前在新增和修改方法中手动赋值的代码删除或注释掉。

然后,我们启动项目,在员工管理模块中,测试增加/更新员工信息功能,然后通过debug 或者 直接查询数据库数据变更的形式,看看我们在新增/修改数据时,这些公共字段数据是否能够完成自动填充。
边栏推荐
- VS Code插件国际化
- 数据库学习之表的约束
- SCS【2】单细胞转录组 之 cellranger
- delta method 介绍
- 大佬,oracle单表增量同步时候源库服务器额外占用内存近2g,这不正常吧
- I would like to ask you guys, when FLink SQL reads the source, specify the time field of the watermark. If the specified field is in the grid
- MySQL之InnoDB引擎(六)
- 强化学习_11_Datawhale模仿学习
- 深入理解数组
- ctfshow SSTI 知识点总结
猜你喜欢
随机推荐
WooCommerce installation and rest api usage
Elementary Structure
基于STC8G2K64S4单片机通过OLED屏幕显示模拟量光敏模拟值
强化学习_07_DataWhale深度Q网络进阶技巧
MySQL's InnoDB engine (6)
MySQL设置初始密码—注意版本mysql-8.0.30
Grammar Basics (Judgment Statements)
navicat for mysql 连接时报错:1251-Client does not support authentication protocol requested by server
tqdm高级使用方法(类keras进度条)
椭圆曲线离散对数问题以及求解
ATH10传感器读取温湿度
initramfs与initrd的区别
Discussion on Chinese Fuzzy Retrieval in Databases
关于MongoDb查询Decimal128转BigDecimal问题
ES13 - ES2022 - 第 123 届 ECMA 大会批准了 ECMAScript 2022 语言规范
强化学习_06_pytorch-DQN实践(CartPole-v0)
I would like to ask you guys, when FLink SQL reads the source, specify the time field of the watermark. If the specified field is in the grid
排序二叉树代码
All articles summary directory
C language file operation








