天天看點

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

JPA審計

在春天JPA中,支援在字段或者方法上進行注解

@CreatedDate

, ,

@CreatedBy

@LastModifiedDate

@LastModifiedBy

從字面意思可以很清楚的了解,這幾個注解的用處。

  • @CreatedDate

    表示該字段為建立時間時間字段,在這個實體被插入的時候,會設定值
  • @CreatedBy

    表示該字段為建立人,在這個實體被插入的時候,會設定值
  • @LastModifiedDate

    @LastModifiedBy

    同理。

如何使用?

首先申明實體類,需要在類上加上注解

@EntityListeners(AuditingEntityListener.class)

,其次在應用程式啟動類中加上注解

EnableJpaAuditing

,同時在需要的字段上加上

@CreatedDate

@CreatedBy

@LastModifiedDate

@LastModifiedBy

等注解。

這個時候,在jpa.save方法被調用的時候,時間字段會自動設定并插入資料庫,但是CreatedBy和LastModifiedBy并沒有指派,需要因為實作

AuditorAware

接口來報道檢視你需要插入的值。

  • 應用
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;
        }
    }
}
                
  • 實體
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;
}
                
</div>
           

繼續閱讀