1.pom.xml的文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>business-base</groupId>
<artifactId>li-common</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
2.配置代码
@Configuration
@EnableJpaAuditing
public class AuditingConfig {
@Bean
public AuditorAware<String> auditorProvider() {
return new DefaultAuditorAwareImpl();
}
}
public class DefaultAuditorAwareImpl implements AuditorAware<String> {
@Override
public String getCurrentAuditor() {
//此处应该返回当前登录的用户的名,可以使用 private static final ThreadLocal<AuthContext> contextHolder = new InheritableThreadLocal<AuthContext>();等线程方法 将登录用户存入
//
return null;
}
}
3.抽象模型类
@MappedSuperclass
public abstract class AbstractModel extends AbstractPersistable<Long> {
private static final long serialVersionUID = 7205853521241442700L;
// TODO 需要实现集群化的ID生成机制
}
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractAuditModel extends AbstractModel {
private static final long serialVersionUID = -8388269103763562978L;
@CreatedBy
private String createdBy;
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date createdDate;
@LastModifiedBy
private String lastModifiedBy;
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
private Date lastModifiedDate;
@Version
private long version;
public String getCreatedBy() {
return this.createdBy;
}
public void setCreatedBy(final String createdBy) {
this.createdBy = createdBy;
}
public Date getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(final Date createdDate) {
this.createdDate = createdDate;
}
public String getLastModifiedBy() {
return this.lastModifiedBy;
}
public void setLastModifiedBy(final String lastModifiedBy) {
this.lastModifiedBy = lastModifiedBy;
}
public Date getLastModifiedDate() {
return this.lastModifiedDate;
}
public void setLastModifiedDate(final Date lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}
public long getVersion() {
return this.version;
}
public void setVersion(final long version) {
this.version = version;
}
}