Execution order java-mybaits-013-mybatis-Interceptor- interceptors

I. Overview

  Known interceptor can intercept four types: Executor, ParameterHandler, ResultSetHandler, StatementHandler.

1.1 execution sequence, different types of interceptors

Background: Different types

Project Address: https://github.com/bjlhx15/mybatis.git  in mybatis-interceptor-001 infrastructure projects

Sequence [Executor → StatementHandler → ParameterHandler → ResultSetHandler]

1, write the code interceptor

  

2, mybatis.xml Configuration

    <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, a test output

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

  We can see, regardless of the configuration file, execute the following order: [Executor → StatementHandler → ParameterHandler → ResultSetHandler]

    

1.2, different positions of the same type

Background: Unlike Type Location

Project Address: https://github.com/bjlhx15/mybatis.git  in mybatis-interceptor-002 infrastructure projects

Sequence: [Array vertical position before reverse processing knockdown 3> before treatment intercept 2> 1 knockdown before treatment> After executor.query ()> 1 knockdown treatment> After 2 knockdown treatment> 3] knockdown after treatment

1, write blocker

  

2, write xml

    <plugins>
        <!-- 拦截器配置 -->
        <plugin interceptor="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.Test1Interceptor"/>
        <plugin interceptor="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.Test2Interceptor"/>
    </plugins> 

3, a test output

Test2Interceptor
Test1Interceptor

4、why

  View mybatis configuration org.apache.ibatis.session.Configuration. Created

    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 out in a new class Configuration. It is equivalent to the mybatis-config <plugins> </ plugins>

  Check the order of addition 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;
    }

  You can see interceptorChain interceptors order is to configure the order

  

  But after proxy,

  

3 Pretreatment intercept> 2 knockdown Pretreatment> 1 interception Pretreatment> executor.query ()> 1 knockdown after treatment> After 2 knockdown treatment> 3 knockdown after treatment

1.3, the same type of arrangement positions of the different

Background: There is spring in the configuration, as well as configure 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上下位置,输出一致

 

  

 

  

 

  

 

  

 

 

 

 

 

 

 

但是

Guess you like

Origin www.cnblogs.com/bjlhx/p/11263268.html