Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自動生成時間和修改者


JPA Audit

在spring jpa中,支持在字段或者方法上進行注解@CreatedDate@CreatedBy@LastModifiedDate@LastModifiedBy,從字面意思可以很清楚的了解,這幾個注解的用處。

  • @CreatedDate
    表示該字段為創建時間時間字段,在這個實體被insert的時候,會設置值
  • @CreatedBy
    表示該字段為創建人,在這個實體被insert的時候,會設置值

  • @LastModifiedDate@LastModifiedBy同理。

如何使用?

首先申明實體類,需要在類上加上注解@EntityListeners(AuditingEntityListener.class),其次在application啟動類中加上注解EnableJpaAuditing,同時在需要的字段上加上@CreatedDate@CreatedBy@LastModifiedDate@LastModifiedBy等注解。

這個時候,在jpa.save方法被調用的時候,時間字段會自動設置並插入數據庫,但是CreatedBy和LastModifiedBy並沒有賦值,因為需要實現AuditorAware接口來返回你需要插入的值。

  • Application
復制代碼
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;


@SpringBootApplication
@EnableJpaAuditing
public class WalletApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(WalletApplication.class).web(true).run(args);
    }
}
復制代碼

 

  • AuditorAware
復制代碼
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

@Configuration
public class UserIDAuditorBean implements AuditorAware<Long> {
    @Override
    public Long getCurrentAuditor() {
        SecurityContext ctx = SecurityContextHolder.getContext();
        if (ctx == null) {
            return null;
        }
        if (ctx.getAuthentication() == null) {
            return null;
        }
        if (ctx.getAuthentication().getPrincipal() == null) {
            return null;
        }
        Object principal = ctx.getAuthentication().getPrincipal();
        if (principal.getClass().isAssignableFrom(Long.class)) {
            return (Long) principal;
        } else {
            return null;
        }
    }
}
復制代碼

 

  • Entity
復制代碼
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Table;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;


/**
 * 店鋪與支付渠道設備綁定.
 * @author Wang.ch
 *
 */
@Entity
@Table(name = "store_source_bind")
@EntityListeners(AuditingEntityListener.class)
public class StoreSourceBind {
    /**
     * 創建時間
     */
    @Column(name = "create_time")
    @CreatedDate
    private Date createTime;
    /**
     * 創建人
     */
    @Column(name = "create_by")
    @CreatedBy
    private Long createBy;
    /**
     * 修改時間
     */
    @Column(name = "lastmodified_time")
    @LastModifiedDate
    private Date lastmodifiedTime;
    /**
     * 修改人
     */
    @Column(name = "lastmodified_by")
    @LastModifiedBy
    private String lastmodifiedBy;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM