溫馨提示×

溫馨提示×

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

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

Mybatis自定義插件

發(fā)布時間:2020-06-10 23:17:05 來源:網(wǎng)絡(luò) 閱讀:538 作者:石小令 欄目:編程語言

相信工作中用mybatis的同學(xué)大部分都使用過PageHelper分布插件,最近也是想了解一下PageHelper的實現(xiàn)原理,PageHelper也是通過mybatis的插件來實現(xiàn)的。具體怎么去實現(xiàn)一個mybatis插件下面做具體的介紹。

1.mybatis插件機制

工作中遇到過一個場景,打印mybatis的執(zhí)行sql日志到公司日志平臺。那么就需要自定義mybatis插件來實現(xiàn),在執(zhí)行sql之前,希望能夠攔截到mybatis的執(zhí)行sql,然后使用公司的日志框架打印日志。
myba支持攔截的方法:

  • ParameterHandler:處理SQL的參數(shù)對象;
  • ResultSetHandler:處理SQL的返回結(jié)果集;
  • StatementHandler:執(zhí)行SQL語句;
  • Executor:執(zhí)行器,執(zhí)行增刪改查操作;

在自定義mybatis插件的時候,需要指定自己所需要的攔截方式,例如我上面工作中是需要使用StatementHandler。

2.mybatis插件使用到的設(shè)計模式

mybatis插件中主要使用到了兩種設(shè)計模式:動態(tài)代理和責(zé)任鏈模式;

1)責(zé)任鏈模式

責(zé)任鏈模式
在說mybatis中的責(zé)任鏈之前,我們先回想一下責(zé)任鏈模式:
Mybatis自定義插件
責(zé)任鏈模式中涉及兩個角色:

  • 抽象處理者(Handler)角色:定義處理請求的接口或者抽象類,提供處理請求的方法和設(shè)置下一個處理者的方法。
  • 具體處理者角色(ConcretaHandler)角色:處理具體的請求或者將請求發(fā)送到下一個具體處理者
    具體處理者擁有下一下處理者的引用,如果需要下一個處理者處理,那么調(diào)用下一個處理者的處理方法即可。
    mybatis插件中責(zé)任鏈模式的應(yīng)用
    mybatis中的插件實際上也可以叫做攔截器,mybatis中使用責(zé)任鏈模式將臃腫的功能拆分成單一的Handler處理類中,開發(fā)人員可以根據(jù)業(yè)務(wù)需求將多個Handler對象組合成一條責(zé)任鏈,實現(xiàn)請求的處理。mybatis也是通過這種模式來提供mybatis的擴展性。

    例如:現(xiàn)在有 HandlerA、HandlerB、HandlerC三個字段的業(yè)務(wù)邏輯
    當業(yè)務(wù)只需要HandlerA和HandlerC時,只需要動態(tài)組合得到HandlerA->HandlerC就可以了。

2)動態(tài)代理

mybatis中的Executor、ParameterHandler、ResultSetHandler、StatementHandler它們都是通過Configuration.new()方法來創(chuàng)建的,而Configuration.new()方法實際上調(diào)用的是InterceptorChain.pluginAll()方法來生成代理對象,所以通過Configuration.new*()系列方法得到的對象實際是一個代理對象。
以Configuration.newExecutor()方法為例介紹:
1.Configuration.newExecutor()

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
            executorType = executorType == null ? this.defaultExecutorType : executorType;
            executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
            Object executor;
            if (ExecutorType.BATCH == executorType) {
                    executor = new BatchExecutor(this, transaction);
            } else if (ExecutorType.REUSE == executorType) {
                    executor = new ReuseExecutor(this, transaction);
            } else {
                    executor = new SimpleExecutor(this, transaction);
            }
            if (this.cacheEnabled) {
                    executor = new CachingExecutor((Executor)executor);
            }
            Executor executor = (Executor)this.interceptorChain.pluginAll(executor);
            return executor;
    }

2.interceptorChain.pluginAll(executor)

public Object pluginAll(Object target) {
            Interceptor interceptor;
            for(Iterator var2 = this.interceptors.iterator(); var2.hasNext(); target = interceptor.plugin(target)) {
                    interceptor = (Interceptor)var2.next();
            }
            return target;
    }

3.target = interceptor.plugin(target)

interceptor.plugin()方法實際上是我們自定義插件的plugin方法。
一般我們這個方法的實現(xiàn)是通過Plugin.wrap來生成代理

public Object plugin(Object target) {
        return Plugin.wrap(target, this);
}

4. Plugin.wrap

public static Object wrap(Object target, Interceptor interceptor) {
        Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
        Class<?> type = target.getClass();
        Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
        return interfaces.length > 0 ? Proxy.newProxyInstance(type.getClassLoader(), interfaces, new Plugin(target, interceptor, signatureMap)) : target;
    }

3.實現(xiàn)自定義Mybatis插件

mybatis中使用的攔截器都需要實現(xiàn)Interceptor接口

public interface Interceptor {
    Object intercept(Invocation var1) throws Throwable;
    Object plugin(Object var1);
    void setProperties(Properties var1);
}

用戶自定義的攔截器除了繼承Interceptor接口,還需要使用@Intercepts和@Signature兩個注解標識。

@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
    }),
    @Signature(type = Executor.class, method = "query", args = {
            MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class,
            CacheKey.class, BoundSql.class
    })})

例如上面的定義:

  • type:屬性指定需要攔截的類型
  • method:屬性指定需要攔截的方法
  • args:屬性指定了被攔截方法的參數(shù)列表

將我們自定義的mybatis的插件配置到mybatis-config.xml中

<plugins>
            <plugin interceptor="com.xxx.XXXInterceptor">
                    <!-- 對攔截器中的屬性進行初始化 -->
                    <property name="name" value="Bob"/>
            </plugin>
    </plugins>

4.案例

例如,我們比較常用的對sql進行監(jiān)控,監(jiān)控sql的執(zhí)行時長及慢查詢等,那么我們就可以通過編寫MonitorInterceptor攔截器

@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
        }),
        @Signature(type = Executor.class, method = "query", args = {
                MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class,
                CacheKey.class, BoundSql.class
        })})
@Slf4j
public class MonitorInercepor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        try {
            Object[] args = invocation.getArgs();
            MappedStatement ms = (MappedStatement) args[0];
            Object parameter = args[1];
            long start = System.currentTimeMillis();
            Object result = invocation.proceed();  // 獲取代理
            long end = System.currentTimeMillis();
            long cost = end - start;
            log.debug("[TimerInterceptor] execute [{}] cost [{}] ms, parameter:{}", ms.getId(), cost, parameter);
            if (cost > 1000) {
                log.warn("Sql語句執(zhí)行時間超過1秒鐘,請檢查優(yōu)化,方法:{},耗時:{}ms,參數(shù):{}", ms.getId(), cost, parameter);
            }
            return result;
        } catch (Throwable r) {
            log.error(r.getMessage(), r);
        }
        return invocation.proceed();
    }
    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }
    @Override
    public void setProperties(Properties properties) { }
}
向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI