JPA之@EnableJpaAuditing注解

在Spring JPA中,支持在字段或方法上进行注解 @CreateDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy。具体含义:

**@CreateDate: ** 表示该字段是创建时间字段,在这个实体被insert的时候,会自动填充创建的时间,不用手动填充该字段。

**@CreatedBy: ** 表示该字段是创建人字段,在这个实体被insert的时候,会自动填充创建人字段,不用手动填充。

@LastModifiedDate、@LastModifiedBy同理。

如何实现自动填充功能,即如何使用审计?
1、在Xxx Application 启动类上添加 @EnableJpaAuditing:开启审计功能。

@EnableScheduling
@EnableJpaAuditing //利用jpa可以给MySQL列属性自动赋值,例如一些创建时间,修改时间
@EnableEurekaClient
@SpringBootApplication
public class CouponTemplateApplication {
    public static void main(String[] args) {
        SpringApplication.run(CouponTemplateApplication.class, args);
    }
    /**
     * 测试中如果无法自动识别,可能是包路径的问题,采用手动声明bean的方式
     * @return
     */
    @Bean
    public UserAuditor setUserAuditorAware(){
        return new UserAuditor();
    }
}

2、实体类上添加 @EntityListeners(AuditingEntityListener.class):开启实体类监听。
3、在需要的字段上加上 @CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 等注解。

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity  //实体类
@EntityListeners(AuditingEntityListener.class) //监听器,自动赋值创建时间
@Table(name = "coupon_template")
@JsonSerialize(using = CouponTemplateSerialize.class) //绑定自定义的序列化器
public class CouponTemplate implements Serializable {

    /** 自增主键 */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id",nullable = false)
    @Basic //指定属于我们数据表的一个列,相反的@Transient,表示该列不属于数据表
    private Integer id;

    /** 是否是可用状态 */
    @Column(name = "available",nullable = false)
    private Boolean available;

    /** 是否过期 */
    @Column(name = "expired",nullable = false)
    private Boolean expired;

    /** 优惠券名称 */
    @Column(name = "name",nullable = false)
    private String name;

    /** 优惠券 logo */
    @Column(name = "logo",nullable = false)
    private String logo;

    /** 优惠券描述 */
    @Column(name = "intro",nullable = false)
    private String desc;
    
    /** 优惠券模板 创建时间
     *      使用@CreateDate注解在插入的时候,自动生成创建时间,与监听注解有关
     * */
    @CreatedDate
    @Column(name = "create_time",nullable = false)
    private Date createTime;
}

4、实现 AuditorAware 接口来返回你需要插入的值。重点!

@Configuration
@Slf4j
public class UserAuditor implements AuditorAware<String> {

    /**
     * 获取当前创建或修改的用户
     * @return
     */
    @Override
    public Optional<String> getCurrentAuditor() {
        UserDetails user;
        try {
            user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return Optional.ofNullable(user.getUsername());
        }catch (Exception e){
            return Optional.empty();
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章