MyBatis插件开发原理

  MyBatis采用责任链模式,通过动态代理组织多个插件(拦截器),通过这些插件可以改变MyBatis的默认行为(诸如SQL重写之类的),由于插件会深入到MyBatis的核心,因此在编写自己的插件前最好了解下它的原理,以便写出安全高效的插件。

  MyBatis在四大对象的创建过程中,都会有插件进行介入。插件可以利用动态代理机制一层层的包装目标对象,而实现目标对象在执行目标方法之前进行拦截的效果。
  插件介入指的是:创建过程中都会涉及到调用interceptChain.pluginAll()方法对四大对象进行重新包装,返回一个代理对象。
  MyBatis 允许在已映射语句执行过程中的某一点进行拦截调用,默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:

  1、拦截执行器的方法:Executor(update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
  2、拦截参数的处理:ParameterHandler(getParameterObject, setParameters)
  3、拦截结果集的处理:ResultSetHandler(handleResultSets, handleOutputParameters)
  4、拦截Sql语法构建的处理:StatementHandler(prepare, parameterize, batch, update, query)

  默认情况下,Mybatis允许使用插件来拦截的接口和方法包括以下几个:

  插件开发是基于动态代理实现的,所有有必要对动态代理有所了解。

  我们可以看一下MyBatis是怎么创建这四大接口对象的。找到源码BaseStatementHandler  

 this.parameterHandler = configuration.newParameterHandler(mappedStatement, parameterObject, boundSql);
this.resultSetHandler = configuration.newResultSetHandler(executor, mappedStatement, rowBounds, parameterHandler, resultHandler, boundSql);

  进入configuration类,下面几处都是在创建newParameterHandler,ResultSetHandler,StatementHandler这几个对象,在调用的过程中,大家都看到了都使用了interceptorChain.pluginAll方法分别对每一个对象进行了重新包装并返回

 public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
return parameterHandler;
} public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler,
ResultHandler resultHandler, BoundSql boundSql) {
ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
return resultSetHandler;
} public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
return statementHandler;
}

  点进interceptorChain.pluginAll方法里面

 /**
*每一个拦截器对目标类都进行一次代理
*@target
*@return 层层代理后的对象
**/
public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target; }

  这一段代码可以看到:获取所有的Interceptor(拦截器),我们如果需要自定义拦截器就得实现Interceptor这个接口。然后调用interceptor.plugin(target);返回target包装之后的对象。

  所以,我们可以使用插件为目标对象创建一个代理对象,这Spring的AOP一样,其实都是动态代理,面向切面的编程。

MyBatis插件开发 

  下面我们通过案例为StatementHandler创建代理对象

  1、新建一个maven工程,引入mybatis依赖及相关数据库依赖

    

  2、新建一个MyFirstPlugin拦截器类,并且实现Interceptor接口

 package com.test.mybatis.plugin;

 import java.util.Properties;

 import org.apache.ibatis.executor.statement.StatementHandler;
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; /**
* 完成插件签名:
* 告诉MyBatis当前插件用来拦截哪个对象的哪个方法
* @Intercepts(org.apache.ibatis.plugin.Intercepts)和
* 签名注解@Signature(org.apache.ibatis.plugin.Signature),这两个注解用来配置拦截器要拦截的接口的方法。
*
* @Intercepts注解中的属性是一个@Signature(签名)数组,可以在同一个拦截器中同时拦截不同的接口和方法。
*
* @Signature中
* type:设置拦截的接口,可选值是4个:Executor、ParameterHandler、ResultSetHandler、StatementHandler
* method:设置拦截接口中的方法名,需要和接口匹配
* args:设置拦截方法的参数类型数组,通过方法名和参数类型可以确定唯一一个方法
*/
@Intercepts(
{
@Signature(type=StatementHandler.class,method="parameterize",args=java.sql.Statement.class)
})
public class MyFirstPlugin implements Interceptor { /**
* intercept:拦截: 拦截目标对象的目标方法的执行;
*/
public Object intercept(Invocation invocation) throws Throwable {
// TODO Auto-generated method stub
System.out.println("MyFirstPlugin...intercept:" + invocation.getMethod());
// 执行目标方法
Object proceed = invocation.proceed();
// 返回执行后的返回值
return proceed;
} /**
* plugin: 包装目标对象的:包装:为目标对象创建一个代理对象
*/
public Object plugin(Object target) {
// TODO Auto-generated method stub
// 我们可以借助Plugin的wrap方法来使用当前Interceptor包装我们目标对象
System.out.println("MyFirstPlugin...plugin:mybatis将要包装的对象" + target);
Object wrap = Plugin.wrap(target, this);
// 返回为当前target创建的动态代理
return wrap;
} /**
* setProperties:
* 将插件注册时 的property属性设置进来
*/
public void setProperties(Properties properties) {
// TODO Auto-generated method stub
System.out.println("插件配置的信息:" + properties);
} }

  3、在mybatis的全局配置文件中配置

 <!--plugins:注册插件 -->
<plugins>
<plugin interceptor="com.test.mybatis.plugin.MyFirstPlugin">
<property name="username" value="root" />
<property name="password" value="123456" />
</plugin>
</plugins>

  4、编辑测试类TestMybatis.java

 public static void main(String[] args) throws IOException {

     // 获取SqlSessionFactory
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession();
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); Employee employee01 = mapper.selectByPrimaryKey(1);
System.out.println(employee01.getId()); } finally {
session.close();
}
}

  5、运行结果如下:

    

插件开发总结

  插件开发步骤如下

    1、编写插件实现Interceptor接口,并使用@Intercepts注解完成插件签名

 @Intercepts({                                  @Signature(type=StatementHandler.class,method="parameterize",args=java.sql.Statement.class)
})
public class MyFirstPlugin implements Interceptor {

