Spring Data审计功能@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy的使用
2017-07-01 14:21
429 查看
在Spring JPA中,在实体bean的属性上加注解@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy,可以再实体插入数据库时进行设置值,这样以来,在对普通实体类的添加、修改操作时,就不用关心创建时间、更新时间这些信息了。本文以SpringBoot为例
1、引入依赖
2、实体bean
3、实现AuditorAware接口
4、启用JPA Audit注解
1、引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
2、实体bean
@MappedSuperclass @Audited @EntityListeners(AuditingEntityListener.class) public abstract class AbstractEntity implements Serializable { private static final long serialVersionUID = 1L; @CreatedBy @Column(name = "created_by", nullable = false, length = 32, updatable = false) @JsonIgnore private String createdBy; @CreatedDate @Column(name = "created_date", nullable = false) @JsonIgnore private ZonedDateTime createdDate = ZonedDateTime.now(); @LastModifiedBy @Column(name = "modified_by", nullable = false, length = 32) @JsonIgnore private String modifiedBy; @LastModifiedDate @Column(name = "modified_date") @JsonIgnore private ZonedDateTime modifiedDate = ZonedDateTime.now(); } // 省略对应的 set/get 方法......
3、实现AuditorAware接口
@Component public class SpringSecurityAuditorAware implements AuditorAware<String> { @Override public String getCurrentAuditor() { //返回会话中或者指定的用户名 } }
4、启用JPA Audit注解
@Configuration @EntityScan @EnableJpaRepositories @EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware") @EnableTransactionManagement public class DatabaseConfiguration { private final Logger log = LoggerFactory.getLogger(DatabaseConfiguration.class); @Inject private Environment env; @Bean public Hibernate5Module hibernate5Module() { return new Hibernate5Module(); } }
相关文章推荐
- Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者
- Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者
- JPA Override 源码中createdDate,createdBy,lastModifiedBy,lastModifiedDate(过于麻烦,见下一篇已更新方法)
- Jpa配置实体类创建时间更新时间自动赋值,@CreateDate,@LastModifiedDate
- Jpa配置实体类创建时间更新时间自动赋值,@CreateDate,@LastModifiedDate
- Spring-Data-Jpa AuditingEntityListener @CreatedDate @LastModifiedDate 用法
- Spring data mongodb @CreatedBy@LastModifiedBy@CreatedBy@LastModifiedBy SpringSecurityAuditorAware,只记录用户名
- System.ArgumentOutOfRangeException at System.Web.HttpCachePolicy.UtcSetLastModified (DateTime utcDate)
- 关于何时使用实体标签ETag和最后修改时间(Last-modified)的规则
- VBS教程:属性-DateLastModified 属性
- Expires / Cache-Control / Last-Modified / If-Modified-Since / ETag / If-None-Match 区别使用
- Yomotsu 使用 THREE.js 创建的示例(Examples created by Yomotsu using THREE.js)
- Expires / Cache-Control / Last-Modified / If-Modified-Since / ETag / If-None-Match 区别使用
- SpringMVC-使用converter转换器解决后台接收Date格式数据时The request sent by the client was syntactically incorrect ()
- group by Year(CreatedDate), Month(CreatedDate) 用sql, entity sql (linq表达式好像挺复杂)
- [Step By Step]在SAP Business Objects Data Services中使用Date Generation生成日期维度数据并导入到SAP HANA中(Date Generation)
- 关于浏览器的缓存和getLastModified()的使用问题
- Expires / Cache-Control / Last-Modified / If-Modified-Since / ETag / If-None-Match 的区别以及使用详解
- 关于浏览器的缓存和getLastModified()的使用问题。
- LINQ To SharePint - 缺失Created By/Modified By 字段