Springboot hibernate envers使用過程詳解
添加maven配置
<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent> <artifactId>springboot-envers</artifactId> <name>springboot-envers</name> <dependencies> <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.hibernate</groupId> <artifactId>hibernate-envers</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> </dependencies></project>
使用User類作為被審計(jì)的對(duì)象
@Entity@Table(name = 'user')@Audited@JsonIgnoreProperties(value = 'hibernateLazyInitializer')public class User { @Id @GeneratedValue private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
添加配置
spring.jpa.hibernate.ddl-auto=createspring.jpa.properties.org.hibernate.envers.audit_strategy=org.hibernate.envers.strategy.internal.ValidityAuditStrategyspring.jpa.properties.org.hibernate.envers.audit_strategy_validity_store_revend_timestamp=truespring.h2.console.enabled=truespring.h2.console.path=/h2spring.datasource.url=jdbc:h2:mem:enversspring.datasource.username=saspring.datasource.password=saspring.datasource.driverClassName=org.h2.Driver
創(chuàng)建相應(yīng)的UserRepository
@Repositorypublic interface UserRepository extends JpaRepository<User, Long> {}
添加用于增刪改的Controller
@Controllerpublic class UserController { @Autowired private UserRepository userRepository; private int counter; @ResponseBody @RequestMapping('/user/add') public Object add() { User user = new User(); user.setName('name' + ++counter); userRepository.save(user); return user; } @ResponseBody @RequestMapping('/user/update/{id}') public Object update(@PathVariable Long id) { User user = userRepository.getOne(id); user.setName('name' + ++counter); userRepository.save(user); return user; } @ResponseBody @RequestMapping('/user/delete/{id}') public Object delete(@PathVariable Long id) { User user = userRepository.getOne(id); userRepository.delete(user); return user; }}
添加啟動(dòng)類
@SpringBootApplicationpublic class SpringbootEnversApplication { public static void main(String[] args) { SpringApplication.run(SpringbootEnversApplication.class, args); }}
運(yùn)行程序后,訪問http://localhost:8080/h2,輸入密碼sa,即可登陸數(shù)據(jù)庫(kù)并查詢數(shù)據(jù)
由于配置了spring.jpa.hibernate.ddl-auto=create,可以看到系統(tǒng)已經(jīng)為我們生成了相關(guān)的數(shù)據(jù)表
其中USER是實(shí)體類的表,USER_AUD是對(duì)應(yīng)的審計(jì)表
依次訪問以下鏈接,增加兩條數(shù)據(jù),分別對(duì)兩條數(shù)據(jù)進(jìn)行更新,再刪除第一條數(shù)據(jù)
http://localhost:8080/user/add
http://localhost:8080/user/add
http://localhost:8080/user/update/1
http://localhost:8080/user/update/2
http://localhost:8080/user/delete/1
在h2頁(yè)面查詢USER表
可以看到,USER表只有第二條數(shù)據(jù)更新后的記錄了
而查詢USER_AUD表
可以看到表中存在5條記錄,分別對(duì)應(yīng)著上面的五次操作
其中ID是USER表的主鍵,REV是USER_AUD的主鍵,REVTYPE是操作類型,0新增,1更新,2刪除,name則是對(duì)應(yīng)USER的name屬性
hibernate提供了兩種審計(jì)策略,分別是
org.hibernate.envers.strategy.internal.DefaultAuditStrategy org.hibernate.envers.strategy.internal.ValidityAuditStrategy如果使用DefaultAuditStrategy,USER_AUD表中不會(huì)有REVEND,REVEND_TSTMP兩個(gè)字段,只會(huì)單純的記錄變更與版本
而使用ValidityAuditStrategy,在新增一條變更記錄時(shí),會(huì)更新上一條變更記錄的REVEND,REVEND_TSTMP為當(dāng)前的版本號(hào)以及變更時(shí)間
因?yàn)閂alidityAuditStrategy除了插入新紀(jì)錄還要更新舊的記錄,所以插入速度會(huì)慢一點(diǎn),但是因?yàn)樘峁┝祟~外的信息,對(duì)于數(shù)據(jù)查詢,速度則較DefaultAuditStrategy更快一些
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案2. ASP.NET泛型三之使用協(xié)變和逆變實(shí)現(xiàn)類型轉(zhuǎn)換3. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析4. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))5. 如何在jsp界面中插入圖片6. XML基本概念XPath、XSLT與XQuery函數(shù)介紹7. JS中map和parseInt的用法詳解8. PHP循環(huán)與分支知識(shí)點(diǎn)梳理9. AspNetCore&MassTransit Courier實(shí)現(xiàn)分布式事務(wù)的詳細(xì)過程10. jsp實(shí)現(xiàn)登錄界面
