哈喽!大家好,今天给大家分享一篇如何使用Spring Data JPA完成审计功能,文章中附有完整代码演示示例,便于学习;
✨个人博客:https://blog.csdn.net/weixin_43759352✨
✨公众号:【SimpleMemory】✨
❤️❤️❤️如果有对【后端技术】感兴趣的大佬们,欢迎关注!!!❤️❤️❤️
在项目中每条数据在创建修改的时候,我们都需要记录创建人,创建时间,修改人,修改时间等信息。如果每次新增的时候都去手动set,代码冗余且显得很不友好。
下面给大家分享如何使用Spring Data JPA完成审计功能:
实现该功能,主要涉及到下列注解:
@EnableJpaAuditing注解:启用JPA审计功能开关。
@CreatedBy注解:创建人,当实体被insert的时候,会设置值。
@LastModifiedBy注解:最后一次修改者,当实体每次被update的时候,会设置值。
@CreatedDate注解:创建日期,当实体被insert的时候,会设置值。
@LastModifiedDate注解:最后一次修改日期,当实体每次被update的时候,会设置值。
如何使用
一、引入依赖;
org.springframework.boot
spring-boot-starter-data-jpa
二、实现AuditorAware接口,来自定义获取用户的信息;在实际项目中需要从用户权限模块中获取到当前登录用户的实际信息;
package com.xxkfz.simplememory.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import java.util.Optional;
/**
* @author 公众号: SimpleMemory
* @version 1.0.0
* @ClassName SpringSecurityAuditorAware.java
* @Description 通过实现AuditorAware接口自定义获取用户信息
* @createTime 2022年02月18日 19:21:00
*/
@Configuration
public class SpringSecurityAuditorAware implements AuditorAware {
@Override
public Optional getCurrentAuditor() {
return Optional.of("xxkfz");
}
}
三、 创建实体类,并标记审计属性;
package com.xxkfz.simplememory.entity;
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.*;
import java.util.Date;
/**
* @author 公众号: SimpleMemory
* @version 1.0.0
* @ClassName SysUser.java
* @Description TODO
* @createTime 2022年02月18日 19:18:00
*/
@Data
@EntityListeners({AuditingEntityListener.class})
@Entity
@Table(name = "t_user")
public class SysUser {
@Id
@Column(name = "id")
private String id;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@CreatedBy
@Column(name = "created_by")
private String createdBy;
@LastModifiedBy
@Column(name = "last_modified_by")
private String lastModifiedBy;
@CreatedDate
@Column(name = "created_date")
private Date createdDate;
@LastModifiedDate
@Column(name = "last_modified_date")
private Date lastModifiedDate;
@Column(name = "real_name")
private String realName;
}
四、创建JPA Repository接口实现,用于对数据访问;
package com.xxkfz.simplememory.repository;
import com.xxkfz.simplememory.entity.SysUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @author 公众号: SimpleMemory
* @version 1.0.0
* @ClassName UserRepository.java
* @Description JPA Repository接口实现,用于对数据库的访问
* @createTime 2022年02月18日 19:24:00
*/
@Repository
public interface UserRepository extends JpaRepository {
}
五、在启动类上添加注解@EnableJpaAuditing
package com.xxkfz.simplememory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@EnableJpaAuditing
@SpringBootApplication
public class SimpleMemoryApplication {
public static void main(String[] args) {
SpringApplication.run(SimpleMemoryApplication.class, args);
}
}
六、编写测试类,进行测试;
package com.xxkfz.simplememory.controller.system;
import cn.hutool.core.lang.UUID;
import com.xxkfz.simplememory.entity.SysUser;
import com.xxkfz.simplememory.mapper.SysOrderMapper;
import com.xxkfz.simplememory.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @author 公众号: SimpleMemory
* @version 1.0.0
* @ClassName SysUserController.java
* @Description TODO
* @createTime 2022年02月18日 19:25:00
*/
@RestController
@RequestMapping("/sys_user")
@Slf4j
public class SysUserController {
@Autowired
UserRepository userRepository;
@GetMapping("save")
public void save() {
SysUser user = new SysUser();
user.setId(UUID.fastUUID().toString());
user.setUsername("公众号【SimpleMemory】");
SysUser sysUser = userRepository.save(user);
log.info("sysUser === {}", sysUser);
}
@GetMapping("update")
public void update() {
SysUser sysUser = userRepository.getById("1");
sysUser.setUsername("admin");
SysUser updateSysUser = userRepository.save(sysUser);
log.info("updateSysUser === {}", updateSysUser);
}
}
如果这篇【文章】对您有帮助,希望大家点赞、评论、关注、收藏;如果对【后端技术】感兴趣的小可爱,也欢迎关注❤️❤️❤️ 公众号【SimpleMemory】❤️❤️❤️,将会继续给大家带来【收获与惊喜】!