Java中如何通过基类实现审计字段的复用

ChatGPT4o 使用 ChatGPT 4o 回答

问题:

java 像创建人 修改时间 等审计字段,如何抽取到一个基类,实现多个Entity继承复用

回答:

在Java中,你可以通过创建一个基类来包含通用的审计字段(如创建人、修改时间等),然后让其他实体类继承这个基类。这样可以实现代码复用和一致性管理。以下是一个示例:

  1. 创建一��基类
import javax.persistence.MappedSuperclass;
import javax.persistence.Column;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import java.time.LocalDateTime;

@MappedSuperclass
public abstract class Auditable {

    @Column(name = "created_by", updatable = false)
    private String createdBy;

    @Column(name = "created_date", updatable = false)
    private LocalDateTime createdDate;

    @Column(name = "last_modified_by")
    private String lastModifiedBy;

    @Column(name = "last_modified_date")
    private LocalDateTime lastModifiedDate;

    @PrePersist
    protected void onCreate() {
        createdDate = LocalDateTime.now();
        // You can set createdBy here if you have access to the current user
    }

    @PreUpdate
    protected void onUpdate() {
        lastModifiedDate = LocalDateTime.now();
        // You can set lastModifiedBy here if you have access to the current user
    }

    // Getters and Setters
    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public LocalDateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(LocalDateTime createdDate) {
        this.createdDate = createdDate;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public LocalDateTime getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }
}
  1. 让实体类继承基类
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class MyEntity extends Auditable {

    @Id
    private Long id;

    private String name;

    // Other fields, getters, and setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
  1. 配置审计用户(可选):

如果你需要自动设置 createdBylastModifiedBy 字段,你可以使用 Spring Security 或其他安全框架来获取当前用户信息。以下是一个示例,假设你使用 Spring Security:

import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class AuditorAwareImpl implements AuditorAware<String> {

    @Override
    public Optional<String> getCurrentAuditor() {
        // Return the current user (e.g., from Spring Security context)
        return Optional.of("currentUser"); // Replace with actual user retrieval logic
    }
}

然后在你的配置类中启用审计:

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorAwareImpl")
public class JpaConfig {
}

通过这种方式,你可以将审计字段抽取到一个基类,并在多个实体类中复用这些字段。