欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot hibernate envers使用過程詳解

 更新時(shí)間:2020年06月22日 11:31:03   作者:yytxdy  
這篇文章主要介紹了Springboot hibernate envers使用過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

添加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)文章

  • idea常用配置之注釋快捷鍵方式

    idea常用配置之注釋快捷鍵方式

    這篇文章主要介紹了idea常用配置之注釋快捷鍵方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • java?字段值為null,不返回該字段的問題

    java?字段值為null,不返回該字段的問題

    這篇文章主要介紹了java?字段值為null,不返回該字段的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • springboot整合微信支付sdk過程解析

    springboot整合微信支付sdk過程解析

    這篇文章主要介紹了springboot整合微信支付sdk過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • java8新特性教程之time包使用總結(jié)

    java8新特性教程之time包使用總結(jié)

    Java8新增了date和time的util包,下面這篇文章主要給大家介紹了關(guān)于java8新特性教程之time包使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • 深入理解Java new String()方法

    深入理解Java new String()方法

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著Java new String()展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Java實(shí)現(xiàn)掃雷游戲詳細(xì)代碼講解

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

    windows自帶的游戲《掃雷》是陪伴了無數(shù)人的經(jīng)典游戲,本文將利用Java語(yǔ)言實(shí)現(xiàn)這一經(jīng)典的游戲,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下
    2022-05-05
  • 最新評(píng)論