这篇“mybatis拦截器及不生效如何解决”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“mybatis拦截器及不生效如何解决”文章吧。
在一些需求下,使用拦截器会大大简化工作量也更加灵活:
在项目中,要更新数据表的审计字段,比如 create_time, creator, update_time, updator, 这些字段,如果每一个表对应的mapper 都去写一次,或每一个方法都去更新一下,这个工作量非常大并且不太友好,并且不够优雅。
记录一些日志,比如执行sql时侯,要打印每一个sql执行了多久,那就要记录sql执行前的时间戳,执行后的时间戳,得到其执行时间,再打印。
等等场景
在这些场景下,使用拦截器肯定会更加灵活且方法。
定义一个拦截器
把这个拦截器交给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) {
}
}
为什么要有这么一个类呢,主要是因为如果你的模块里面引用了 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拦截器及不生效如何解决”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://www.cnblogs.com/aaacarrot/p/16995126.html