這期內(nèi)容當中小編將會給大家?guī)碛嘘PMyBatis中如何使用 plugins插件,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
10多年的朝陽網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整朝陽建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“朝陽網(wǎng)站設計”,“朝陽網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
插件是MyBatis對外開放了四個接口,可以用于自定義擴展。
| 接口 | 可代理方法 | 說明 | |
|---|---|---|---|
| Executor | 執(zhí)行器,對事務、緩存等提供統(tǒng)一接口 | update | 執(zhí)行update、insert、delete操作 |
| query | 執(zhí)行select操作 | ||
| flushStatements | 在commit的時候自動調(diào)用,SimpleExecutor、ReuseExecutor、BatchExecutor處理不同 | ||
| commit | 提交事務 | ||
| rollback | 事務回滾 | ||
| getTransaction | 獲取事務 | ||
| close | 結束或關閉事務 | ||
| isClosed | 判斷事務是否關閉 | ||
| ParameterHandler | 參數(shù)處理器,負責為 PreparedStatement 的 sql 語句參數(shù)動態(tài)賦值 | getParameterObject | 獲取參數(shù) |
| setParameters | 設置參數(shù) | ||
| ResultSetHandler | 結果集處理 | handleResultSets | 處理結果集 |
| handleOutputParameters | 處理存儲過程出參 | ||
| StatementHandler | 四大組件中最重要的一個對象,負責操作 Statement 對象與數(shù)據(jù)庫進行交流,在工作時還會使用 ParameterHandler 和 ResultSetHandler 對參數(shù)進行映射,對結果進行實體類的綁定 | prepare | (BaseSatementHandler)SQL預編譯 |
| parameterize | 設置參數(shù) | ||
| batch | 批量處理 | ||
| update | 增刪改操作 | ||
| query | 查詢操作 |
以上4個接口在MyBatis中的工作流程如下圖:

創(chuàng)建自定義插件主要步驟:
編寫插件代碼實現(xiàn)Interceptor接口,設置要代理的方法
在mybatis-config.xml中注冊插件
下邊來簡單做一個分表的插件,根據(jù)主鍵ID分,實現(xiàn)單數(shù)入<表名>表雙數(shù)入<表名_1>表
/**
* 簡單分表,根據(jù)傳入的主鍵ID,實現(xiàn)單數(shù)入<表名>表雙數(shù)入<表名_1>表
* @Author: maomao
* @Date: 2021-04-09 17:28
*/
@Intercepts({
@Signature(type = Executor.class, //表示要代理的接口類型
method = "update", //表示要代理接口的對應方法
args = {MappedStatement.class, Object.class} //表示代理方法對應的參數(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>
上邊就可以簡單的實現(xiàn)一個分表的邏輯,不需要修改任何業(yè)務代碼。是不是非常方便。
那MyBatis是如何實現(xiàn)插件功能的呢?如果有多個插件它又是如何執(zhí)行的呢?
插件的實現(xiàn)使用了動態(tài)代理、反射和責任鏈的方式實現(xiàn)。
下邊我將抽出MyBatis的插件核心代碼。
簡單抽出MyBatis代理鏈核心代碼地址
核心類說明: 
上述就是小編為大家分享的MyBatis中如何使用 plugins插件了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標題:MyBatis中如何使用plugins插件
文章起源:http://www.chinadenli.net/article2/peioic.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設、做網(wǎng)站、外貿(mào)網(wǎng)站建設、標簽優(yōu)化、網(wǎng)站制作、服務器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)