广告
返回顶部
首页 > 资讯 > 后端开发 > Python >mybatis拦截器及不生效的解决方法
  • 220
分享到

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

mybatis拦截器mybatis拦截器不生效 2023-05-14 17:05:01 220人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录背景:mybatis拦截器怎样做定义一个拦截器定义一个 MybatisInterceptorAutoConfiguration背景: 在一些需求下,使用拦截器会大大简化工作量也更

背景:

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

  • 项目中,要更新数据表的审计字段,比如 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<SqlSessionFactory> sqlSessionFactoryList;
 
    @PostConstruct
    public void addMyInterceptor() {
        LogInterceptor e = new LogInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(e);
        }
    }
}

附录几个参考的博文:

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

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

--结束END--

本文标题: mybatis拦截器及不生效的解决方法

本文链接: https://www.lsjlt.com/news/203349.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作