I. 개요
집행자, ParameterHandler, ResultSetHandler, StatementHandler : 알려진 인터셉터는 네 가지 유형을 가로 챌 수 있습니다.
1.1 실행 순서, 인터셉터의 종류
배경 : 다른 유형
프로젝트 주소 : https://github.com/bjlhx15/mybatis.git 의 MyBatis-요격-001 인프라 프로젝트에
시퀀스 [집행자 → StatementHandler → ParameterHandler → ResultSetHandler]
1, 코드 인터셉터 쓰기
2, mybatis.xml 구성
< 플러그인 > <! - 拦截器配置-> < 플러그인 인터셉터 = "com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.TestTypeInterceptorParameterHandler" /> < 플러그인 인터셉터 = "com.github.bjlhx15.mybatis.readwrite .split.mybatisinterceptor.TestTypeInterceptorResultSetHandler " /> < 플러그인 인터셉터 ="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.TestTypeInterceptorStatementHandler " /> < 플러그인 인터셉터 ="com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor .TestTypeInterceptorExecutor "/> </플러그인 >
(3) 시험 출력
TestTypeInterceptorExecutor
[2019년 7월 29일 10 : 37 : 25 : 540-HTTP-NIO-8080 간부-1] [INFO] - com.alibaba.druid.pool.DruidDataSource.init (DruidDataSource.java:930) - {은 dataSource -1} inited는
TestTypeInterceptorStatementHandler을
[2019년 7월 29일 10 : 37 : 29 : 827-HTTP-NIO-8080 간부-1] [DEBUG] - org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug (BaseJdbcLogger.java : 145) - ==> 준비 :없이 accountBalance에서 queryId에, ID, 이름, 버전, 밸런스로서 선택 '참'
TestTypeInterceptorParameterHandler
[37 : 31 : 10 177 2019년 7월 29일-HTTP-NIO-8080 간부-1] [DEBUG] - org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug (BaseJdbcLogger.java:145) - ==> 파라미터 :
TestTypeInterceptorResultSetHandler
우리는 다음과 같은 순서를 실행 구성 파일에 관계없이 볼 수 있습니다 : [집행자 → StatementHandler → ParameterHandler ResultSetHandler →]
동일한 유형의 1.2 상이한 위치
배경 : 입력 위치는 달리
프로젝트 주소 : https://github.com/bjlhx15/mybatis.git 의 MyBatis-요격-002 인프라 프로젝트에
서열 처리 후의 최저 [executor.query ()> 1 최저 처리 후 처리 전 처리 인터셉트 전에 역방향 프로세싱 최저 3> 2> 1 녹다운 ""전 어레이 수직 최저 2 처리> 후 3]
1, 쓰기 차단
2, 쓰기 XML
< 플러그인 > <! - 拦截器配置-> < 플러그인 인터셉터 = "com.github.bjlhx15.mybatis.readwrite.split.mybatisinterceptor.Test1Interceptor" /> < 플러그인 인터셉터 = "com.github.bjlhx15.mybatis.readwrite .split.mybatisinterceptor.Test2Interceptor " /> </ 플러그인 >
(3) 시험 출력
Test2Interceptor
Test1Interceptor
4, 왜
보기 구성 org.apache.ibatis.session.Configuration을 MyBatis로. 작성
공공 집행자 newExecutor (트랜잭션 거래는 ExecutorType는 ExecutorType) {
는 ExecutorType =는 ExecutorType == null이? this.defaultExecutorType :는 ExecutorType;
는 ExecutorType =는 ExecutorType == 널? ExecutorType.SIMPLE :는 ExecutorType;
개체 집행자;
경우 (ExecutorType.BATCH ==는 ExecutorType) {
실행기 = 새로운 실행기 (이 트랜잭션);
} 다른 경우 (ExecutorType.REUSE ==는 ExecutorType) {
실행기 = 새로운 ReuseExecutor (이 트랜잭션);
} 다른 {
집행 = 새로운 SimpleExecutor (이, 트랜잭션 (transaction));
}
경우 (this.cacheEnabled) {
실행 프로그램 새로운 CachingExecutor = ((실행자) 실행 프로그램);
}
실행기 실행기 = (실행자) this.interceptorChain.pluginAll (실행 프로그램);
집행자를 반환;
}
새로운 클래스 구성에서 interceptorChain. 그것은 MyBatis로 구성 - <플러그인> </ 플러그인>에 해당
또한 순서를 확인 pluginAll
민간 최종 목록 <인터셉터> 인터셉터 = 새로운 ArrayList를 (); 공개 InterceptorChain () { } 공용 개체 pluginAll (객체 타겟) { 인터셉터 인터셉터; 대 (I 달러 반복자 = 이 .interceptors.iterator (); I $ .hasNext () = 목표 interceptor.plugin (대상)) { 인터셉터 = (인터셉터) I $ 다음 내용 (); } 리턴 타겟; }
당신이 볼 수 interceptorChain 순서가 순서를 구성하는 것입니다 인터셉터
그러나 프록시 후,
3 전처리 인터셉트> 2 최저 전처리> 1 차단 전처리> executor.query ()> 처리 후의 최저 1> 2 처리 후의 최저 처리> 3 후 최저
1.3, 서로 다른의 배치 위치의 동일한 유형
배경 : 봄이 구성 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上下位置,输出一致
但是