提交 421f6bdd 作者: 方治民

feat: 加入 JpaAuditing 操作人员记录

上级 b1a92522
......@@ -5,12 +5,14 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EntityScan(
basePackageClasses = { Application.class, Jsr310JpaConverters.class },
basePackages = Application.BASE_PACKAGES
)
@EnableJpaAuditing
@EnableJpaRepositories(basePackages = Application.BASE_PACKAGES)
@SpringBootApplication(scanBasePackages = Application.BASE_PACKAGES)
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 @@
package com.yiring.common.domain;
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 lombok.*;
import lombok.experimental.FieldDefaults;
import lombok.experimental.FieldNameConstants;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UpdateTimestamp;
import org.hibernate.annotations.*;
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;
/**
......@@ -27,11 +31,13 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@ToString
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder(toBuilder = true)
@FieldNameConstants
@FieldDefaults(level = AccessLevel.PRIVATE)
@SuperBuilder(toBuilder = true)
@MappedSuperclass
@FilterDef(name = "deletedFilter", parameters = @ParamDef(name = "isDeleted", type = Boolean.class))
@Filter(name = "deletedFilter", condition = "deleted = :isDeleted")
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public abstract class BasicEntity {
@Comment("主键")
......@@ -40,22 +46,30 @@ public abstract class BasicEntity {
@GenericGenerator(name = SnowflakeId.GENERATOR, strategy = SnowflakeId.Strategy.STRING)
String id;
@Comment("创建人")
@CreatedBy
String createBy;
@Comment("创建时间")
@Column(nullable = false)
@CreationTimestamp
@CreatedDate
LocalDateTime createTime;
@Comment("修改人")
@LastModifiedBy
String updateBy;
@Comment("最后修改时间")
@Column(nullable = false)
@UpdateTimestamp
@LastModifiedDate
LocalDateTime updateTime;
@Comment("删除时间")
LocalDateTime deleteTime;
Boolean deleted = Boolean.FALSE;
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论