SpringDataJpa的auditing功能

开启

@SpringCloudApplication
@EnableJpaAuditing
public class Application{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

标记

@Entity
@Table(name = "sample")
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
public class Sample {

    @Tolerate
    public Sample(){

    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ApiModelProperty("创建日期")
    @CreatedDate
    private Date createdAt;

    @ApiModelProperty("创建者")
    @CreatedBy
    private String createdBy;

    @ApiModelProperty("更新日期")
    @LastModifiedDate
    private Date lastModifiedAt;

    @ApiModelProperty("更新者")
    @LastModifiedBy
    private String lastModifiedBy;

    @ApiModelProperty("版本号")
    @Version
    private Long version;

    @Override
    public String toString(){
        return ToStringBuilder.reflectionToString(this);
    }
}

提供user

@Component
public class AuditorProvider implements AuditorAware {
    @Override
    public String getCurrentAuditor() {
        return "xixicat";
    }
}

参考

  • spring-data-jpa-auditing

  • spring-data-jpa-tutorial-auditing-part-one

  • spring-data-jpa-tutorial-auditing-part-two

你可能感兴趣的:(springboot)