黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 Mybatis插件原理

Mybatis插件原理

更新時間:2021-07-16 15:56:40 來源:動力節點 瀏覽1120次

當我們使用Mybatis的時候,總會使用到各種插件,如PageHelper(分頁插件)等,當我們需要自定義插件來改變,就必須了解插件的實現原理。

概述

Mybatis插件又稱攔截器,Mybatis采用責任鏈模式,通過動態代理組織多個插件(攔截器),通過這些插件可以改變Mybatis的默認行為。MyBatis允許你在已映射語句執行過程中的某一點進行攔截調用。默認情況下,MyBatis允許使用插件來攔截的方法調用包括:

  • Executor(update,query,flushStatements,commit,rollback,getTransaction,close,isClosed)攔截執行器的方法;
  • ParameterHandler(getParameterObject,setParameters)攔截參數的處理;
  • ResultSetHandler(handleResultSets,handleOutputParameters)攔截結果集的處理;
  • StatementHandler(prepare,parameterize,batch,update,query)攔截Sql語法構建的處理;

Mybatis四大接口

mybatis插件

上圖Mybatis框架的整個執行過程。Mybatis插件能夠對則四大對象進行攔截,可以包含到了Mybatis一次會議的所有操作??梢奙ybatis的的插件很強大。

  • Executor是Mybatis的內部執行器,它負責調用StatementHandler操作數據庫,并把結果集通過ResultSetHandler進行自動映射,另外,他還處理了二級緩存的操作。從這里可以看出,我們也是可以通過插件來實現自定義的二級緩存的。
  • StatementHandler是Mybatis直接和數據庫執行sql腳本的對象。另外它也實現了Mybatis的一級緩存。這里,我們可以使用插件來實現對一級緩存的操作(禁用等等)。
  • ParameterHandler是Mybatis實現Sql入參設置的對象。插件可以改變我們Sql的參數默認設置。
  • ResultSetHandler是Mybatis把ResultSet集合映射成POJO的接口對象。我們可以定義插件對Mybatis的結果集自動映射進行修改。

攔截器為什么能夠攔截(四大接口) 

public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
    ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
    parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
    return parameterHandler;
}

public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler, ResultHandler resultHandler, BoundSql boundSql) {
    ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
    resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
    return resultSetHandler;
}

public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
    StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
    statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
    return statementHandler;
}

public Executor newExecutor(Transaction transaction, ExecutorType executorType, boolean autoCommit) {
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor 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 (cacheEnabled) {
        executor = new CachingExecutor(executor, autoCommit);
    }
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
}

這4個方法實例化了對應的對象之后,都會調用interceptorChain的pluginAll方法,那么下面我們在來看pluginAll干了什么

public class InterceptorChain {

    private final List<Interceptor> interceptors = new ArrayList<Interceptor>();

    public Object pluginAll(Object target) {
        for (Interceptor interceptor : interceptors) {
            target = interceptor.plugin(target);
        }
        return target;
    }

    public void addInterceptor(Interceptor interceptor) {
        interceptors.add(interceptor);
    }

    public List<Interceptor> getInterceptors() {
        return Collections.unmodifiableList(interceptors);
    }
}

原來這個pluginAll方法就是遍歷所有的攔截器,然后順序執行我們插件的plugin方法,一層一層返回我們原對象(Executor/ParameterHandler/ResultSetHander/StatementHandler)的代理對象。當我們調用四大接口對象的方法時候,實際上是調用代理對象的響應方法,代理對象又會調用四大接口對象的實例。

攔截器Interceptor

Mybatis的插件實現要實現Interceptor接口,我們看下這個接口定義的方法。

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

這個接口只聲明了三個方法。

  • setProperties方法是在Mybatis進行配置插件的時候可以配置自定義相關屬性,即:接口實現對象的參數配置
  • plugin方法是插件用于封裝目標對象的,通過該方法我們可以返回目標對象本身,也可以返回一個它的代理,可以決定是否要進行攔截進而決定要返回一個什么樣的目標對象,官方提供了示例:return Plugin.wrap(target,this);
  • intercept方法就是要進行攔截的時候要執行的方法

官方推薦插件開發方式

@Intercepts({@Signature(type = Executor.class, method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class TestInterceptor implements Interceptor {
    public Object intercept(Invocation invocation) throws Throwable {
        Object target = invocation.getTarget(); //被代理對象
        Method method = invocation.getMethod(); //代理方法
        Object[] args = invocation.getArgs(); //方法參數
        // do something ...... 方法攔截前執行代碼塊
        Object result = invocation.proceed();
        // do something .......方法攔截后執行代碼塊
        return result;
    }
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }
}

簡單編寫Mybatis插件

注:MyBatis默認沒有一個攔截器接口的實現類,開發者可以實現符合自己需求的攔截器。

下面的MyBatis官網的一個攔截器實例:

@Intercepts({@Signature(type= Executor.class, method = "update", args = {MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {
    public Object intercept(Invocation invocation) throws Throwable {
        return invocation.proceed();
    }
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }
    public void setProperties(Properties properties) {
    }
}

全局xml配置:

<plugins>
<plugin interceptor="org.format.mybatis.cache.interceptor.ExamplePlugin"></plugin>
</plugins>

這個攔截器攔截Executor接口的update方法(其實也就是SqlSession的新增,刪除,修改操作),所有執行executor的update方法都會被該攔截器攔截到。

以上就是動力節點小編介紹的"Mybatis插件原理",希望對大家有幫助,想了解更多可查看Mybatis視頻教程。動力節點在線學習教程,針對沒有任何Java基礎的讀者學習,讓你從入門到精通,主要介紹了一些Java基礎的核心知識,讓同學們更好更方便的學習和了解Java編程,感興趣的同學可以關注一下。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 狠狠操天天操夜夜操 | 2021天天躁狠狠燥 | 国产精品免费一区二区三区四区 | 清风阁爱干 | 午夜国产在线视频 | 国产成人精品一区二区不卡 | 亚洲日产综合欧美一区二区 | 妞干网免费在线观看 | 韩国午夜影院 | 免费观看一级特黄欧美大片 | 亚洲激情综合 | 精品国产一区二区三区不卡 | 一本久道在线 | 插一插射一射视频 | 极品色天使在线婷婷天堂亚洲 | 人人干视频在线观看 | 五月天丁香婷婷综合久久 | 2020国产精品视频 | 茄子视频黄色 | 日韩欧美视频二区 | 看中国一级毛片 | 欧美色xx | 国产三级a三级三级野外 | 中文字幕亚洲综合久久202 | 欧美精 | 猫色综合网 | 日日做日日摸夜夜爽 | 日韩欧美亚洲综合一区二区 | 一级毛片免费观看 | 欧美日本一区二区 | 中文字幕亚洲综合久久菠萝蜜 | 久久亚洲国产伦理 | 最近免费中文字幕mv | 国产一区二区自拍视频 | 亚洲国产成人久久一区二区三区 | 不卡国产 | 黄网站色成年片在线观看 | 狠狠色噜噜狠狠狠狠色综合网 | 久久国产综合 | 在线色影院 | 天天色综合图片 |