1.Mybatis-Plugin的设计思路

听起来一个挺神奇的单词,插件。说白了就是使用了Jdk自带的动态代理.在需要的时候进行代理。AOP怎么用,他就怎么用。

Plugin类等价于InvocationHandler.Mybatis在调用Jdk代码如下:

 Proxy.newProxyInstance(
type.getClassLoader(),
interfaces,
new Plugin(target, interceptor, signatureMap));

Plugin类继承InvocationHandler接口

public class Plugin implements InvocationHandler {

  private final Object target;
private final Interceptor interceptor;
private final Map<Class<?>, Set<Method>> signatureMap; private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {
this.target = target;
this.interceptor = interceptor;
this.signatureMap = signatureMap;
} @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

invoke方法执行的时候,也就是intercept被调用的时候.

用一句成语来讲,借鸡下蛋。借尸还魂(这个词还有点吓人)

2.写一个例子.使用Intecept接口,但不应用在Mybatis框架内。

思路:① 写一个接口+一个类②写一个Inteceptor对该接口进行增强。

--------------------接口----------------------------------

package org.mybatis.example.plugin;

public interface HelloWorld {

    void sayHello(Integer v);
}

-------------------实现类---------------------------------

package org.mybatis.example.plugin;

public class HelloWorldImpl implements HelloWorld {

    public void sayHello(Integer v){
System.out.println("Hello World!");
} public static void main(String[] args) { PluginUsedWithoutMyBatis plugin = new PluginUsedWithoutMyBatis(); HelloWorld helloWorld = (HelloWorld) plugin.plugin(new HelloWorldImpl()); helloWorld.sayHello(1);
}
}

--------------------Interceptor-----------------------------------

@Intercepts(@Signature(type= HelloWorld.class,method="sayHello",args={Integer.class}))
public class PluginUsedWithoutMyBatis implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable { String methodName =invocation.getMethod().getName(); Object result =null;
if(methodName.equals("sayHello")){ System.out.println("method Begin:"); result =invocation.proceed();
System.out.println("method Begin:");
}else{
result =invocation.proceed();
} return result;
} @Override
public Object plugin(Object target) {
return Plugin.wrap(target,this);
} @Override
public void setProperties(Properties properties) { } public static void main(String[] args) { }
}

执行HelloWorldImpl中的Main方法,得到一段朴素的代码增强结果。

method Begin:
Hello World!
method Begin:

PluginUsedWithoutMyBatis类中需要注意的:

@Intercepts、@Signature这两个注解,限定了可以增强的接口,以及方法名称以及参数。
理解成过滤器。在Mybatis的Plugins配置中,所有的Plugin都配置在一起。根据Signature决定Interceptor对谁增强。

问题:

如果两个Interceptor都对同一个对象增强,结果如何?

写了另外一个代理类,对HelloWorldImpl进行二次增强,结果如下:

enhance Second Begin:
enhance First Begin:
Hello World!
enhance First end:
enhance Second end:

可以看到两次增强的结果。

3、回到Mybatis,是否可以对所有的MyBatis接口增强?

Mybatis的Configruation类,只在几个对象的创建时做了增强

StatementHandler 、ResultSetHandler 、ParameterHandler 、Executor 
  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;
} 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 ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
return parameterHandler;
} public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor 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 (cacheEnabled) {
executor = new CachingExecutor(executor);
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}

4、现有增强插件的实现:

Executor的query、update时候可以监控sql执行时间。监控慢SQL
Executor的query、update catch异常,当发生异常时,可以打印sql以及执行参数。以及具体错误消息。

