Springboot hibernate envers怎么使用

发布时间:2020-06-22 16:40:57 作者:清晨
来源:亿速云 阅读:276

这篇文章将为大家详细讲解有关Springboot hibernate envers怎么使用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

添加maven配置

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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.h3database</groupId>
      <artifactId>h3</artifactId>
    </dependency>
  </dependencies>

</project>

使用User类作为被审计的对象

@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.h3.console.enabled=true
spring.h3.console.path=/h3
spring.datasource.url=jdbc:h3:mem:envers
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.driverClassName=org.h3.Driver

创建相应的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;
  }
}

添加启动类

@SpringBootApplication
public class SpringbootEnversApplication {
  public static void main(String[] args) {
    SpringApplication.run(SpringbootEnversApplication.class, args);
  }
}

运行程序后,访问http://localhost:8080/h3,输入密码sa,即可登陆数据库并查询数据

由于配置了spring.jpa.hibernate.ddl-auto=create,可以看到系统已经为我们生成了相关的数据表

Springboot hibernate envers怎么使用

其中USER是实体类的表,USER_AUD是对应的审计表

依次访问以下链接,增加两条数据,分别对两条数据进行更新,再删除第一条数据

  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

在h3页面查询USER表

Springboot hibernate envers怎么使用

可以看到,USER表只有第二条数据更新后的记录了

而查询USER_AUD表

Springboot hibernate envers怎么使用

可以看到表中存在5条记录,分别对应着上面的五次操作

其中ID是USER表的主键,REV是USER_AUD的主键,REVTYPE是操作类型,0新增,1更新,2删除,name则是对应USER的name属性

hibernate提供了两种审计策略,分别是

如果使用DefaultAuditStrategy,USER_AUD表中不会有REVEND,REVEND_TSTMP两个字段,只会单纯的记录变更与版本

而使用ValidityAuditStrategy,在新增一条变更记录时,会更新上一条变更记录的REVEND,REVEND_TSTMP为当前的版本号以及变更时间

因为ValidityAuditStrategy除了插入新纪录还要更新旧的记录,所以插入速度会慢一点,但是因为提供了额外的信息,对于数据查询,速度则较DefaultAuditStrategy更快一些


关于Springboot hibernate envers怎么使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. springboot配置jpa
  2. 如何使用配置SpringBoot JPA

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot hibernate

上一篇:好程序员web前端学习路线分享AJAX状态码ajax.status及封装

下一篇:用python GUI来实现计算器的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》