spring boot 中如何设置hibernate Interceptor

首先需用你用spring boot 搭建一个web项目,持久层用的spring data jpa(实现类是hibernate)

    因为在之前用mybatis plus的时候有个功能,能在insert的时候,自动填充实体类中的创建人、创建时间属性,同理update的时候也能填充更新人、更新时间的属性。其实这这是一个方面而已,我们也能实现记录的审计,谁新增了记录,谁删除了记录等。。就等着你去做了

    在做的突发奇想,能不能在hibernate中也实现这样的功能呢?如果要实现这样的功能,肯定实在insert之前填充创建人、创建时间,想就应该用拦截器来实现这样的功能。

    第一能想到是的是用aop来实现这样的功能,首先自定义注解,在新增、更新、删除的方法上加上对应的自定义注解,在拦击的的方法中去做属性的填充。

    第二个既然struts1、struts2、springmvc 有拦截器,在hibernate中是否也有这样的拦截器呢,于是在hibernate的文档中还真找到了拦截器的功能,我擦 我都开始佩服我自己了, 哈哈哈。。。。。。既然有拦截的功能,只要实现集成接口,重写方法就就行了,然后配置上拦截器就ok了。具体的实现先按下不说,在期间我到了spring data jpa 已经有了这个功能,实现起来也很简单。

    第三用spring data jpa的方式来实现上边说的填充功能

    1: 在spring boot的启动方法上添加上注解 (@EnableJpaAuditing)

    

    2: 在实体类上添加注解(@EntityListeners(AuditingEntityListener.class)) 

    

    3:在实体类的属性上添加注解

    @CreatedDate 创建时间

    @LastModifiedDate 更新时间

    spring data jpa能自动区分出来insert 和 update 能自动填上时间,狠省心啊

    @CreatedBy()  

    @LastModifiedBy()

        

   重点来了,每个人登录的账号不一样,怎么设置当前的创建人、更新人呢

    

    具体怎么获取当前登录人的id,需要看你用的什么框架(shiro、spring security)

    肯定是能获取的,具体我就不说了

=================华丽的分割线============================

   再来说说用hibernate的拦截器怎么来实现上说的功能

    自定义个类继承 EmptyInterceptor 类,给来中有多个方法,具体的请参考官方文档,我们在这里只说怎么用

    以更新为例说明:在方法体onFlushDirty中去重写,属性对应的 属性值、属性名、属性的类型 分别对应在

    Object[] currentState、 String[] propertyNames,、Type[] types

    遍历到要修改的属性名字并获取当前的index 去去改 Object的index参数就能实现,填充或者更改了

    方法体有个boolean类型的返回值,我得的true 和false 都能填充并保存到数据库,为什么 这个我也不知道。

    下面还有一个配置哟

    

    

interceptor的值等于 HibernateInterceptor错在的package位置+类名   

spring.jpa.properties.hibernate.ejb.interceptor=com.smile.config.hibernate.HibernateInterceptor

好了就这样了, 下回再研究多租户和hibernate的审计的功能,还有hibernate search

================================================

最后附上 官网文档http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch14.html


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot provides a way to intercept method calls using Method Interceptor. Method Interceptor allows you to intercept a method call before and after the method is called. This is useful when you want to add some additional behavior to a method, such as logging or security checks. To create a method interceptor in Spring Boot, you need to create a class that implements the `MethodInterceptor` interface from the `org.aopalliance.intercept` package. This interface has two methods: 1. `Object invoke(MethodInvocation invocation) throws Throwable`: This method is called before and after the method is executed. The `MethodInvocation` object contains information about the method call and allows you to manipulate the method parameters and return value. 2. `MethodInterceptor getInterceptor()`: This method returns the actual interceptor that will be used to intercept the method call. Here's an example of how to create a method interceptor in Spring Boot: ```java import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class LoggingInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("Before method call: " + invocation.getMethod().getName()); Object result = invocation.proceed(); System.out.println("After method call: " + invocation.getMethod().getName()); return result; } } ``` In this example, the `LoggingInterceptor` class implements the `MethodInterceptor` interface and overrides the `invoke` method to log the method name before and after the actual method call. To use this interceptor, you need to configure it in your Spring Boot application context: ```java import org.springframework.aop.framework.ProxyFactoryBean; @Configuration public class AppConfig { @Bean public LoggingInterceptor loggingInterceptor() { return new LoggingInterceptor(); } @Bean public MyService myService() { ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean(); proxyFactoryBean.setTarget(new MyServiceImpl()); proxyFactoryBean.setInterceptorNames("loggingInterceptor"); return (MyService) proxyFactoryBean.getObject(); } } ``` In this example, the `loggingInterceptor` bean is created and configured to intercept method calls. The `MyService` bean is also created and configured to use the `loggingInterceptor` for method interception. When a method is called on the `MyService` bean, the `LoggingInterceptor` will intercept the method call and log the method name before and after the actual method call.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值