当前位置: 首页 > article >正文

公共字段自动填充-MyBatis-Plus

由于使用了MyBatis-Plus提供的方法操作数据库,所有无法直接使用AOP技术来在mapper方法执行前对公共字段赋值。

在 MyBatis-Plus 中,可以通过实现 MyBatis-Plus 提供的 MetaObjectHandler 接口来实现公共字段的自动填充,比如在插入或更新数据时,自动填充创建时间、更新时间等字段。

实现步骤:

  1. 创建一个类并实现 MetaObjectHandler 接口 该类用于实现自动填充的逻辑。

  2. 配置自动填充字段 在实体类中,通过注解来标识哪些字段需要自动填充。

1. 实现MetaObjectHandler 接口

创建实现 MetaObjectHandler 的类,该类用于在插入和更新时自动填充字段,比如创建时间、更新时间、创建用户、更新用户等。

// 标注为Spring组件,使其可以被Spring容器管理
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    // 插入时的自动填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        // 字段自动填充的逻辑,可以判断当前字段是否为空然后进行填充
        
        // 使用strictInsertFill方法填充"createTime"字段,填充类型为LocalDateTime,填充值为当前时间
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); 
        
        // 使用strictInsertFill方法填充"updateTime"字段,填充类型为LocalDateTime,填充值为当前时间
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); 
        
        // 使用strictInsertFill方法填充"createUser"字段,填充类型为Long,填充值为当前用户ID
        this.strictInsertFill(metaObject, "createUser", Long.class, BaseContext.getCurrentId());   
        
        // 使用strictInsertFill方法填充"updateUser"字段,填充类型为Long,填充值为当前用户ID
        this.strictInsertFill(metaObject, "updateUser", Long.class, BaseContext.getCurrentId());   
    }

    // 更新时的自动填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        // 使用strictUpdateFill方法填充"updateTime"字段,填充类型为LocalDateTime,填充值为当前时间
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); 
        
        // 使用strictUpdateFill方法填充"updateUser"字段,填充类型为Long,填充值为当前用户ID
        this.strictUpdateFill(metaObject, "updateUser", Long.class, BaseContext.getCurrentId());   
    }
}

解释:

  • insertFill:这个方法在插入数据时会被触发,用于自动填充createTimecreateUserupdateTime等字段。
  • updateFill:这个方法在更新数据时会被触发,用于自动填充updateTimeupdateUser字段。
  • BaseContext.getCurrentId():这是为了获取当前操作用户的ID,可以结合ThreadLocal进行存储与获取。

2. 在实体类中标记需要自动填充的字段

在实体类中的相关字段上使用@TableField注解来声明这些字段需要自动填充。

// 使用@Data注解自动生成getter、setter、toString、equals和hashCode方法
@Data
public class Employee {

    @TableField(fill = FieldFill.INSERT)  // 在插入时自动填充该字段
    private LocalDateTime createTime; // 创建时间字段,类型为LocalDateTime

    @TableField(fill = FieldFill.INSERT_UPDATE)  // 在插入和更新时自动填充该字段
    private LocalDateTime updateTime; // 更新时间字段,类型为LocalDateTime

    @TableField(fill = FieldFill.INSERT)  // 在插入时自动填充该字段
    private Long createUser; // 创建人ID字段,类型为Long

    @TableField(fill = FieldFill.INSERT_UPDATE)  // 在插入和更新时自动填充该字段
    private Long updateUser; // 更新人ID字段,类型为Long

    // 。。。其他属性略。。。
}

解释:

  • @TableField(fill = FieldFill.INSERT):表示在插入时自动填充该字段,例如 createTimecreateUser
  • @TableField(fill = FieldFill.INSERT_UPDATE):表示在插入和更新时都会自动填充该字段,例如 updateTimeupdateUser

3. 工作原理

  • 当你在执行 save 或者 update 操作时,MyBatis-Plus 会自动调用你实现的 MetaObjectHandler,检查并自动为指定字段填充默认值。
  • 使用 BaseContext.getCurrentId() 是为了获取当前操作的用户ID,配合 ThreadLocal,你可以在拦截器中设置用户ID信息,从而实现每个操作记录都是由特定的用户生成或更新的。

4. 总结

  1. 创建 MetaObjectHandler 实现类,负责自动填充字段逻辑。
  2. 在实体类中通过 @TableField 注解标记需要自动填充的字段。
  3. MyBatis-Plus 提供了 FieldFill.INSERTFieldFill.UPDATEFieldFill.INSERT_UPDATE 来区分字段在插入和更新时的自动填充需求。

http://www.kler.cn/news/357564.html

相关文章:

  • java servlet tomcat springboot 版本对照表
  • AI 编译器学习笔记之七五 -- pdb 使用方法
  • 【java】数组(超详细总结)
  • C++:反向迭代器
  • STMicroelectronics 意法半导体芯片选型表
  • mongoDB基础知识
  • QT 如何置顶窗口并激活
  • 4G、5G通信中,“网络侧“含义
  • 【Linux】命令行下的增删查改之“查看”
  • 基于SpringBoot的旅游网站的设计与实现
  • Scroll 生态首个 meme 项目 $Baggor,我们可以有哪些期待?
  • 集群与分布式
  • Lua变量
  • 学习eNSP对准备华为认证有哪些帮助?
  • TypeScript中 interface接口 type关键字 enum枚举类型
  • Spring Boot视频网站:安全与可扩展性设计
  • python车牌号OCR识别(centos版)
  • MySQL的安装(windows,Centos,ubuntu)
  • 《深度学习》OpenCV EigenFaces算法 人脸识别
  • 数据抓取时,使用动态IP要注意哪些?