bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

MyBatis中如何使用plugins插件

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)MyBatis中如何使用 plugins插件,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

10多年的朝陽網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整朝陽建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“朝陽網(wǎng)站設(shè)計(jì)”,“朝陽網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

插件是MyBatis對外開放了四個(gè)接口,可以用于自定義擴(kuò)展。

接口
可代理方法說明
Executor執(zhí)行器,對事務(wù)、緩存等提供統(tǒng)一接口update執(zhí)行update、insert、delete操作


query執(zhí)行select操作


flushStatements在commit的時(shí)候自動調(diào)用,SimpleExecutor、ReuseExecutor、BatchExecutor處理不同


commit提交事務(wù)


rollback事務(wù)回滾


getTransaction獲取事務(wù)


close結(jié)束或關(guān)閉事務(wù)


isClosed判斷事務(wù)是否關(guān)閉
ParameterHandler參數(shù)處理器,負(fù)責(zé)為 PreparedStatement 的 sql 語句參數(shù)動態(tài)賦值getParameterObject獲取參數(shù)


setParameters設(shè)置參數(shù)
ResultSetHandler結(jié)果集處理handleResultSets處理結(jié)果集


handleOutputParameters處理存儲過程出參
StatementHandler四大組件中最重要的一個(gè)對象,負(fù)責(zé)操作 Statement 對象與數(shù)據(jù)庫進(jìn)行交流,在工作時(shí)還會使用 ParameterHandler 和 ResultSetHandler 對參數(shù)進(jìn)行映射,對結(jié)果進(jìn)行實(shí)體類的綁定prepare(BaseSatementHandler)SQL預(yù)編譯


parameterize設(shè)置參數(shù)


batch批量處理


update增刪改操作


query查詢操作

以上4個(gè)接口在MyBatis中的工作流程如下圖:

MyBatis中如何使用 plugins插件

MyBatis實(shí)現(xiàn)自定義插件

創(chuàng)建自定義插件主要步驟:

  1. 編寫插件代碼實(shí)現(xiàn)Interceptor接口,設(shè)置要代理的方法

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

下邊來簡單做一個(gè)分表的插件,根據(jù)主鍵ID分,實(shí)現(xiàn)單數(shù)入<表名>表雙數(shù)入<表名_1>表

/**
 * 簡單分表,根據(jù)傳入的主鍵ID,實(shí)現(xiàn)單數(shù)入<表名>表雙數(shù)入<表名_1>表
 * @Author: maomao
 * @Date: 2021-04-09 17:28
 */
@Intercepts({
        @Signature(type = Executor.class, //表示要代理的接口類型
                method = "update",  //表示要代理接口的對應(yīng)方法
                args = {MappedStatement.class, Object.class} //表示代理方法對應(yīng)的參數(shù)列表,反射時(shí)使用(解決方法重載問題)
        ),
        @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();
        }
        //獲得執(zhí)行的sql語句
        BoundSql boundSql = mappedStatement.getBoundSql(invocation.getArgs()[1]);
        //sql語法解析工具,方便獲取表名
        Statement statement = CCJSqlParserUtil.parse(boundSql.getSql());
        //獲取參數(shù)
        Object parameter = boundSql.getParameterObject();
        JSON parameterJson = JSONUtil.parse(parameter);

        Long id = (Long) parameterJson.getByPath("id");
        //使用ID取余數(shù),確定執(zhí)行表名
        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>

上邊就可以簡單的實(shí)現(xiàn)一個(gè)分表的邏輯,不需要修改任何業(yè)務(wù)代碼。是不是非常方便。

插件的核心原理

那MyBatis是如何實(shí)現(xiàn)插件功能的呢?如果有多個(gè)插件它又是如何執(zhí)行的呢?

插件的實(shí)現(xiàn)使用了動態(tài)代理、反射和責(zé)任鏈的方式實(shí)現(xiàn)。

下邊我將抽出MyBatis的插件核心代碼。

簡單抽出MyBatis代理鏈核心代碼地址

核心類說明: MyBatis中如何使用 plugins插件

上述就是小編為大家分享的MyBatis中如何使用 plugins插件了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享標(biāo)題:MyBatis中如何使用plugins插件
文章起源:http://vcdvsql.cn/article2/peioic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)做網(wǎng)站外貿(mào)網(wǎng)站建設(shè)標(biāo)簽優(yōu)化網(wǎng)站制作服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)