mybatis的plugin的更多相关文章

  1. Mybatis插件Plugin

    Mybatis开源Plugin中最熟知的pagehelper,重点made in China 很多人开始用pagehelper时候,肯定很纳闷,以mysql为例,明明没有加limit语句,为什么打印出 ...

  2. Mybatis辅助神器-MyBatis Log Plugin

    1. 问题描述 Java操作数据库的两台流行天王-mybatis和hibernate,mytabis和hibernate的区别不想废话了,以前用hibernate,最近几年一直用的mybatis,目前 ...

  3. IDEA优秀插件分享之---Mybatis Log Plugin

    小伙伴们在使用mybatis的时候有时候会出现一些sql异常,这个时候就需要对执行的sql语句进行检查.然而mybatis一般使用log4j打印执行的sql语句,类型下面这种的: 这个时候如果sql语 ...

  4. Mybatis Log plugin 破解!!!

    前言 今天重新装了IDEA2020,顺带重装了一些插件,毕竟这些插件都是习惯一直在用,其中一款就是Mybatis Log plugin,按照往常的思路,在IDEA插件市场搜索安装,艹,眼睛一瞟,竟然收 ...

  5. Mybatis log plugin 破解!!!

    前言 今天重新装了IDEA2020,顺带重装了一些插件,毕竟这些插件都是习惯一直在用,其中一款就是Mybatis Log plugin,按照往常的思路,在IDEA插件市场搜索安装,艹,眼睛一瞟,竟然收 ...

  6. 我常用的插件之“Mybatis Log plugin”sql日志格式转化

    前言 今天重新装了IDEA2020,顺带重装了一些插件,毕竟这些插件都是习惯一直在用,其中一款就是Mybatis Log plugin,按照往常的思路,在IDEA插件市场搜索安装,艹,眼睛一瞟,竟然收 ...

  7. 互联网轻量级框架SSM-查缺补漏第八天(MyBatis插件plugin使用及原理)

    简言:今天进行第八天的记录(只是写了八天).有的时候看的多,有的时候看的少,看的少的时候就攒几天一起写了.而今天这个插件我昨天写了一下午,下班没写完就回去了,今天把尾收了,再加上一个过程图方便下面原理 ...

  8. Mybatis之plugin插件设计原理

    大多数框架,都支持插件,用户可通过编写插件来自行扩展功能,Mybatis也不例外. 我们从插件配置.插件编写.插件运行原理.插件注册与执行拦截的时机.初始化插件.分页插件的原理等六个方面展开阐述. 一 ...

  9. 【Mybtais】Mybatis 插件 Plugin开发(一)动态代理步步解析

    需求: 对原有系统中的方法进行'拦截',在方法执行的前后添加新的处理逻辑. 分析: 不是办法的办法就是,对原有的每个方法进行修改,添加上新的逻辑:如果需要拦截的方法比较少,选择此方法到是会节省成本.但 ...

随机推荐

  1. soapUI的安装及破解

    1.下载安装包 SoapUI SoapUI-Pro-x64-5.1.2.exe Protection-4.6.jar scz.key 2.安装SoapUI-Pro-x64-5.1.2.exe 3.拷贝 ...

  2. CSS第二篇

    给导航设置圆角的代码: c3 里面的属性:border-radius:7px: 1补充盒子问题 (盒子边框紧邻变粗问题)将两个盒子边框重叠并且设置定位提高层级避免盒子重叠,鼠标放上去只显示一个盒子的边 ...

  3. 网赚app

    网赚app有很多目前来说做的比较好的赚的比较多的有四款推荐 宝石星球下载地址:http://www.baoshixingqiu.com/redPacket?key=548341 雪梨网APP下载地址 ...

  4. AI之旅(1):出发前的热身运动

    前置知识   无 知识地图 自学就像在海中游泳   当初为什么会想要了解机器学习呢,应该只是纯粹的好奇心吧.AI似乎无处不在,又无迹可循.为什么一个程序能在围棋的领域战胜人类,程序真的有那么聪明吗?如 ...

  5. 非root用户安装python3

    1.下载源码 wget -c https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz 解压: tar xzf Python-3.7.1.tgz ...

  6. Dart 的function

    方法的定义 返回类型 方法名 (参数1 ,参数2 ,...){ 方法体 返回值 } => 的使用

  7. 我的代码-flask

    import pickleimport flaskimport pandas as pdimport jsonfrom flask import Flaskfrom flask import requ ...

  8. 为什么以sys无法远程登录数据库

    今天通过pl/sql连接一个数据库,普通用户能连接上,但是sys就连接不上,告诉我用户名密码错误.之前其实遇到过这个问题,然后就开始查找原因.整个过程记录如下,结果是扎心的,过程也不是完全都懂.记下来 ...

  9. Java BitSet解决海量数据去重

    先提一个问题,怎么在40亿个整数中找到那个唯一重复的数字? 第一想法就是Set的不可重复性,依次把每个数字放入HashSet中,当放不去进去的时候说明这就是重复的数字,输出这个数字. if(hs.co ...

  10. Android studio 中添加依赖model时依赖所需的准备

    例如向app中添加依赖core: core要做如下修改: 1.将core中build.gradle中   修改为  . 2.将core中的 applicationId 注释掉.