已经是最新一篇文章了!
已经是最后一篇文章了!
Mybatis-Plus数据审计
晨昏滚滚水东流,今古悠悠日西坠。
Mybatis 审计功能(数据自动填充)
为实体的一些公共字段进行自动赋值,一般是一些审计字段: createdBy、createdTime、updatedBy、updatedTime
创建抽象审计类
/**
* @author pi'ka'chu
*/
@Data
public abstract class AbstractAuditBase {
@JsonIgnore
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createdTime;
@JsonIgnore
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updatedTime;
@JsonIgnore
@TableField(fill = FieldFill.INSERT)
private String createdBy;
@JsonIgnore
@TableField(fill = FieldFill.INSERT_UPDATE)
private String updatedBy;
}
继承抽象审计类
创建表
DROP TABLE IF EXISTS `t_balance`;
CREATE TABLE `t_balance` (
`id` int UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`balance` decimal(50, 2) NULL DEFAULT NULL,
`updated_time` datetime NULL DEFAULT NULL,
`created_time` datetime NULL DEFAULT NULL,
`created_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`updated_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `name`(`name` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;
SET FOREIGN_KEY_CHECKS = 1;
创建实体类
@TableName(value = "t_balance", autoResultMap = true)
@AllArgsConstructor
@NoArgsConstructor
@ToString(callSuper = true)
@Data
public class Balance extends AbstractAuditBase implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField(value = "name")
private String name;
@TableField(value = "balance")
private BigDecimal balance;
public Balance(String name, BigDecimal balance) {
this.name = name;
this.balance = balance;
}
public Balance(Integer id, BigDecimal balance) {
this.id = id;
this.balance = balance;
}
}
实现 MetaObjectHandler 接口
MetaObjectHandler 接口描述
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
private final String createdBy = "createdBy";
private final String createdTime = "createdTime";
private final String updatedBy = "updatedBy";
private final String updatedTime = "updatedTime";
@Override
public void insertFill(MetaObject metaObject) {
if (metaObject.hasSetter(createdBy)) {
this.setFieldValByName(createdBy, "SYSTEM", metaObject);
}
if (metaObject.hasSetter(createdTime)) {
this.setFieldValByName(createdTime, LocalDateTime.now(), metaObject);
}
if (metaObject.hasSetter(updatedBy)) {
this.setFieldValByName(updatedBy, "SYSTEM", metaObject);
}
if (metaObject.hasSetter(updatedTime)) {
this.setFieldValByName(updatedTime, LocalDateTime.now(), metaObject);
}
}
@Override
public void updateFill(MetaObject metaObject) {
if (metaObject.hasSetter(updatedBy)) {
this.setFieldValByName(updatedBy, "SYSTEM", metaObject);
}
if (metaObject.hasSetter(updatedTime)) {
this.setFieldValByName(updatedTime, LocalDateTime.now(), metaObject);
}
}
}
版权声明:如无特别声明,本站收集的文章归 HuaJi66/Others 所有。 如有侵权,请联系删除。
联系邮箱: GenshinTimeStamp@outlook.com
本文标题:《 Mybatis-Plus数据审计 》
本文链接:/mybatis/Mybatis%E5%AE%9E%E7%8E%B0%E6%95%B0%E6%8D%AE%E5%AE%A1%E8%AE%A1.html