From 9d6e977d93218930b753b1eedb6b332e16381714 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Thu, 5 Dec 2024 17:54:58 +0800 Subject: [PATCH] =?UTF-8?q?feat(web):=20=E6=B7=BB=E5=8A=A0=E5=AE=9E?= =?UTF-8?q?=E4=BD=93=E5=AE=A1=E8=AE=A1=E5=8A=9F=E8=83=BD=E5=B9=B6=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=AE=A1=E8=AE=A1=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 SimpleEntity 中添加 @CreatedBy 和 @LastModifiedBy 注解 - 在 WebApplication 中添加 AuditorAware Bean - 使用 UserService 的 currentLoginUserOptional 方法获取当前登录用户 --- .../java/com/eshore/gringotts/web/WebApplication.java | 8 ++++++++ .../gringotts/web/domain/base/entity/SimpleEntity.java | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/WebApplication.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/WebApplication.java index 595236d..b6bb00b 100644 --- a/gringotts-web/src/main/java/com/eshore/gringotts/web/WebApplication.java +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/WebApplication.java @@ -1,6 +1,7 @@ package com.eshore.gringotts.web; import com.blinkfox.fenix.EnableFenix; +import com.eshore.gringotts.web.domain.user.entity.User; import com.eshore.gringotts.web.domain.user.service.UserService; import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; import org.springframework.boot.ApplicationArguments; @@ -9,6 +10,8 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.data.domain.AuditorAware; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.scheduling.annotation.EnableAsync; @@ -41,4 +44,9 @@ public class WebApplication implements ApplicationRunner { // 初始化系统管理员 userService.initial(); } + + @Bean + public AuditorAware auditorAware(UserService userService) { + return userService::currentLoginUserOptional; + } } diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/entity/SimpleEntity.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/entity/SimpleEntity.java index 947ad14..239af7a 100644 --- a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/entity/SimpleEntity.java +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/entity/SimpleEntity.java @@ -12,7 +12,9 @@ import javax.persistence.OneToOne; import lombok.Getter; import lombok.Setter; import lombok.ToString; +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 org.springframework.data.jpa.domain.support.AuditingEntityListener; @@ -33,11 +35,13 @@ public class SimpleEntity extends IdOnlyEntity { @OneToOne(fetch = FetchType.LAZY) @JoinColumn(nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) @ToString.Exclude + @CreatedBy private User createdUser; @LastModifiedDate private LocalDateTime modifiedTime; @OneToOne(fetch = FetchType.LAZY) @JoinColumn(nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) @ToString.Exclude + @LastModifiedBy private User modifiedUser; }