mybatis拦截器及不生效的解决方法

本文主要介绍了mybatis拦截器及不生效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

背景:

在一些需求下,使用拦截器会大大简化工作量也更加灵活:

  • 在项目中,要更新数据表的审计字段,比如 create_time, creator, update_time, updator, 这些字段,如果每一个表对应的mapper 都去写一次,或每一个方法都去更新一下,这个工作量非常大并且不太友好,并且不够优雅。
  • 记录一些日志,比如执行sql时侯,要打印每一个sql执行了多久,那就要记录sql执行前的时间戳,执行后的时间戳,得到其执行时间,再打印。
  • 等等场景

在这些场景下,使用拦截器肯定会更加灵活且方法。

mybatis拦截器怎样做

  • 定义一个拦截器
  • 把这个拦截器交给spring容器管理
  • 如果项目里面使用了 com.github.pagehelper.PageInterceptor 拦截器可能会无效,则需要再定义一个 MybatisInterceptorAutoConfiguration

根据以上三点,进行详细说明

定义一个拦截器

简单示意一下怎样写。。。具体业务肯定不止这样子的

一个拦截器,主要是实现 Interceptor 这个接口,实现这个接口下的三个方法。
然后在这个实现类加上 @Component 注解,就交给 spring容器管理了,所以1,2是一起的

import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.springframework.stereotype.Component; import java.util.Properties; import lombok.extern.slf4j.Slf4j; @Slf4j @Component @Intercepts({ @Signature( method = "query", type = Executor.class, args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class} ), @Signature( method = "query", type = Executor.class, args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class} ), @Signature( type = Executor.class, method = "update", args = {MappedStatement.class, Object.class} ) }) public class LogInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { log.info("begin >>>>>>>>>"); Object rest = invocation.proceed(); log.info("end >>>>>>>>>"); return rest; } @Override public Object plugin(Object o) { return Plugin.wrap(o, this); } @Override public void setProperties(Properties properties) { } }

定义一个 MybatisInterceptorAutoConfiguration

为什么要有这么一个类呢,主要是因为如果你的模块里面引用了 com.github.pagehelper.PageInterceptor,你自定义的拦截器会无效,是因为mybatis的拦截器这就是一个责任链,但是如果执行了 PageInterceptor,这个Interceptor比较特别,它自己执行完,就不往下传递链条了,即这个链会在它这里断开。所以加了其它的interceptor, 它们必须在 PageInterceptor 之前执行。

具体可见代码:

import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration; import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.context.annotation.Configuration; @Configuration // 这一行很重要,因为interceptor 链的执行与添加是反序的,所以在 PageHelperAutoConfiguration 之后添加,才能先执行。 @AutoConfigureAfter(PageHelperAutoConfiguration.class) public class MybatisInterceptorAutoConfiguration { @Autowired private List sqlSessionFactoryList; @PostConstruct public void addMyInterceptor() { LogInterceptor e = new LogInterceptor(); for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) { sqlSessionFactory.getConfiguration().addInterceptor(e); } } } 

附录几个参考的博文:

Mybatis拦截器
PageHelper导致自定义Mybatis拦截器不生效
Mybatis拦截器失效

到此这篇关于mybatis拦截器及不生效的解决方法的文章就介绍到这了,更多相关mybatis拦截器及不生效内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是mybatis拦截器及不生效的解决方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java