Mybatis實現(xiàn)審計數(shù)據(jù)自動填充

Mybatis實現(xiàn)審計數(shù)據(jù)自動填充

在使用Spring Data JPA的時候,框架層面已經(jīng)提供了審計數(shù)據(jù)相關(guān)的入口,所以只需通過配置就可以實現(xiàn)審計數(shù)據(jù)的自動維護

Spring Data JPA審計數(shù)據(jù)配置

四個注解
  • @CreatedDate,創(chuàng)建時間
  • @CreatedBy,創(chuàng)建人
  • @LastModifiedDate,最后修改時間
  • @LastModifiedBy,最后修改人

Sample:

@Column(name = "created_date", nullable = false, updatable = false)
@CreatedDate
private Date createdDate;

@Column(name = "modified_by")
@LastModifiedBy
private String modifiedBy;
啟用配置
  • 啟用審計@EnableJpaAuditing
  • 配置AuditorProvider
@Configuration
@EnableJpaAuditing
public class JpaConfiguration{
    
    @Bean
    public AuditorAware<String> auditorProvider() {
        return new MyAuditor();
    }
   
    /**
     * 自定義AuditorAware,獲取當(dāng)前用戶名
     */
    public static class MyAuditor implements AuditorAware<String> {
        @Override
        public String getCurrentAuditor() {
            //獲取用戶名,如果使用SpringSecurity的話可以從SecurityContext中獲取
        }
    }
    
}

使用Mybatis實現(xiàn)審計數(shù)據(jù)自動填充

而在使用Mybatis的時候,Mybatis提倡的是簡單,官方也沒有類似的功能,這個時候如果業(yè)務(wù)需要審計數(shù)據(jù)的時候需要手工維護,這時候可以模仿JPA實現(xiàn)一個類似的功能,如下:

使用Mybatis Plus插件
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>${version}</version>
</dependency>
自定義實現(xiàn)自動填充Handler
public class AuditMetaObjectHandler extends MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        boolean createDate = metaObject.hasSetter("createDate");
        boolean updateDate = metaObject.hasSetter("updateDate");
        if (createDate || updateDate) {
            Date now = new Date();
            if (createDate) {
                this.setFieldValByName("createDate", now, metaObject);
            }
            if (updateDate) {
                this.setFieldValByName("updateDate", now, metaObject);
            }
        }
        String username = ...;//獲取用戶,如果使用SpringSecurity的話可以從SecurityContext中獲取
        if (metaObject.hasSetter("createUser")) {
            this.setFieldValByName("createUser", username, metaObject);
        }
        if (metaObject.hasSetter("updateUser")) {
            this.setFieldValByName("updateUser", username, metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasSetter("updateDate")) {
            this.setFieldValByName("updateDate", new Date(), metaObject);
        }
        if (metaObject.hasSetter("updateUser")) {
            String username = ...;//獲取用戶,如果使用SpringSecurity的話可以從SecurityContext中獲取
            this.setFieldValByName("updateUser", username, metaObject);
        }
    }
}
啟用配置

在MybatisPlusConfigurer配置中新增配置:

/**
* 審計數(shù)據(jù)插件
*
* @return AuditMetaObjectHandler
*/
@Bean
@ConditionalOnMissingBean(name = "auditMetaObjectHandler")
public AuditMetaObjectHandler auditMetaObjectHandler() {
    return new AuditMetaObjectHandler();
}
配置實體類
/**
  * 創(chuàng)建時間
  */
@TableField(value = "create_date", fill = FieldFill.INSERT)
private Date createDate;

/**
  * 創(chuàng)建用戶
  */
@TableField(value = "create_user", fill = FieldFill.INSERT)
private String createUser;

/**
 * 更新時間
 */
@TableField("update_date", fill = FieldFill.INSERT_UPDATE)
private Date updateDate;

/**
 * 更新用戶
 */
@TableField("update_user", fill = FieldFill.INSERT_UPDATE)
private String updateUser;

總結(jié)

這樣就可以實現(xiàn)類似Spring Data JPA的審計功能

參考

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容