在spring jpa audit 中,在字段或者方法上使用注解@CreatedDate
、@CreatedBy
、@LastModifiedDate
、@LastModifiedBy
,当进行实体插入或者更新可以自动赋值
-
@CreatedDate 创建时间
-
@CreatedBy 创建人
-
@LastModifiedDate 更新时间
-
@LastModifiedBy 更新人
使用:
1.定义实体类,并使用注解标注字段
import lombok.Data; import org.springframework.data.annotation.*; import org.springframework.data.mongodb.core.mapping.Field; import java.time.LocalDateTime; @Data public class BaseEntity { @Id private String id; @Field @CreatedBy private String createUserId; @Field @LastModifiedBy private String updateUserId; @Field @CreatedDate private LocalDateTime createTime; // 创建时间 @Field @LastModifiedDate private LocalDateTime updateTime; // 修改时间 }
2.添加 AuditorAware配置,设置默认用户