4. 插件开发原理

4. 插件开发原理

插件原理没有搞懂, 不要进行插件的开发, 容易导致很严重的问题

1. 插件概述

​ 插件是用来改变或者扩展mybatis的原有的功能,mybaits的插件就是通过继承Interceptor拦截器实现的;在没有完全理解插件之前禁止使用插件对mybaits进行扩展,又可能会导致严重的问题;

mybatis中能使用插件进行拦截的接口和方法如下:

  • Executor(update、query 、 flushStatment 、 commit 、 rollback 、 getTransaction 、 close 、 isClose)
  • StatementHandler(prepare 、 paramterize 、 batch 、 update 、 query)
  • ParameterHandler( getParameterObject 、 setParameters )
  • ResultSetHandler( handleResultSets 、 handleCursorResultSets 、 handleOutputParameters )

2. 插件开发快速入门

定义一个阈值,当查询操作运行时间超过这个阈值记录日志供运维人员定位慢查询,插件实现步骤:

1.实现Interceptor接口方法

2.确定拦截的签名

3.在配置文件中配置插件

4.运行测试用例

org.apache.ibatis.plugin.Interceptor

public interface Interceptor {

    //执行拦截逻辑的方法
    Object intercept(Invocation invocation) throws Throwable;

    //target是被拦截的对象,它的作用就是给被拦截的对象生成一个代理对象
    Object plugin(Object target);

    //读取在plugin中设置的参数
    void setProperties(Properties properties);

}

实现代码

@Intercepts(
    {@Signature(type = StatementHandler.class, 
                method = "query", 
                args = {Statement.class, ResultHandler.class})
    //	@Signature(type=StatementHandler.class,method="query",args={MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class})
})
public class ThresholdInterceptor implements Interceptor {

    private long threshold;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        long begin = System.currentTimeMillis();
        Object ret = invocation.proceed();
        long end = System.currentTimeMillis();
        long runTime = end - begin;
        if (runTime >= threshold) {
            Object[] args = invocation.getArgs();
            Statement stat = (Statement)args[0];
            MetaObject metaObjectStat = SystemMetaObject.forObject(stat);
            PreparedStatementLogger statementLogger = 
                (PreparedStatementLogger)metaObjectStat.getValue("h");
            Statement statement = statementLogger.getPreparedStatement();
            System.out.println("sql语句:“" + statement.toString() + "”执行时间为:" 
                               + runTime + "毫秒,已经超过阈值!");
        }
        return ret;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this); // 封装
    }

    @Override
    public void setProperties(Properties properties) { // 读取参数配置
        this.threshold = Long.valueOf(properties.getProperty("threshold"));
    }

}

配置

<plugins>
    <plugin interceptor="com.len.mybatis.Interceptors.ThresholdInterceptor">
        <property name="threshold" value="10"/>
    </plugin>
</plugins>

源码分析之责任链模式

责任链模式:就是把一件工作分别经过链上的各个节点,让这些节点依次处理这个工作;和装饰器模式不同,每个节点都知道后继者是谁;适合为完成同一个请求需要多个处理类的场景;

image-20220703213826795

要素分析

  • Handler:定义了一个处理请求的标准接口;
  • ConcreteHandler:具体的处理者,处理它负责的部分,根据业务可以结束处理流程,也可以将请求转发给它的后继者;
  • client :发送者,发起请求的客户端;

责任链模式优点:

  • 降低耦合度。它将请求的发送者和接收者解耦。
  • 简化了对象。使得对象不需要知道链的结构。
  • 增强给对象指派职责的灵活性。通过改变链内的成员或者调动它们的次序,允许动态地新增或者删除责任。
  • 增加新的请求处理类很方便。

3. mybatis插件模块源码分析

参考连接

Mybatis-PageHelper/Interceptor.md at master · pagehelper/Mybatis-PageHelper · GitHub

1.插件的初始化 (XMLConfigBuilder.pluginElement)

2.插件的加载 (Configuration.new*方法,四大对象的创建)

3.插件的调用 (Plugin. wrap、 Plugin. invoke)

public class Configuration {
	/*插件集合*/
	protected final InterceptorChain interceptorChain = new InterceptorChain();
	
    public List<Interceptor> getInterceptors() {
        return interceptorChain.getInterceptors();
    }
    
    public void addInterceptor(Interceptor interceptor) {
        interceptorChain.addInterceptor(interceptor);
    }
}	

public class InterceptorChain {
	// 使用list数据结构, 所以责任链的顺序就是配置的顺序
    private final List<Interceptor> interceptors = new ArrayList<>();
}

image-20220703214714358

