• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Springboot hibernate envers使用过程详解

springboot 搞代码 4年前 (2022-01-05) 25次浏览 已收录 0个评论

这篇文章主要介绍了Springboot hibernate envers使用过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

添加maven配置

   4.0.0 org.springframework.bootspring-boot-starter-parent2.2.5.RELEASEspringboot-enversspringboot-envers  org.springframework.bootspring-boot-starter-web org.springframework.bootspring-boot-starter-data-jpa org.hibernatehibernate-envers com.h2databaseh2

使用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,即可来源gao@daima#com搞(%代@#码@网登陆数据库并查询数据

由于配置了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更快一些

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是Springboot hibernate envers使用过程详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Springboot hibernate envers使用过程详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址