溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MyBatis中如何使用 plugins插件

發布時間:2021-08-05 16:16:58 來源:億速云 閱讀:263 作者: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

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女