MyBatis plugins插件(4)

插件是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>表

/**
 * 簡單分表,根據傳入的主鍵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代理鏈核心代碼地址

核心類說明:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章