java-mybaits-013-mybatis-Interceptor-拦截器执行顺序
一、概述
已知拦截器能够拦截四种类型:Executor、ParameterHandler、ResultSetHandler、StatementHandler。
1.1、不同类型拦截器的执行顺序
背景:不同类型
项目地址:https://github.com/bjlhx15/mybatis.git 中的mybatis-interceptor-001 基础项目
顺序【Executor→StatementHandler→ParameterHandler→ResultSetHandler】
1、编写拦截器代码
2、mybatis.xml配置
<plugins>
<!-- 拦截器配置 -->
<plugin interceptor="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.TestTypeInterceptorParameterHandler"/>
<plugin interceptor="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.TestTypeInterceptorResultSetHandler"/>
<plugin interceptor="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.TestTypeInterceptorStatementHandler"/>
<plugin interceptor="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.TestTypeInterceptorExecutor"/>
</plugins>
3、执行测试输出
TestTypeInterceptorExecutor
[2019-07-29 10:37:25:540-http-nio-8080-exec-1] [INFO] - com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:930) - {dataSource-1} inited
TestTypeInterceptorStatementHandler
[2019-07-29 10:37:29:827-http-nio-8080-exec-1] [DEBUG] - org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:145) - ==> Preparing: select 'true' as QUERYID, id, name, version, balance from accountbalance
TestTypeInterceptorParameterHandler
[2019-07-29 10:37:31:177-http-nio-8080-exec-1] [DEBUG] - org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:145) - ==> Parameters:
TestTypeInterceptorResultSetHandler
可以看到不论配置文件如何,执行顺序如下:【Executor→StatementHandler→ParameterHandler→ResultSetHandler】
1.2、同类型位置不同
背景:同类型位置不同
项目地址:https://github.com/bjlhx15/mybatis.git 中的mybatis-interceptor-002 基础项目
顺序:数组上下位置倒序【3拦截前处理 > 2拦截前处理 > 1拦截前处理 > executor.query() > 1拦截后处理 > 2拦截后处理 > 3拦截后处理】
1、编写拦截器
2、编写xml
<plugins>
<!-- 拦截器配置 -->
<plugin interceptor="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.Test1Interceptor"/>
<plugin interceptor="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.Test2Interceptor"/>
</plugins>
3、执行测试输出
Test2Interceptor
Test1Interceptor
4、why
查看mybatis配置org.apache.ibatis.session.Configuration。中创建
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? this.defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Object executor;
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
} if (this.cacheEnabled) {
executor = new CachingExecutor((Executor)executor);
} Executor executor = (Executor)this.interceptorChain.pluginAll(executor);
return executor;
}
interceptorChain是在Configuration类中new出来的。它等价于mybatis-config中的<plugins></plugins>
查看 pluginAll添加顺序
private final List<Interceptor> interceptors = new ArrayList(); public InterceptorChain() {
} public Object pluginAll(Object target) {
Interceptor interceptor;
for(Iterator i$ = this.interceptors.iterator(); i$.hasNext(); target = interceptor.plugin(target)) {
interceptor = (Interceptor)i$.next();
} return target;
}
可以看到 interceptorChain 拦截器顺序就是 配置顺序
但是在经过代理后,
3拦截前处理 > 2拦截前处理 > 1拦截前处理 > executor.query() > 1拦截后处理 > 2拦截后处理 > 3拦截后处理
1.3、同类型不同配置位置
背景:有spring中配置,还有mybatis中配置
项目地址:https://github.com/bjlhx15/mybatis.git 中的mybatis-interceptor-002 基础项目
顺序:数组上下位置倒序【mybatis配置拦截器2 > mybatis配置拦截器1 > spring配置拦截器2 >spring配置拦截器1 > executor.query() > spring配置拦截器1 > spring配置拦截器2 > mybatis配置拦截器1 > mybatis配置拦截器2 】
1、编写拦截器
2、spring中xml配置
<bean id="wrSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 -->
<property name="dataSource" ref="wrDataSource"/>
<property name="mapperLocations" value="classpath:mapper/auto/**/*.xml"/>
<!-- mybatis的全局配置文件 如没有特需 可以不配置 -->
<property name="plugins">
<array>
<bean class="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.TestnterceptorIString1"/>
<bean class="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.TestnterceptorIString2"/>
</array>
</property>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
mybatis中配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<plugins>
<!-- 拦截器配置 -->
<plugin interceptor="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.TestInterceptorMybatis1"/>
<plugin interceptor="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.TestInterceptorMybatis2"/>
</plugins> </configuration>
3、输出
TestInterceptorMybatis2
TestInterceptorMybatis1
TestnterceptorIString2
TestnterceptorIString1
可以调整 spring中configLocation和plugins上下位置,输出一致
但是
java-mybaits-013-mybatis-Interceptor-拦截器执行顺序的更多相关文章
- Mybatis Interceptor 拦截器原理 源码分析
Mybatis采用责任链模式,通过动态代理组织多个拦截器(插件),通过这些拦截器可以改变Mybatis的默认行为(诸如SQL重写之类的),由于插件会深入到Mybatis的核心,因此在编写自己的插件前最 ...
- mybatis Interceptor拦截器代码详解
mybatis官方定义:MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...
- SpringMVC自定义多个拦截器执行顺序
一.正常流程下的拦截器(全部放行) 1.springMVC中拦截器实现这个接口HandlerInterceptor 第一个拦截器 HandlerInterceptor1 public class ...
- Struts2的默认拦截器执行顺序
我们在写Struts2的时候package属性默认都是差不多这样吧 <package name="packageName" namespace="/" e ...
- Springmvc的拦截器执行顺序及各方法作用
实现HandlerInterceptor接口或者继承HandlerInterceptor的子类,比如Spring 已经提供的实现了HandlerInterceptor 接口的抽象类HandlerInt ...
- Mybatis拦截器执行过程解析
上一篇文章 Mybatis拦截器之数据加密解密 介绍了 Mybatis 拦截器的简单使用,这篇文章将透彻的分析 Mybatis 是怎样发现拦截器以及调用拦截器的 intercept 方法的 小伙伴先按 ...
- SpringMVC 中的Interceptor 拦截器
1.配置拦截器 在springMVC.xml配置文件增加: <mvc:interceptors> <!-- 日志拦截器 --> <mvc:interceptor> ...
- Mybatis之拦截器原理(jdk动态代理优化版本)
在介绍Mybatis拦截器代码之前,我们先研究下jdk自带的动态代理及优化 其实动态代理也是一种设计模式...优于静态代理,同时动态代理我知道的有两种,一种是面向接口的jdk的代理,第二种是基于第三方 ...
- MyBatis实现拦截器分页功能
1.原理 在mybatis使用拦截器(interceptor),截获所执行方法的sql语句与参数. (1)修改sql的查询结果:将原sql改为查询count(*) 也就是条数 (2)将语句sql进行拼 ...
- Spring中的Interceptor 拦截器 专题
spring-webmvc-4.3.14.RELEASE.jar org.springframework.web.servlet.DispatcherServlet#doDispatch /** * ...
随机推荐
- 用Jmeter做性能测试,之后报表展示
https://octoperf.com/blog/2017/10/19/how-to-analyze-jmeter-results/ 看到性能测试平台的开发,我在想需要什么功能,报表需要什么样子的 ...
- orm多表查询基于双下划线
###########基于双下划线的跨表查询(基于join实现的)############# key: 正向查询按字段,反向查询按表名小写 1.查询python这本书出版社的名字 ret = Book ...
- 运维CMDB建设思路
在我们日常的运维工作中,面对着大量的基础设施和软件服务,该如何管理?这个管理的原则又是什么?粒度该如何控制?我们是否可以建立一个统一的标准模型来管理以上对象?管理过程中,如何降低人力成本?资源对象的生 ...
- 大数据之路week06--day03(jdk8新特性 Lambda表达式)
为什么使用Lambda表达式?(做为初学者接触这个新的语法,会很懵逼,说道理,我在接触到这一块的时候,语法规则我看到了也很懵逼,因为这个和逻辑的关系不是很大,但就是作为一种新的语法出现,一时间很难接受 ...
- flask中使用ajax 处理前端请求 弹框展示
菜小鱼初次使用 ajax,想前端提交数据,后端处理后,将结果以弹框的形式展示,在网上查看了好多,不停的调试,终于调通了 html: <html> <head></head ...
- CQOI2005 三角形面积并 和 POJ1177 Picture
1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 1664 Solved: 443[Submit][Stat ...
- 【xsy1103】随机数表(RanMat)矩阵快速幂
题目大意:你生成了一个随机数表,生成机制是这样子的: $a[i]=A1a[i-1]+A2(2≤i≤m)$ $b[i]=B1b[i-1]+B2(2≤i≤m)$ $M[1][y]=a[y]%P,(1≤y≤ ...
- 2.3 vue配置(上)
rm,在打包之前把上一次打包之后的东西删掉,然后webpack重新打包 通过DefinePlugin形成一个环境变量 HTML打包插件
- MySQL 日期、字符串、时间戳互转
背景 原文地址:https://www.cnblogs.com/jhy-ocean/p/5560857.html 平时比较常用的时间.字符串.时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去 ...
- [ZJOI2009] 硬币游戏(找规律)
题目 洛谷传送门 题解 把1/21/21/2转化成0/10/10/1,所以直接可以异或. 对于长度为nnn的0/10/10/1数列,发现每变换2k(k>1)2^k(k>1)2k(k> ...