在前面的文章里澈灼,介紹了兩個(gè)插件:根據(jù)注解實(shí)現(xiàn)的sql自動(dòng)生成插件和分頁(yè)插件。這兩個(gè)插件在沒(méi)有開(kāi)啟cache的情況下可以很好的使用,但開(kāi)啟cache后卻出現(xiàn)了一些問(wèn)題,為了解決這些問(wèn)題汽绢,編寫(xiě)了攔截cache的插件,通過(guò)這個(gè)攔截器修正了這些問(wèn)題侧戴。
1 問(wèn)題#
1.1 什么問(wèn)題##
最容易出現(xiàn)的問(wèn)題是開(kāi)啟cache后庶喜,分頁(yè)查詢(xún)時(shí)無(wú)論查詢(xún)哪一頁(yè)都返回第一頁(yè)的數(shù)據(jù)。另外救鲤,使用sql自動(dòng)生成插件生成get方法的sql時(shí),傳入的參數(shù)不起作用秩冈,無(wú)論傳入的參數(shù)是多少本缠,都返回第一個(gè)參數(shù)的查詢(xún)結(jié)果。
1.2 為什么出現(xiàn)這些問(wèn)題##
在之前講解Mybatis的執(zhí)行流程的時(shí)候提到入问,在開(kāi)啟cache的前提下丹锹,Mybatis的executor會(huì)先從緩存里讀取數(shù)據(jù)稀颁,讀取不到才去數(shù)據(jù)庫(kù)查詢(xún)。問(wèn)題就出在這里楣黍,sql自動(dòng)生成插件和分頁(yè)插件執(zhí)行的時(shí)機(jī)是在statementhandler里匾灶,而statementhandler是在executor之后執(zhí)行的,無(wú)論sql自動(dòng)生成插件和分頁(yè)插件都是通過(guò)改寫(xiě)sql來(lái)實(shí)現(xiàn)的租漂,executor在生成讀取cache的key(key由sql以及對(duì)應(yīng)的參數(shù)值構(gòu)成)時(shí)使用都是原始的sql阶女,這樣當(dāng)然就出問(wèn)題了。
1.3 解決問(wèn)題##
找到問(wèn)題的原因后哩治,解決起來(lái)就方便了秃踩。只要通過(guò)攔截器改寫(xiě)executor里生成key的方法,在生成可以時(shí)使用自動(dòng)生成的sql(對(duì)應(yīng)sql自動(dòng)生成插件)或加入分頁(yè)信息(對(duì)應(yīng)分頁(yè)插件)就可以了业筏。
2 攔截器簽名#
@Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class CacheInterceptor implements Interceptor {
...
}
從簽名里可以看出憔杨,要攔截的目標(biāo)類(lèi)型是Executor(注意:type只能配置成接口類(lèi)型),攔截的方法是名稱(chēng)為query的方法蒜胖。
3 intercept實(shí)現(xiàn)#
public Object intercept(Invocation invocation) throws Throwable {
Executor executorProxy = (Executor) invocation.getTarget();
MetaObject metaExecutor = MetaObject.forObject(executorProxy, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY);
// 分離代理對(duì)象鏈
while (metaExecutor.hasGetter("h")) {
Object object = metaExecutor.getValue("h");
metaExecutor = MetaObject.forObject(object, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY);
}
// 分離最后一個(gè)代理對(duì)象的目標(biāo)類(lèi)
while (metaExecutor.hasGetter("target")) {
Object object = metaExecutor.getValue("target");
metaExecutor = MetaObject.forObject(object, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY);
}
Object[] args = invocation.getArgs();
return this.query(metaExecutor, args);
}
public <E> List<E> query(MetaObject metaExecutor, Object[] args) throws SQLException {
MappedStatement ms = (MappedStatement) args[0];
Object parameterObject = args[1];
RowBounds rowBounds = (RowBounds) args[2];
ResultHandler resultHandler = (ResultHandler) args[3];
BoundSql boundSql = ms.getBoundSql(parameterObject);
// 改寫(xiě)key的生成
CacheKey cacheKey = createCacheKey(ms, parameterObject, rowBounds, boundSql);
Executor executor = (Executor) metaExecutor.getOriginalObject();
return executor.query(ms, parameterObject, rowBounds, resultHandler, cacheKey, boundSql);
}
private CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {
Configuration configuration = ms.getConfiguration();
pageSqlId = configuration.getVariables().getProperty("pageSqlId");
if (null == pageSqlId || "".equals(pageSqlId)) {
logger.warn("Property pageSqlId is not setted,use default '.*Page$' ");
pageSqlId = defaultPageSqlId;
}
CacheKey cacheKey = new CacheKey();
cacheKey.update(ms.getId());
cacheKey.update(rowBounds.getOffset());
cacheKey.update(rowBounds.getLimit());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
// 解決自動(dòng)生成SQL消别,SQL語(yǔ)句為空導(dǎo)致key生成錯(cuò)誤的bug
if (null == boundSql.getSql() || "".equals(boundSql.getSql())) {
String id = ms.getId();
id = id.substring(id.lastIndexOf(".") + 1);
String newSql = null;
try {
if ("select".equals(id)) {
newSql = SqlBuilder.buildSelectSql(parameterObject);
}
SqlSource sqlSource = buildSqlSource(configuration, newSql, parameterObject.getClass());
parameterMappings = sqlSource.getBoundSql(parameterObject).getParameterMappings();
cacheKey.update(newSql);
} catch (Exception e) {
logger.error("Update cacheKey error.", e);
}
} else {
cacheKey.update(boundSql.getSql());
}
MetaObject metaObject = MetaObject.forObject(parameterObject, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY);
if (parameterMappings.size() > 0 && parameterObject != null) {
TypeHandlerRegistry typeHandlerRegistry = ms.getConfiguration().getTypeHandlerRegistry();
if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
cacheKey.update(parameterObject);
} else {
for (ParameterMapping parameterMapping : parameterMappings) {
String propertyName = parameterMapping.getProperty();
if (metaObject.hasGetter(propertyName)) {
cacheKey.update(metaObject.getValue(propertyName));
} else if (boundSql.hasAdditionalParameter(propertyName)) {
cacheKey.update(boundSql.getAdditionalParameter(propertyName));
}
}
}
}
// 當(dāng)需要分頁(yè)查詢(xún)時(shí),將page參數(shù)里的當(dāng)前頁(yè)和每頁(yè)數(shù)加到cachekey里
if (ms.getId().matches(pageSqlId) && metaObject.hasGetter("page")) {
PageParameter page = (PageParameter) metaObject.getValue("page");
if (null != page) {
cacheKey.update(page.getCurrentPage());
cacheKey.update(page.getPageSize());
}
}
return cacheKey;
}
4 plugin實(shí)現(xiàn)#
public Object plugin(Object target) {
// 當(dāng)目標(biāo)類(lèi)是CachingExecutor類(lèi)型時(shí)台谢,才包裝目標(biāo)類(lèi)寻狂,否者直接返回目標(biāo)本身,減少目標(biāo)被代理的
// 次數(shù)
if (target instanceof CachingExecutor) {
return Plugin.wrap(target, this);
} else {
return target;
}
}