Spring?Data?JPA實現(xiàn)審計功能過程詳解
在項目中每條數(shù)據(jù)在創(chuàng)建修改的時候,我們都需要記錄創(chuàng)建人,創(chuàng)建時間,修改人,修改時間等信息。如果每次新增的時候都去手動set,代碼冗余且顯得很不友好。
下面給大家分享如何使用Spring Data JPA完成審計功能:
實現(xiàn)該功能,主要涉及到下列注解:
- @EnableJpaAuditing注解:啟用JPA審計功能開關。
- @CreatedBy注解:創(chuàng)建人,當實體被insert的時候,會設置值。
- @LastModifiedBy注解:最后一次修改者,當實體每次被update的時候,會設置值。
- @CreatedDate注解:創(chuàng)建日期,當實體被insert的時候,會設置值。
- @LastModifiedDate注解:最后一次修改日期,當實體每次被update的時候,會設置值。
如何使用
一、引入依賴;
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
二、實現(xiàn)AuditorAware接口,來自定義獲取用戶的信息;在實際項目中需要從用戶權限模塊中獲取到當前登錄用戶的實際信息;
package com.xxkfz.simplememory.config; import org.springframework.context.annotation.Configuration; import org.springframework.data.domain.AuditorAware; import java.util.Optional; /** * @version 1.0.0 * @ClassName SpringSecurityAuditorAware.java * @Description 通過實現(xiàn)AuditorAware<T>接口自定義獲取用戶信息 */ @Configuration public class SpringSecurityAuditorAware implements AuditorAware<String> { @Override public Optional<String> getCurrentAuditor() { return Optional.of("xxkfz"); } }
三、 創(chuàng)建實體類,并標記審計屬性;
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; /** * @ClassName SysUser.java * @Description TODO */ @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; }
四、創(chuàng)建JPA Repository接口實現(xiàn),用于對數(shù)據(jù)訪問;
package com.xxkfz.simplememory.repository; import com.xxkfz.simplememory.entity.SysUser; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; /** * @ClassName UserRepository.java * @Description JPA Repository接口實現(xiàn),用于對數(shù)據(jù)庫的訪問 */ @Repository public interface UserRepository extends JpaRepository<SysUser, String> { }
五、在啟動類上添加注解@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.*; /** * @ClassName SysUserController.java * @Description TODO */ @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); } }
到此這篇關于Spring Data JPA實現(xiàn)審計功能過程詳解的文章就介紹到這了,更多相關Spring Data JPA實現(xiàn)審計內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring+Http請求+HttpClient實現(xiàn)傳參
這篇文章主要介紹了Spring+Http請求+HttpClient實現(xiàn)傳參,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03SpringBoot?+?MyBatis-Plus構建樹形結構的幾種方式
在實際開發(fā)中,很多數(shù)據(jù)都是樹形結構,本文主要介紹了SpringBoot?+?MyBatis-Plus構建樹形結構的幾種方式,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-08-08Java中將String類型依照某個字符分割成數(shù)組的方法
下面小編就為大家分享一篇Java中將String類型依照某個字符分割成數(shù)組的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03Elasticsearch8.1中的Script使用實例深入解讀
這篇文章主要為大家介紹了Elasticsearch8.1中的Script使用實例深入解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-102020新版idea創(chuàng)建項目沒有javaEE 沒有Web選項的完美解決方法
這篇文章主要介紹了2020新版idea創(chuàng)建項目沒有javaEE 沒有Web選項的完美解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09Netty客戶端接入流程NioSocketChannel創(chuàng)建解析
這篇文章主要為大家介紹了Netty客戶端接入流程NioSocketChannel創(chuàng)建源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03Java中final,finally,finalize三個關鍵字的區(qū)別_動力節(jié)點Java學院整理
這篇文章給大家收集整理了有關java中final,finally,finalize三個關鍵字的區(qū)別介紹,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-04-04