spring mvc+jpa CreatedDate Auditing 增加审计功能

jpa支持审计功能都是熟知的,但网上都是spring boot的配置情况(即EnableJpaAuditing,今天找到了spring mvc的配置方案,在springmvc配置文件中通过配置的方式,增加@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 如下:

首先实体类增加

//实体通用继承类
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)

字段增加(以创造时间为例)

@Temporal(TemporalType.TIMESTAMP)//格式化时间
@LastModifiedDate
 private Date createTime;

还需要在pom文件中引上审计所需的包,不然会报Could not configure Spring Data JPA auditing-feature because spring-aspects.jar is not on the classpath!|If you want to use auditing please add spring-aspects.jar to the classpath.|的错误

  <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>

最后在spring配置文件中增加,我的是spring-mvc.xml

<jpa:auditing />

最为关键的一句,作用和spring boot的

在Application启动类中添加注解 @EnableJpaAuditing,一样!

完!

 

posted @ 2020-04-24 09:23  风起时的悟  阅读(1189)  评论(0编辑  收藏  举报