账户属性自动注入(auditor)
基类
@MappedSuperclass
@EntityListeners({AuditingEntityListener.class})
public abstract class BaseEntity<U> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;
@CreatedDate
protected LocalDateTime created;
@CreatedBy
protected U createdBy;
@LastModifiedDate
protected LocalDateTime lastModified;
@LastModifiedBy
protected U lastModifiedBy;
}
子类
@Entity
public class Author extends BaseEntity<String> implements Serializable
配置类
public class AuditorAwareImpl implements AuditorAware<String> {
@Override
public Optional<String> getCurrentAuditor() {
// 可接入security 来实现账户属性的自动注入
return "账户属性";
}
}
主键自生成
使用GenerationType.IDENTITY而不是GenerationType.AUTO