您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页SpringDataJpa使用审计(Auditing)功能

SpringDataJpa使用审计(Auditing)功能

来源:伴沃教育

SpringBoot项目使用SpringDataJpa提供的审计功能的使用流程

SpringDataJpa提供审计注解:@CreatedBy,@LastModifiedBy,@CreatedDate,@LastModifiedDate

第一步:在SpringBoot启动类上添加@EnableJpaAuditing

 

public @interface EnableJpaAuditing {

    /**
     * Configures the {@link AuditorAware} bean to be used to lookup the current principal.
     *
     * @return
     */
        // 当SpringIOC容器中注册了多个审计的Bean,需要指定Bean的名称
    String auditorAwareRef() default "";

    /**
     * Configures whether the creation and modification dates are set. Defaults to {@literal true}.
     *
     * @return
     */
    boolean setDates() default true;

    /**
     * Configures whether the entity shall be marked as modified on creation. Defaults to {@literal true}.
     *
     * @return
     */
    boolean modifyOnCreate() default true;

    /**
     * Configures a {@link DateTimeProvider} bean name that allows customizing the {@link org.joda.time.DateTime} to be
     * used for setting creation and modification dates.
     *
     * @return
     */
    String dateTimeProviderRef() default "";
}

第二步:在实体类上进行添加注解的标注字段

第三步:实现 AuditorAware 接口,泛型T是返回的限定类型,并注册到Spring管理的容器中

第四步:在实体类上添加 @EntityListeners(AuditingEntityListener.class) 注解

之后,当进行save操作的时候,会自动设置获取通过审计注解获取的相关信息

 

 

 

参考官方文献:

转载于:https://www.cnblogs.com/XingXiaoMeng/p/11374146.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务