原因: 在工作中因數(shù)據(jù)量的問題需要分表,在添加分表攔截器時發(fā)現(xiàn)使用springboot自動加載pageHelper插件時無法將分頁插件放置到分表插件后執(zhí)行,導(dǎo)致分表插件處理參數(shù)的邏輯過于復(fù)雜。
-
mybatis 攔截器執(zhí)行順序
15232868-72c3acc43e6e85b9.png
2.pageHelper和分表插件的先后順序
2.1 pageHelper插件實現(xiàn)方式是Executor接口,最先執(zhí)行的接口,若要分表插件在分頁插件之前執(zhí)行也只能使用Executor接口。
2.2 分頁插件自動加載代碼存在于第三方j(luò)ar包中,而分表插件為自己實現(xiàn),邏輯存在于項目源碼中,若使用springboot 的自動掃描加載首先加載的是自定義的分表插件,導(dǎo)致分表插件最后執(zhí)行。
2.3 實現(xiàn)方式
項目中的實現(xiàn)方式為實現(xiàn)ApplicationListener接口監(jiān)聽程序啟動事件,再程序啟動后加載分表插件,確保分表插件在分頁插件后加載。
/**
* mybatis攔截器配置,確保分表攔截器在分頁攔截器前執(zhí)行(分頁攔截器不會將任務(wù)責任鏈向后傳遞),最后加載分表攔截器
* mybatis攔截器配置的先后順序類似棧的數(shù)據(jù)結(jié)構(gòu),先入后出
*/
@Configuration
@AutoConfigureAfter({PageHelperAutoConfiguration.class})
public class SubTableAutoConfiguration implements ApplicationListener<ContextRefreshedEvent> {
@Resource
private List<SqlSessionFactory> sqlSessionFactoryList;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
//分表插件對象
MybatisSubTable mybatisSubTable = new MybatisSubTable();
//遍歷添加攔截器
sqlSessionFactoryList.forEach(e -> e.getConfiguration().addInterceptor(mybatisSubTable));
}
}
3.分表插件實現(xiàn)方式
3.1 實現(xiàn)邏輯
分表插件使用注解的方式實現(xiàn),掃描分表注解,記錄分表的表明和分表的屬性
/**
* 啟用分表注解,當sql為查詢時,若查詢多個表單,設(shè)置查詢參數(shù)的分表字段為 英文(,)逗號分隔的字符串
* 如:
* 以工單表為例 當查詢t_domain_task_26000422021031816303800000,t_domain_task_23000422016110910302325284兩張表時
* 設(shè)置domainCode=26000422021031816303800000,23000422016110910302325284
* 注:
* 在查詢sql中不能出現(xiàn) where domainCode=?樣式
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SubTableAnnotation {
/**
* 分表的屬性,分表策略,以何屬性分表
*/
String value();
/**
* 分表的表名,為不包含分表字段屬性的原始表名 <br>
* eg:t_domain_task_2019 原生表名為t_domain_task
*/
String baseTableName();
}
3.2 攔截器實現(xiàn)
因要做分表操作,所以需要攔截query 和 update方法
@Intercepts(
{
@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}),
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
}
)
初始化時記錄需要分表操作的表名及屬性,攔截到sql后獲取表名和參數(shù)修改表名,具體實現(xiàn)如下
/**
* 分表查詢攔截器 在分頁攔截器后面運行 包含crud 不存在分表屬性時會拋異常
*/
@Intercepts(
{
@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}),
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
}
)
@Slf4j
public class MybatisSubTable implements Interceptor {
private Map<String, String> init() {
//獲取標記了@SubTableAnnotation的Bean
Map<String, Object> beansWithAnnotation = SpringApplicationContextUtil.getApplicationContext().getBeansWithAnnotation(SubTableAnnotation.class);
Map<String, String> stringStringMap = new HashMap<>();
beansWithAnnotation.forEach((n, v) -> {
//spring管理的bean只能使用AnnotationUtils獲取注解
SubTableAnnotation annotation = AnnotationUtils.findAnnotation(v.getClass(), SubTableAnnotation.class);
Assert.notNull(annotation, "@SubTableAnnotation 注解獲取異常");
String value = annotation.value();
String tableName = annotation.baseTableName();
stringStringMap.put(tableName, value);
});
return stringStringMap;
}
/**
* 需要分表查詢的類
* key 表明
* value 分表標識字段
*/
private Map<String, String> subTable = null;
@Override
public Object intercept(Invocation invocation) throws Throwable {
if (Objects.isNull(subTable)) {
subTable = init();
}
//不存在分表屬性 直接返回
if (subTable.isEmpty()) {
return invocation.proceed();
}
//獲取參數(shù)組PreparedStatement
Object[] args = invocation.getArgs();
MappedStatement mappedStatement = (MappedStatement) args[0];
Object parameter = args[1];
BoundSql boundSql = mappedStatement.getBoundSql(parameter);
String sql = boundSql.getSql().toLowerCase().replaceAll("\\s+", " ").trim();
// 1 獲取表名
String tableName = getTableNameBySql(sql);
// sql中不存在表名,不支持分表查詢的表,不存在的表
if (tableName == null) {
return invocation.proceed();
}
//分表屬性名查詢校驗,失敗時拋出異常
checkKeyFromWhere(tableName, sql);
Object subValueObj = getValue(parameter, tableName);
//分表參數(shù)的屬性值為空時,放棄處理,直接返回
// 20191025 改為為空時,拋異常
Assert.notNull(subValueObj, "分表參數(shù)為空,將會拋出異常 sql:[" + sql + "]");
String[] subValue = subValueObj.toString().split(",");
//參數(shù)構(gòu)建
List<ParameterMapping> newParameterMappings = Arrays.stream(subValue).flatMap(e -> boundSql.getParameterMappings().stream()).collect(Collectors.toList());
//新sql
String newSql = updateSql(sql, subValue, tableName);
//構(gòu)建新的BoundSql對象
BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), newSql, newParameterMappings, boundSql.getParameterObject());
//創(chuàng)建新的MappedStatement 對象
MappedStatement newMappedStatement = createNewMappedStatement(mappedStatement, newBoundSql);
args[0] = newMappedStatement;
return invocation.proceed();
}
private MappedStatement createNewMappedStatement(MappedStatement mappedStatement, BoundSql newBoundSql) {
MappedStatement.Builder builder = new MappedStatement.Builder(mappedStatement.getConfiguration(), mappedStatement.getId(), parameterObject -> newBoundSql, mappedStatement.getSqlCommandType());
builder.resource(mappedStatement.getResource());
builder.fetchSize(mappedStatement.getFetchSize());
builder.statementType(mappedStatement.getStatementType());
builder.keyGenerator(mappedStatement.getKeyGenerator());
if (mappedStatement.getKeyProperties() != null && mappedStatement.getKeyProperties().length > 0) {
builder.keyProperty(mappedStatement.getKeyProperties()[0]);
}
builder.timeout(mappedStatement.getTimeout());
builder.parameterMap(mappedStatement.getParameterMap());
builder.resultMaps(mappedStatement.getResultMaps());
builder.resultSetType(mappedStatement.getResultSetType());
builder.cache(mappedStatement.getCache());
builder.flushCacheRequired(mappedStatement.isFlushCacheRequired());
builder.useCache(mappedStatement.isUseCache());
return builder.build();
}
/**
* 當類型為Executor時才返回代理,減少不必要的代理
*/
@Override
public Object plugin(Object target) {
if (target instanceof Executor) {
return Plugin.wrap(target, this);
} else {
return target;
}
}
/**
* 構(gòu)建新的sql
*
* @param sql 源sql
* @param subValue 分表屬性值
* @return 新sql
*/
private String updateSql(String sql, String[] subValue, String tableName) {
return Stream.of(subValue).map(value -> {
//不為空時,替換表名
String replaceTableName = tableName + "_" + value;
//加上前后空格,防止短表名和長表名已相同的字符串開始 如 t_domain 和 t_domain_task
return sql.replace(tableName, " " + replaceTableName + " ");
}).collect(Collectors.joining(" union all "));
}
/**
* sql中存在庫中表單且sql中使用的表單啟用了分表 返回分表名,反之返回null
*/
private String getTableNameBySql(String sql) {
if (sql.contains("last_insert_id()")) {
return null;
}
if (sql.toLowerCase().startsWith("select")
|| sql.toLowerCase().startsWith("update")
|| sql.toLowerCase().startsWith("delete")
|| sql.toLowerCase().startsWith("insert")) {
//修改為使用druid的解析器,提升健壯性
log.trace(sql);
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement sqlStatement = parser.parseStatement();
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
sqlStatement.accept(visitor);
String tableName = visitor.getTables().keySet().stream().findFirst().orElse(new TableStat.Name(null)).getName();
if (subTable.containsKey(tableName)) {
return tableName;
}
}
return null;
}
/**
* 獲取分表屬性的值
*
* @param param 參數(shù)對象
* @param tableName 表名
*/
private String getValue(Object param, String tableName) {
//dao接口獲取的參數(shù)對象
//map類型參數(shù) 包含多個參數(shù)時以map形式傳遞
if (param instanceof HashMap) {
//真實類型為 MapperMethod的ParamMap get方法獲取不存在的key值時會拋異常
HashMap<?, ?> param2 = (HashMap<?, ?>) param;
//多參數(shù),且沒有名為 分表字段名 的參數(shù),遍歷所有參數(shù)獲取分表字段
if (!param2.containsKey(subTable.get(tableName))) {
HashMap<?, ?> param1 = (HashMap<?, ?>) param;
for (Object key : param1.keySet()) {
Object value1 = param1.get(key);
//非基本類型及其包裝類型或字符串類型時,開始套娃
if (!(ClassUtils.isPrimitiveOrWrapper(value1.getClass()) || value1 instanceof String)) {
return getValue(value1,tableName);
}
}
}else {
Object value = param2.get(subTable.get(tableName));
return value == null ? null : value.toString();
}
//String類型參數(shù), 單參數(shù)
} else if (param instanceof String) {
return param.toString();
} else {
//對象參數(shù)
try {
Method method = param.getClass().getMethod("get" + subTable.get(tableName).substring(0, 1).toUpperCase() + subTable.get(tableName).substring(1));
Object value = method.invoke(param);
return value == null ? null : value.toString();
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
log.error("獲取分表參數(shù)失敗,請校驗參數(shù)", e);
return null;
}
}
return null;
}
/**
* 校驗sql中是否存在分表參數(shù)的查詢
*
* @param tableName 表名
* @param sql 查詢語句
*/
private void checkKeyFromWhere(String tableName, String sql) {
String subKey = subTable.get(tableName);
String[] split = sql.split(" where");
if (split.length > 1 && split[1].contains(" " + subKey.toLowerCase())) {
log.error("分表處理失?。悍直聿樵儠r where 語句參數(shù)不能包含分表字段參數(shù) {}【{}】", tableName, subKey);
throw new BusinessException("分表處理失?。悍直聿樵儠r where 語句參數(shù)不能包含分表字段參數(shù) {}【{}】", tableName, subKey);
}
}
}
``
