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=create spring.jpa.properties.org.hibernate.envers.audit_strategy=org.hibernate.envers.strategy.internal.ValidityAuditStrategy spring.jpa.properties.org.hibernate.envers.audit_strategy_validity_store_revend_timestamp=true spring.h2.console.enabled=true spring.h2.console.path=/h2 spring.datasource.url=jdbc:h2:mem:envers spring.datasource.username=sa spring.datasource.password=sa spring.datasource.driverClassName=org.h2.Driver
創(chuàng)建相應(yīng)的UserRepository
@Repository public interface UserRepository extends JpaRepository<User, Long> { }
添加用于增刪改的Controller
@Controller public 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)類
@SpringBootApplication public 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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot如何實(shí)現(xiàn)并發(fā)任務(wù)并返回結(jié)果
這篇文章主要介紹了SpringBoot如何實(shí)現(xiàn)并發(fā)任務(wù)并返回結(jié)果問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

Java實(shí)現(xiàn)掃雷游戲詳細(xì)代碼講解