    2、在全局配置文件中注册插件

 <!--plugins:注册插件 -->
<plugins>
<plugin interceptor="com.test.mybatis.plugin.MyFirstPlugin">
<property name="username" value="root" />
<property name="password" value="123456" />
</plugin>
</plugins>

  插件的原理

    按照插件注解声明,按照插件配置顺序调用插件plugin方法,生成被拦截对象的动态代理
    多个插件依次生成目标对象的代理对象,层层包裹,先声明的先包裹;形成代理链
    目标方法执行时依次从外到内执行插件的intercept方法

  插件的作用    

    1、可以统计SQL的执行时间

    2、可以进行分页操作,如插件:Mybatis-PageHelper

    ......

【Mybatis】MyBatis之插件开发(十)的更多相关文章

  1. MyBatis基础入门《十九》动态SQL(set,trim)

    MyBatis基础入门<十九>动态SQL(set,trim) 描述: 1. 问题 : 更新用户表数据时,若某个参数为null时,会导致更新错误 2. 分析: 正确结果: 若某个参数为nul ...

  2. MyBatis基础入门《十八》动态SQL(if-where)

    MyBatis基础入门<十八>动态SQL(if-where) 描述: 代码是在<MyBatis基础入门<十七>动态SQL>基础上进行改造的,不再贴所有代码,仅贴改动 ...

  3. MyBatis基础入门《十六》缓存

    MyBatis基础入门<十六>缓存 >> 一级缓存 >> 二级缓存 >> MyBatis的全局cache配置 >> 在Mapper XML文 ...

  4. MyBatis基础入门《十五》ResultMap子元素(collection)

    MyBatis基础入门<十五>ResultMap子元素(collection) 描述: 见<MyBatis基础入门<十四>ResultMap子元素(association ...

  5. MyBatis基础入门《十四》ResultMap子元素(association )

    MyBatis基础入门<十四>ResultMap子元素(association ) 1. id: >> 一般对应数据库中改行的主键ID,设置此项可以提高Mybatis的性能 2 ...

  6. MyBatis基础入门《十二》删除数据 - @Param参数

    MyBatis基础入门<十二>删除数据 - @Param参数 描述: 删除数据,这里使用了@Param这个注解,其实在代码中,不使用这个注解也可以的.只是为了学习这个@Param注解,为此 ...

  7. MyBatis基础入门《十 一》修改数据

    MyBatis基础入门<十 一>修改数据 实体类: 接口类: xml文件: 测试类: 测试结果: 数据库: 如有问题,欢迎纠正!!! 如有转载,请标明源处:https://www.cnbl ...

  8. MyBatis基础入门《十》添加数据

    MyBatis基础入门<十>添加数据 描述: 修改了实体类:TblClient.java,将其字段:cbirthday 由String类型改成了Date类型. TblClient.java ...

  9. MyBatis实现与插件开发

    分析源码之前也需要源码下载并安装到本地仓库和开发工具中,方便给代码添加注释:安装过程和mybatis源码的安装过程是一样的,这里就不再重复描述了:下载地址:https://github.com/myb ...

  10. springmvc 项目完整示例04 整合mybatis mybatis所需要的jar包 mybatis配置文件 sql语句 mybatis应用

    百度百科: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBat ...

随机推荐

  1. Python正则提取数据单引号内数据,并判断是否是空列表(是否提取到数据)

    #coding=utf- import re string1="asdfgh'355'dfsfas" string2="fafafasfasdfasdf" pa ...

  2. 错误:找不到或无法加载主类(myEclipse and IDEA)

    一.myEclipse: 一个简单的main类启动时报无法加载主类的处理方法 1.找到Prolems--->Error--->右键Delete 2.点击项目,右键刷新 3.点击导航栏上的P ...

  3. Linux命令基础1-环境介绍

    1.linux的简单历史 1)先有unix,后来有linux 2)linux操作系统是开源和免费的,里面的软件可能部分要收费 3)linux有不同发行版本,redhat,centos等. 4)1991 ...

  4. Yarn节点及作用

    1.yarn中的角色:ResourceManager.NodeManager.ApplicationMaster. ResourceManager:集群计算资源的分配,启动ApplicationMas ...

  5. PostgreSQL 锁机制浅析

    锁机制在 PostgreSQL 里非常重要 (对于其他现代的 RDBMS 也是如此).对于数据库应用程序开发者(特别是那些涉及到高并发代码的程序员),需要对锁非常熟悉.对于某些问题,锁需要被重点关注与 ...

  6. 备份docker运行的gitlab

    #!/bin/bash data=$(date "+%Y-%m-%d %H:%M:%S") gitBak='/data/gitlab/data/backups' delFile=` ...

  7. Hadoop上 Hive 操作

    数据dept表的准备: --创建dept表 CREATE TABLE dept( deptno int, dname string, loc string) ROW FORMAT DELIMITED ...

  8. The Last Goodbye 电影《霍比特人3:五军之战》插曲

    https://music.163.com/#/song?id=29755223 I saw the light fade from the sky我看到天空褪去色彩On the wind I hea ...

  9. P4425 【[HNOI/AHOI2018]转盘】

    颂魔眼中的一眼题我大湖南竟无一人\(AC\) 首先我们考虑一个性质:我们肯定存在一种最优解,满足从某个点出发,一直往前走,不停下来. 证明:我们假设存在一种最优解,是在\(t_i\)的时候到达\(a\ ...

  10. (23)打鸡儿教你Vue.js

    实例: 模板语法 vue-router,vuex以及调式方法介绍 打包部署: npm run build Webpack 目前无论在求职还是工作中,使用越来越普及.而想要学懂,学会Webpack更绝非 ...