更新時間:2022-06-16 09:48:58 來源:動力節點 瀏覽2033次
MyBatis分庫分表如何實現?動力節點小編來告訴大家。
首先實現org.apache.ibatis.plugin.Interceptor接口,復寫以下三個方法:
實現攔截邏輯的地方,內部要通過invocation.proceed()顯式地推進責任鏈前進,也就是調用下一個攔截器攔截目標方法。
Object intercept(Invocation invocation) throws Throwable;
用當前這個攔截器生成對目標target的代理,實際是通過Plugin.wrap(target,this)來完成的,把目標target和攔截器this傳給了包裝函數。
Object plugin(Object target);
設置額外的參數,參數配置在攔截器的Properties節點里。
void setProperties(Properties properties);
如果想要攔截所有的sql,在實現類上添加 annotation
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare",
args = {Connection.class})})
注:Mybatis支持對Executor、StatementHandler、PameterHandler和ResultSetHandler進行攔截,也就是說會對這4種對象進行代理。
框架如上,具體實現有兩個重要點:
1.表的拆分規則
可以在Mapper對象中加上一個annotation,按以下方式去獲取:
String className = id.substring(0, id.lastIndexOf("."));
Class classObj = Class.forName(className);
//根據配置自動生成分表SQL
TableSeg tableSeg = classObj.getAnnotation(TableSeg.class);
TableSeg對象定義:
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface TableSeg {
/**
* 表名
* @return
*/
public String tableName();
/**
* 分表方式,取模,如%5:表示取5余數,
* 如果不設置,直接根據shardBy值分表
* @return
*/
public String shardType();
/**
* 根據什么字段分表
* 多個字段用數學表達表示,如a+b a-b
* @return
*/
public String shardBy();
}
2.sql解析與替換
可以通過以下方法去獲取BoundSql,這個對象有關于這個sql的內容
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
BoundSql boundSql = statementHandler.getBoundSql();
目前看到一些常規做法都是利用string 的replace方案替換sql中的表名,這顯然一個埋坑的做法。利用詞法分析器才是完美方案,可以使用antlr4,grammar文件可以去github上找到。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習