提交 421f6bdd 作者: 方治民

feat: 加入 JpaAuditing 操作人员记录

上级 b1a92522
...@@ -5,12 +5,14 @@ import org.springframework.boot.SpringApplication; ...@@ -5,12 +5,14 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters; import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EntityScan( @EntityScan(
basePackageClasses = { Application.class, Jsr310JpaConverters.class }, basePackageClasses = { Application.class, Jsr310JpaConverters.class },
basePackages = Application.BASE_PACKAGES basePackages = Application.BASE_PACKAGES
) )
@EnableJpaAuditing
@EnableJpaRepositories(basePackages = Application.BASE_PACKAGES) @EnableJpaRepositories(basePackages = Application.BASE_PACKAGES)
@SpringBootApplication(scanBasePackages = Application.BASE_PACKAGES) @SpringBootApplication(scanBasePackages = Application.BASE_PACKAGES)
public class Application { public class Application {
......
/* (C) 2023 YiRing, Inc. */
package com.yiring.auth.config;
import cn.dev33.satoken.stp.StpUtil;
import com.yiring.auth.domain.user.User;
import com.yiring.auth.domain.user.UserRepository;
import java.util.Optional;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;
/**
* @author Jim
* @version 0.1
* 2023/3/3 11:43
*/
@Component
@Configurable
@RequiredArgsConstructor
public class InjectAuditorAware implements AuditorAware<String> {
final UserRepository userRepository;
@Override
public @NonNull Optional<String> getCurrentAuditor() {
String loginId = StpUtil.getLoginIdAsString();
if (loginId != null) {
Optional<User> optional = userRepository.findById(loginId);
if (optional.isPresent()) {
return Optional.of(optional.get().getId());
}
}
return Optional.empty();
}
}
...@@ -2,16 +2,20 @@ ...@@ -2,16 +2,20 @@
package com.yiring.common.domain; package com.yiring.common.domain;
import com.yiring.common.snowflake.SnowflakeId; import com.yiring.common.snowflake.SnowflakeId;
import jakarta.persistence.*; import jakarta.persistence.EntityListeners;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import lombok.*; import lombok.*;
import lombok.experimental.FieldDefaults; import lombok.experimental.FieldDefaults;
import lombok.experimental.FieldNameConstants; import lombok.experimental.FieldNameConstants;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.Comment; import org.hibernate.annotations.*;
import org.hibernate.annotations.CreationTimestamp; import org.springframework.data.annotation.CreatedBy;
import org.hibernate.annotations.GenericGenerator; import org.springframework.data.annotation.CreatedDate;
import org.hibernate.annotations.UpdateTimestamp; import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener; import org.springframework.data.jpa.domain.support.AuditingEntityListener;
/** /**
...@@ -27,11 +31,13 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener; ...@@ -27,11 +31,13 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@ToString @ToString
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@SuperBuilder(toBuilder = true)
@FieldNameConstants @FieldNameConstants
@FieldDefaults(level = AccessLevel.PRIVATE) @FieldDefaults(level = AccessLevel.PRIVATE)
@SuperBuilder(toBuilder = true) @FilterDef(name = "deletedFilter", parameters = @ParamDef(name = "isDeleted", type = Boolean.class))
@MappedSuperclass @Filter(name = "deletedFilter", condition = "deleted = :isDeleted")
@EntityListeners(AuditingEntityListener.class) @EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public abstract class BasicEntity { public abstract class BasicEntity {
@Comment("主键") @Comment("主键")
...@@ -40,22 +46,30 @@ public abstract class BasicEntity { ...@@ -40,22 +46,30 @@ public abstract class BasicEntity {
@GenericGenerator(name = SnowflakeId.GENERATOR, strategy = SnowflakeId.Strategy.STRING) @GenericGenerator(name = SnowflakeId.GENERATOR, strategy = SnowflakeId.Strategy.STRING)
String id; String id;
@Comment("创建人")
@CreatedBy
String createBy;
@Comment("创建时间") @Comment("创建时间")
@Column(nullable = false) @CreatedDate
@CreationTimestamp
LocalDateTime createTime; LocalDateTime createTime;
@Comment("修改人")
@LastModifiedBy
String updateBy;
@Comment("最后修改时间") @Comment("最后修改时间")
@Column(nullable = false) @LastModifiedDate
@UpdateTimestamp
LocalDateTime updateTime; LocalDateTime updateTime;
@Comment("删除时间") @Comment("删除时间")
LocalDateTime deleteTime; Boolean deleted = Boolean.FALSE;
public interface Where { public interface Where {
String EXIST = " delete_time is null "; String EXIST = " deleted = false ";
String DELETE_SET = " set deleted = true ";
String DELETE_SET = " set delete_time = now() where id = ? "; String WHERE_ID = " where id = ? ";
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论