mybatis的拦截器的执行流程(以StatementHandler类型的拦截器为例)

1.    获取到dao层的动态代理对象org.apache.ibatis.binding.MapperProxy,并执行其invoke方法,该方法是入口

2.    执行org.apache.ibatis.binding.MapperMethod.execute()

3.    执行org.mybatis.spring.SqlSessionTemplate.selectList()

4.    通过动态代理的方式执行SqlSessionTemplate的内部代理类SqlSessionInterceptor的invoke方法

5.    SqlSessionInterceptor的invoke方法中首先获取SqlSession

6.    执行org.mybatis.spring.SqlSessionUtils.getSqlSession获取SqlSession

7.    执行org.apache.ibatis.session.defaults.DefaultSqlSessionFactory. openSessionFromDataSource()方法

8.    执行org.apache.ibatis.session.Configuration. newExecutor,得到org.apache.ibatis.session.defaults.DefaultSqlSession,其中的执行器是SimpleExecutor

9.    回到SqlSessionTemplate的内部代理类SqlSessionInterceptor的invoke方法中,通过反射的方式继续继续执行selectList()方法

10.    执行DefaultSqlSession的selectList()方法,

11.    执行org.apache.ibatis.executor.SimpleExecutor.doQuery()方法

12.    执行org.apache.ibatis.session.Configuration. newStatementHandler()方法获取StatementHandler

13.    通过StatementHandler statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);获取StatementHandler

14.    InterceptorChain中的 pluginAll中遍历所有拦截器
        通过调用拦截器的plugin()方法生成动态代理对象;
        第一次循环的代理对象是原始的StatementHandler返回的是Plugin类型的代理类P1
        第二次循环代理的对象是P1返回的是Plugin类型的代理类P2

15.    回到org.apache.ibatis.executor.SimpleExecutor.doQuery()中,执行handler.prepare方法时,因为handler其实是Plugin类型的动态代理对象,且代理的方法是prepare,所以会先执行Plugin对象的invoke()方法,在Plugin对象的invoke()方法中首先调用了拦截器的intercept()方法,接着以反射的方式调用了代理对象的prepare方法

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值