温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MyBatis中如何使用 plugins插件

发布时间:2021-08-05 16:16:58 来源:亿速云 阅读:194 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关MyBatis中如何使用 plugins插件,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

插件是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中如何使用 plugins插件

MyBatis实现自定义插件

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

  1. 编写插件代码实现Interceptor接口,设置要代理的方法

  2. 在mybatis-config.xml中注册插件

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

/**
 * 简单分表,根据传入的主键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中注册插件

<plugins>
    <plugin interceptor="com.freecloud.plug.mybatis.plugins.SimpleTableInterceptor"></plugin>
</plugins>

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

插件的核心原理

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

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

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

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

核心类说明: MyBatis中如何使用 plugins插件

上述就是小编为大家分享的MyBatis中如何使用 plugins插件了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI