JPA 基础(3)—— Auditing
2019-03-28 本文已影响0人
黄宝玲_1003
JPA 基础(1)—— 数据库持久化代码实战
JPA 基础(2)—— 分页操作
JPA 基础(3)—— Auditing
JPA 基础(4)—— 关联表映射
我们在创建实体类时,可以用@CreatedDate
、@LastModifiedDate
、@CreatedBy
、@LastModifiedBy
透明地跟踪创建或更改实体的人员以及更改发生的时间。
package com.jpa.test;
import lombok.Data;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Data
@Entity
@Table(name = "student")
public class StudentEntity {
@Id
private String no;
@Column(name = "class_no")
private String classNo;
private Integer grade;
@CreatedDate
private Date createdTime;
@LastModifiedDate
private Date updatedTime;
@CreatedBy
private Date createdBy;
@LastModifiedBy
private Date updatedBy;
}
需要在主类加上注解@EnableJpaAuditing
开启审计功能。
package com.jpa.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@EnableJpaAuditing
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}