org.apache.ibatis.session.Configuration#newExecutor(org.apache.ibatis.transaction.Transaction, org.apache.ibatis.session.ExecutorType)

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);
    }
    //如果有<cache>节点,通过装饰器,添加二级缓存的能力
    if (cacheEnabled) {
        executor = new CachingExecutor(executor);
    }
    //通过interceptorChain遍历所有的插件为executor增强,添加插件的功能
    executor = (Executor)interceptorChain.pluginAll(executor);
    return executor;
}
public class InterceptorChain {

    private final List<Interceptor> interceptors = new ArrayList<>();

    public Object pluginAll(Object target) {
        for (Interceptor interceptor : interceptors) {
            target = interceptor.plugin(target);
        }
        return target;
    }

    public void addInterceptor(Interceptor interceptor) {
        interceptors.add(interceptor);
    }

    public List<Interceptor> getInterceptors() {
        return Collections.unmodifiableList(interceptors);
    }

}

插件什么时候被代理的?

org.apache.ibatis.builder.xml.XMLConfigBuilder#parseConfiguration

private void parseConfiguration(XNode root) {
    try {
...
        //解析<plugins>节点
        pluginElement(root.evalNode("plugins"));
...
    } catch (Exception e) {
        throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: "
                                   + e, e);
    }
}
private void pluginElement(XNode parent) throws Exception {
    if (parent != null) {
        //遍历所有的插件配置
        for (XNode child : parent.getChildren()) {
            //获取插件的类名
            String interceptor = child.getStringAttribute("interceptor");
            //获取插件的配置
            Properties properties = child.getChildrenAsProperties();
            //实例化插件对象
            Interceptor interceptorInstance = (Interceptor)resolveClass(interceptor).newInstance();
            //设置插件属性
            interceptorInstance.setProperties(properties);
            //将插件添加到configuration对象,底层使用list保存所有的插件并记录顺序
            configuration.addInterceptor(interceptorInstance);
        }
    }
}

这里并没有生成代理, 说明在解析plugins元素时候并没有生成代理, 那么是在使用时候被代理了

org.apache.ibatis.session.Configuration#newExecutor(org.apache.ibatis.transaction.Transaction, org.apache.ibatis.session.ExecutorType)

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
...
    //通过interceptorChain遍历所有的插件为executor增强,添加插件的功能
    executor = (Executor)interceptorChain.pluginAll(executor);
    return executor;
}
public class InterceptorChain {

    private final List<Interceptor> interceptors = new ArrayList<>();

    public Object pluginAll(Object target) {
        for (Interceptor interceptor : interceptors) {
            target = interceptor.plugin(target); // 重点是这个代码
        }
        return target;
    }
}

com.len.mybatis.Interceptors.ThresholdInterceptor

@Override
public Object plugin(Object target) {
    return Plugin.wrap(target, this); // 使用了mybatis提供的工具生成代理
}

所以这个代理生成方式就是, 具体实现的方法需要提供代理生成的方法plugin, mybatis在使用插件时候会通过责任链的pluginAll方法回调这个plugin生成代理

4. Mybatis分页插件PageHelper

  1. 分页插件的使用;
    中文文档:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md
    使用手册:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md

  2. 分页插件的注意事项;
    注意事项:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/Important.md

  3. 使用

    依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.4</version>
</dependency>

配置

<plugins>
    <!-- com.github.pagehelper为PageHelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
        <property name="pageSizeZero" value="true"/>
	</plugin>
</plugins>
@Test
public void testManyParamQuery() {
    // 2.获取sqlSession
    SqlSession sqlSession = sqlSessionFactory.openSession();
    // 3.获取对应mapper
    TUserMapper mapper = sqlSession.getMapper(TUserMapper.class);

    String email = "qq.com";
    Byte sex = 1;

    // 第一种方式使用map
    //    Map<String, Object> params = new HashMap<String, Object>();
    //    params.put("email", email);
    //    params.put("sex", sex);
    //    List<TUser> list1 = mapper.selectByEmailAndSex1(params);
    //    System.out.println(list1.size());

    // 第二种方式直接使用参数
    Page<TUser> startPage = PageHelper.startPage(2, 3);
    List<TUser> list2 = mapper.selectByEmailAndSex2(email, sex);
    System.out.println(list2.size());
    //    return startPage;

    // 第三种方式用对象
    //    EmailSexBean esb = new EmailSexBean();
    //    esb.setEmail(email);
    //    esb.setSex(sex);
    //    List<TUser> list3 = mapper.selectByEmailAndSex3(esb);
    //    System.out.println(list3.size());
}

注意事项:

Page startPage = PageHelper.startPage(2, 3);
List list2 = mapper.selectByEmailAndSex2(email, sex);

  1. 这两句要连着, 不要加任何语句

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b4nDak4T-1659111130373)(C:/Users/hgy/AppData/Roaming/Typora/typora-user-images/image-20220704005549259.png)]

image-20220704005631816

  1. 不支持嵌套结果集的查询
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岁月玲珑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值