Mybatis外掛程式開發

2021-10-17 02:28:37 字數 3325 閱讀 1835

這裡主要了解一下mybatis的外掛程式開發。

mybatis外掛程式準確的說應該是***,介面名為interceptor,在配置中叫外掛程式,功能非常強大,可以讓我們無侵入式的對sql的執行進行干涉,從sql語句重寫、引數注入、結果集返回等每個主要環節。

​ executor (update, query, flushstatements, commit, rollback, gettransaction, close, isclosed):主要用於sql重寫;

parameterhandler (getparameterobject, setparameters):用於引數處理;

resultsethandler (handleresultsets, handleoutputparameters):用於結果集的二次處理;

statementhandler (prepare, parameterize, batch, update, query):用於jdbc層的控制;

我們從外掛程式配置、外掛程式編寫、外掛程式執行原理、外掛程式註冊與執行攔截的時機、初始化外掛程式、分頁外掛程式的原 理等六個方面展開闡述。

1.外掛程式配置

​ mybatis的外掛程式配置在configuration的內部,初始化時會讀取這些外掛程式,儲存於configuration的interceptorchain中。

>

>

interceptor

="com.li.intercept.parameterhandlerintercept"

>

name

="parameterhandlerintercept"

value

="100"

/>

plugin

>

plugins

>

configuration

>

2.外掛程式載入

在xmlconfigbuilder類中的parseconfiguration()方法中呼叫pluginelement(root.evalnode(「plugins」));,進行主配置檔案的解析;

private

void

parseconfiguration

(xnode root)

catch

(exception e)

}

最後儲存於configuration中的interceptorchain裡。

//interceptorchain原始碼

public

class

interceptorchain

return target;

}public

void

addinterceptor

(interceptor interceptor)

public list

getinterceptors()

}

上面的for迴圈代表了只要是外掛程式,都會以責任鏈的方式逐一呼叫;

3.如何編寫乙個mybatis外掛程式

自定義外掛程式必須實現org.apache.ibatis.plugin.interceptor介面;

public

inte***ce

interceptor

編寫乙個executorhandlerinterceptor

@intercepts()

})public

class

executorhandlerinterceptor

implements

interceptor

public object plugin

(object target)

public

void

setproperties

(properties properties)

}

含義:

mybatis可以攔截哪些類

configuration的原始碼中可以看出在newparameterhandler()、newresultsethandler()、newstatementhandler()、newexecutor()方法中都呼叫了interceptorchain.pluginall()方法;

則我們可以知道mybatis可以攔截parameterhandler、resultsethandler、statementhandler、executor共4個介面 物件內的方法

public parameterhandler newparameterhandler

boundsql boundsql)

public resultsethandler newresultsethandler

parameterhandler parameterhandler, resulthandler resulthandler, boundsql boundsql)

public statementhandler newstatementhandler

object parameterobject, rowbounds rowbounds, resulthandler resulthandler, boundsql boundsql)

public executor newexecutor

(transaction transaction)

public executor newexecutor

(transaction transaction, executortype executortype)

else

if(executortype.reuse == executortype)

else

// 如果開啟快取(預設是開啟的),則使用快取執行器

if(cacheenabled)

// 外掛程式執行

executor =

(executor) interceptorchain.

pluginall

(executor)

;return executor;

}

interceptorchain.pluginall():在這個方法中對***進行了註冊,就是生產對應的**物件,並未進行呼叫;

***的執行時機:plugin()方法註冊***後,那麼,在執行上述4個介面物件內的具體方法時,就會自動出發***,也就是外掛程式的執行;

mybatis外掛程式開發原理及方法

mybatis在四大物件的建立過程中,都會有外掛程式進行介入。在四大物件建立的時候 1.每個建立出來的物件不是直接返回的,而是 interceptorchain.pluginall parameterhandler 2.獲取到所有的interceptor 外掛程式需要實現的介面 呼叫intercep...

mybatis 外掛程式原理

1 mybatis初始化 每個基於 mybatis 的應用都是以乙個 sqlsessionfactory 的例項為中心的。sqlsessionfactory 的例項可以通過 sqlsessionfactorybuilder 獲得。而 sqlsessionfactorybuilder 則可以從 xml...

mybatis分頁外掛程式

其實吧,這個分頁的封裝是我從mybatis實戰上抄的,然後又重構了下 形成了自己的。現在之所以會記錄一下,主要原因是出現了質變 對foreach的支援,而解決這個問題的過程中,我感覺,應該基本上使用上沒有多少侷限行了。下面說說實際的吧。基本的設計思路,是使用mybatis外掛程式,首先是下面這一串註...