一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Springboot hibernate envers使用过程代码解析

时间:2020-06-22 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下Springboot hibernate envers使用过程代码解析,代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

添加maven配置



  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    2.2.5.RELEASE
  
  springboot-envers
  springboot-envers

  
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.springframework.boot
      spring-boot-starter-data-jpa
    
    
      org.hibernate
      hibernate-envers
    
    
      com.h2database
      h2
    
  

使用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.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

创建相应的UserRepository

@Repository
public interface UserRepository extends JpaRepository {
}

添加用于增删改的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/h2,输入密码sa,即可登陆数据库并查询数据

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

其中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

在h2页面查询USER表

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

而查询USER_AUD表

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

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

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

org.hibernate.envers.strategy.internal.DefaultAuditStrategy

org.hibernate.envers.strategy.internal.ValidityAuditStrategy

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

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

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

热门栏目