Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者
来源:互联网 发布:网络直播需要哪些设备 编辑:程序博客网 时间:2024/05/22 08:12
转载自原文: http://www.jianshu.com/p/14cb69646195
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@EnableJpaAuditingpublic 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;@Configurationpublic 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;}
阅读全文
0 0
- Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者
- JPA @CreatedBy@CreatedDate@LastModifiedBy@LastModifiedDate
- Spring Data审计功能@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy的使用
- JPA Override 源码中createdDate,createdBy,lastModifiedBy,lastModifiedDate(过于麻烦,见下一篇已更新方法)
- Jpa配置实体类创建时间更新时间自动赋值,@CreateDate,@LastModifiedDate
- Spring+JPA自动生成Table配置
- hibernate jpa注解配置自动生成数据库时间
- JPA自动生成数据表
- Spring Data JPA 介绍和使用
- Spring Data JPA 介绍和使用
- 使用spring data jpa时,自动注入dao层报错
- 向数据库插入数据、修改数据时自动生成时间以及自动修改时间
- JPA不能自动生成主键
- jpa自动主键生成和hibernate提供的自动主键生成方式
- Spring Boot:在Spring Boot中使用Mysql和JPA
- Spring Boot自动生成模板和Spring Boot CLI安装使用
- Spring Data JPA 使用
- spring-data-jpa 使用
- SDUT-2557 分段函数
- js获取当前页面的URL
- 交换两个变量的值,不使用第三个变量的方法及实现
- 文章标题
- Android开发如何写出优雅的代码
- Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者
- Unity3d shader 教程一 准备
- 使用maven 一步一步搭建Spring+mybatis环境
- PL/SQL Developer 中文乱码解决
- 对于python setup.py install安装的包如何卸载
- (转)听赌徒谈风险:没犯任何错误照样输个精光
- 电商6.18-it系统实战
- unity光照系统-反射探头Reflection Probe
- group_concat函数详解