首页 > 其他分享 >Spring Data审计功能@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy的使用

Spring Data审计功能@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy的使用

时间:2024-02-01 16:32:12浏览次数:20  
标签:false Spring springframework LastModifiedBy CreatedBy import org data annotation

在Spring JPA中,在实体bean的属性上加注解@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy,可以再实体插入数据库时进行设置值,这样以来,在对普通实体类的添加、修改操作时,就不用关心创建时间、更新时间这些信息了。本文以SpringBoot为例


1、引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>


2、实体bean

注意:

2.1、@MappedSuperclass需要添加,因为一般这种domain类都是需要被继承的,为了在子类对象中能取到父类对象的属性,所以必须得加上;

2.3、@EntityListeners(AuditingEntityListener.class):需要开启domain对象的监听,否则无法监测到对象的修改

BaseEntity.java

package com.imddy.layuiadmin.common;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
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;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.time.LocalDateTime;

@Data
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @CreatedBy
    @Column(name = "create_by", nullable = false, updatable = false)
    @JsonIgnore
    private String createBy;

    @CreatedDate
    @Column(name = "create_time", nullable = false, updatable = false)
    @JsonIgnore
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createTime;

    @LastModifiedBy
    @Column(name = "update_by", nullable = false)
    @JsonIgnore
    private String updateBy;

    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    @JsonIgnore
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime updateTime;
}


3、实现AuditorAware接口

ShiroAuditorAware.java

package com.imddy.layuiadmin.auditoraware;

import org.apache.shiro.SecurityUtils;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class ShiroAuditorAware implements AuditorAware<String> {

    @Override
    public Optional<String> getCurrentAuditor() {
        //返回会话中或者指定的用户名
        //自己去处理获取用户名
        //return Optional.of((String) SecurityUtils.getSubject().getPrincipal());
        return Optional.of("用户名");
        /** 用于审计的用户的名称 */
    }

}


4、启用JPA Audit注解@EnableJpaAuditing(xxxApplication.java也就是项目的启动类中)

EnableJpaAuditingConfig.java

package com.imddy.layuiadmin.config;

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

@Configuration
@EnableJpaAuditing(auditorAwareRef="shiroAuditorAware")
public class EnableJpaAuditingConfig {

}

//1、这种是全部开启

//@EnableJpaAuditing

//2、这种是指定开启

//@EnableJpaAuditing(auditorAwareRef="shiroAuditorAware")




标签:false,Spring,springframework,LastModifiedBy,CreatedBy,import,org,data,annotation
From: https://blog.51cto.com/lenglingx/9533957

相关文章

  • 深入理解spring注解之@ComponentScan注解
    今天主要从以下几个方面来介绍一下@ComponentScan注解:@ComponentScan注解是什么@ComponentScan注解的详细使用 1,@ComponentScan注解是什么 其实很简单,@ComponentScan主要就是定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中  2,@Compone......
  • springboot整合mybatisplus
    1、引入依赖<dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency&g......
  • springAOP的理解和具体实现
    AOP:Aspect-OrientedProgramming面向切面编程。其实是对OOP(面向对象编程)的补充和完善简单点描述就是OOP有些事做起来很麻烦,很不方便,而利用AOP是可以很快很便捷的处理。OOP引入封装、继承、多态等概念去公共对象的处理。但对于公共行为的时候就不好弄。此时就衍生出AOP。比如对......
  • springboot整合mybatis(纯注解版)
    springboot整合mybatis1、注解:参考表@ResponseBody:表示该方法的返回结果直接写入HTTPresponsebody中,一般在异步获取数据时使用,用于构建RESTful的api。在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTPres......
  • META-INF.spring
    META-INF/spring 目录通常用于存放 Spring 框架相关的配置文件。这个目录位于 Java 或 Spring 应用的类路径(classpath)中,使得 Spring 框架能够在应用启动时自动加载这些配置文件。在 Spring 应用中,META-INF/spring 目录可能包含以下类型的文件:1. Spring 配置文件:这些......
  • 面试官:SpringCloudGateway过滤器类型有哪些?
    在SpringCloudGateway中,过滤器是在请求到达目标服务之前或之后,执行某些特定操作的一种机制。例如,它可以实现对传入的请求进行验证、修改、日志记录、身份验证、流量控制等各种功能。在SpringCloudGateway中,过滤器总共分为以下两大类:局部过滤器:只作用于某一个路由(route......
  • Spring自带的这11个工具类,真香!
    前言最近有些小伙伴,希望我分享一些好用的工具类,帮他们提升开发效率。今天这篇文章专门跟大家一起总结一下,Spring框架本身自带的一些好用的工具类,希望对你会有所帮助。1Assert很多时候,我们需要在代码中做判断:如果不满足条件,则抛异常。有没有统一的封装呢?其实Spring给我们......
  • spring boot分页
    0verridepublicPair<List<ApplyDto>,Long>selectDbApplyList(ApplyDtoapplyDto){Longid=applyDto.getId();//办事顺信DbSupervisiondbSupervision=abSupervisionMapper.selectDbSupervisionByDbId(id);if(dbSupervision==null){thrownewRuntimeExcep......
  • springboot @configuration
    @controller@service@repository下面都是@component注解booleanproxyBeanMethods()defaulttrue;proxyBeanMethods 是 Spring Boot 中 @Configuration 注解的一个属性,用于控制是否应该为 @Configuration 类中通过 @Bean 方法定义的 bean 创建代理。这个属性的......
  • Spring Boot的自动装配原理及流程
    自动装配的流程(原理)                       参考链接:https://blog.csdn.net/weixin_45764765/article/details/1102505311、main方法中SpringApplication.run(HelloBoot.class,args)的执行流程中有refreshContext(context)。2、而这个refreshContext(cont......