久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring Data JPA的Audit功能審計(jì)數(shù)據(jù)庫(kù)的變更

瀏覽:31日期:2023-07-06 09:31:03
目錄1 數(shù)據(jù)庫(kù)審計(jì)2 Spring Data JPA審計(jì)2.1 項(xiàng)目準(zhǔn)備2.2 創(chuàng)建實(shí)體父類2.3 如何獲取名字2.4 測(cè)試3 總結(jié)

我最新最全的文章都在 南瓜慢說(shuō) www.pkslow.com ,歡迎大家來(lái)喝茶!

1 數(shù)據(jù)庫(kù)審計(jì)

數(shù)據(jù)庫(kù)審計(jì)是指當(dāng)數(shù)據(jù)庫(kù)有記錄變更時(shí),可以記錄數(shù)據(jù)庫(kù)的變更時(shí)間和變更人等,這樣以后出問(wèn)題回溯問(wèn)責(zé)也比較方便。對(duì)于審計(jì)表記錄的變更可以兩種方式,一種是建立一張審計(jì)表專門(mén)用于記錄,另一種是在數(shù)據(jù)庫(kù)增加字段。本文所討論的是第二種方案。

那如何在新增、修改、刪除的時(shí)候同時(shí)增加記錄呢?如果每張表都單獨(dú)記錄,代碼就會(huì)顯得很冗余。更好的方式應(yīng)該是做切面或者事件監(jiān)聽(tīng),當(dāng)數(shù)據(jù)有變更時(shí)統(tǒng)一進(jìn)行記錄。

2 Spring Data JPA審計(jì)

Spring Data JPA為我們提供了方便的Audit功能,通過(guò)四個(gè)注解來(lái)標(biāo)記字段:

(1) @CreatedBy: 創(chuàng)建人

(2) @CreatedDate: 創(chuàng)建時(shí)間

(3) @LastModifiedBy: 最后修改人

(4) @LastModifiedDate: 最后修改時(shí)間

接下來(lái)我們來(lái)看看怎么使用。

2.1 項(xiàng)目準(zhǔn)備

通過(guò)Docker啟動(dòng)PostgreSQL數(shù)據(jù)庫(kù):

docker run -itd --name pkslow-postgres -e POSTGRES_DB=pkslow -e POSTGRES_USER=pkslow -e POSTGRES_PASSWORD=pkslow -e PGDATA=/var/lib/postgresql/data/pgdata -p 5432:5432 postgres:10

引入相關(guān)依賴:

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

Spring Security不是必須的,這里使用它來(lái)獲取用戶名。配置的用戶為:

spring.security.user.name=pkslowspring.security.user.password=1234562.2 創(chuàng)建實(shí)體父類

其實(shí)父類不是必須的,你可以在每個(gè)想Audit的實(shí)體類進(jìn)行配置,但比較麻煩,不如創(chuàng)建一個(gè)父類,再讓想審計(jì)的子類都繼承它:

@MappedSuperclass@EntityListeners(AuditingEntityListener.class)public class Auditable<U> { @CreatedBy @Column(name = 'created_by') private U createdBy; @CreatedDate @Column(name = 'created_date') private Date createdDate; @LastModifiedBy @Column(name = 'last_modified_by') private U lastModifiedBy; @LastModifiedDate @Column(name = 'last_modified_date') private Date lastModifiedDate; // getter //setter}

@MappedSuperclass可以讓其它子實(shí)體類繼承相關(guān)的字段和屬性;

@EntityListeners設(shè)置監(jiān)聽(tīng)類,會(huì)對(duì)新增和修改進(jìn)行回調(diào)處理。

有了父類之后,子類就簡(jiǎn)單了:

@Entity@Table(name = 'pkslow_users')public class User extends Auditable<String> { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long userId; private String name; private String email; private String country; private String website; //getter setter}2.3 如何獲取名字

數(shù)據(jù)總是被修改的,我們要提供一個(gè)獲取修改人名字的接口,配置如下:

@Configuration@EnableJpaAuditing(auditorAwareRef = 'auditorProvider')public class JpaAuditingConfiguration { @Bean public AuditorAware<String> auditorProvider() {return () -> { String username = 'system'; SecurityContext context = SecurityContextHolder.getContext(); if (context != null) {Authentication authentication = context.getAuthentication();if (authentication != null) { username = authentication.getName();} } String result = username; return Optional.ofNullable(result);}; }}

這里配置的是通過(guò)Spring Security的Context來(lái)獲取登陸用戶的名字,當(dāng)然可以有其它方案,如獲取請(qǐng)求頭的某個(gè)字段等。

注意注解@EnableJpaAuditing開(kāi)啟了審計(jì)功能。

2.4 測(cè)試

我們通過(guò)一個(gè)Controller來(lái)新增數(shù)據(jù),看看會(huì)有什么效果:

@RestController@RequestMapping('/user')public class UserController { @Autowired private UserRepository userRepository; @PostMapping public User save(@RequestBody User user) {return userRepository.save(user); }}

通過(guò)curl命令來(lái)測(cè)試如下:

$ curl ’http://localhost:8088/user’ -X POST > -H ’Content-Type: application/json’ > -H ’Authorization:Basic cGtzbG93OjEyMzQ1Ng==’ > -d ’{> 'name':'larry',> 'email':'admin@pkslow.com',> 'country':'China',> 'website':'www.pkslow.com'> }’{'createdBy':'pkslow','createdDate':'2021-01-15T15:08:47.035+0000','lastModifiedBy':'pkslow','lastModifiedDate':'2021-01-15T15:08:47.035+0000','userId':7,'name':'larry','email':'admin@pkslow.com','country':'China','website':www.pkslow.com}

查看數(shù)據(jù)庫(kù),已經(jīng)生成了審計(jì)記錄:

Spring Data JPA的Audit功能審計(jì)數(shù)據(jù)庫(kù)的變更

3 總結(jié)

代碼請(qǐng)查看:https://github.com/LarryDpk/pkslow-samples

到此這篇關(guān)于Spring Data JPA的Audit功能審計(jì)數(shù)據(jù)庫(kù)的變更的文章就介紹到這了,更多相關(guān)Spring Data JPA審計(jì)數(shù)據(jù)庫(kù)變更內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 张北县| 巴林右旗| 毕节市| 东兴市| 维西| 湄潭县| 明水县| 龙川县| 康马县| 安塞县| 巩留县| 永昌县| 嘉荫县| 伊金霍洛旗| 上思县| 泗阳县| 疏勒县| 华亭县| 柳州市| 文登市| 满洲里市| 汉中市| 朝阳市| 门头沟区| 周至县| 牟定县| 措勤县| 溧阳市| 临夏县| 科技| 江山市| 昆山市| 博爱县| 九龙县| 体育| 布拖县| 廉江市| 伊春市| 昌江| 汉中市| 瑞丽市|