前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MyBatis plugins插件(4)

MyBatis plugins插件(4)

作者头像
兜兜毛毛
发布2021-04-15 10:16:24
5280
发布2021-04-15 10:16:24
举报
文章被收录于专栏:兜兜毛毛兜兜毛毛

插件是MyBatis对外开放了四个接口,可以用于自定义扩展。

接口

可代理方法

说明

Executor

执行器,对事务、缓存等提供统一接口

update

执行update、insert、delete操作

query

执行select操作

flushStatements

在commit的时候自动调用,SimpleExecutor、ReuseExecutor、BatchExecutor处理不同

commit

提交事务

rollback

事务回滚

getTransaction

获取事务

close

结束或关闭事务

isClosed

判断事务是否关闭

ParameterHandler

参数处理器,负责为 PreparedStatement 的 sql 语句参数动态赋值

getParameterObject

获取参数

setParameters

设置参数

ResultSetHandler

结果集处理

handleResultSets

处理结果集

handleOutputParameters

处理存储过程出参

StatementHandler

四大组件中最重要的一个对象,负责操作 Statement 对象与数据库进行交流,在工作时还会使用 ParameterHandler 和 ResultSetHandler 对参数进行映射,对结果进行实体类的绑定

prepare

(BaseSatementHandler)SQL预编译

parameterize

设置参数

batch

批量处理

update

增删改操作

query

查询操作

以上4个接口在MyBatis中的工作流程如下图:

MyBatis实现自定义插件

创建自定义插件主要步骤:

  1. 编写插件代码实现Interceptor接口,设置要代理的方法
  2. 在mybatis-config.xml中注册插件

下边来简单做一个分表的插件,根据主键ID分,实现单数入<表名>表双数入<表名_1>表

代码语言:javascript
复制
/**
 * 简单分表,根据传入的主键ID,实现单数入<表名>表双数入<表名_1>表
 * @Author: maomao
 * @Date: 2021-04-09 17:28
 */
@Intercepts({
        @Signature(type = Executor.class, //表示要代理的接口类型
                method = "update",  //表示要代理接口的对应方法
                args = {MappedStatement.class, Object.class} //表示代理方法对应的参数列表,反射时使用(解决方法重载问题)
        ),
        @Signature(type = Executor.class,
                method = "query",
                args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
        )
})
public class SimpleTableInterceptor  implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        Set<SqlCommandType> typeSet = CollectionUtil.newHashSet(SqlCommandType.DELETE,SqlCommandType.INSERT,SqlCommandType.UPDATE);
        if (!typeSet.contains(mappedStatement.getSqlCommandType())) {
            return invocation.proceed();
        }
        //获得执行的sql语句
        BoundSql boundSql = mappedStatement.getBoundSql(invocation.getArgs()[1]);
        //sql语法解析工具,方便获取表名
        Statement statement = CCJSqlParserUtil.parse(boundSql.getSql());
        //获取参数
        Object parameter = boundSql.getParameterObject();
        JSON parameterJson = JSONUtil.parse(parameter);

        Long id = (Long) parameterJson.getByPath("id");
        //使用ID取余数,确定执行表名
        Long tableIndex = id % 2;

        LoggerUtil.printThread("tableIndex : " + tableIndex);
        
        Table table;
        String newSql,newTableName = null;
        if(statement instanceof Update){
            Update update = (Update) statement;
            table = update.getTable();
            if(tableIndex == 0){
                newTableName = table.getName() + "_1";
            }else if(table.getName().lastIndexOf("_1") > 0){
                newTableName =  table.getName().replace("_1","");
            }
            if(StrUtil.isNotEmpty(newTableName)){
                table.setName(newTableName);
            }
            newSql = update.toString();
        }else{
            Insert insert = (Insert) statement;
            table = insert.getTable();
            if(tableIndex == 0){
                newTableName = table.getName() + "_1";
            }else if(table.getName().lastIndexOf("_1") > 0){
                newTableName =  table.getName().replace("_1","");
            }
            if(StrUtil.isNotEmpty(newTableName)){
                table.setName(newTableName);
            }
            newSql = insert.toString();
        }


        LoggerUtil.printThread("新sql : " + newSql);

        // 自定义sqlSource
        SqlSource sqlSource = new StaticSqlSource(mappedStatement.getConfiguration(), newSql, boundSql.getParameterMappings());

        // 修改原来的sqlSource
        Field field = MappedStatement.class.getDeclaredField("sqlSource");
        field.setAccessible(true);
        field.set(mappedStatement, sqlSource);
        return invocation.proceed();
    }
}

在mybatis-config.xml中注册插件

代码语言:javascript
复制
<plugins>
    <plugin interceptor="com.freecloud.plug.mybatis.plugins.SimpleTableInterceptor"></plugin>
</plugins>

上边就可以简单的实现一个分表的逻辑,不需要修改任何业务代码。是不是非常方便。

插件的核心原理

那MyBatis是如何实现插件功能的呢?如果有多个插件它又是如何执行的呢?

插件的实现使用了动态代理、反射和责任链的方式实现。

下边我将抽出MyBatis的插件核心代码。

简单抽出MyBatis代理链核心代码地址

核心类说明:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • MyBatis实现自定义插件
  • 插件的核心原理
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档