#0 系列目錄#sql
在前面的文章裏,介紹了兩個插件:根據註解實現的sql自動生成插件和分頁插件。這兩個插件在沒有開啓cache的狀況下能夠很好的使用,但開啓cache後卻出現了一些問題,爲了解決這些問題,編寫了攔截cache的插件,經過這個攔截器修正了這些問題。 #1 問題# ##1.1 什麼問題## 最容易出現的問題是開啓cache後,分頁查詢時不管查詢哪一頁都返回第一頁的數據。另外,使用sql自動生成插件生成get方法的sql時,傳入的參數不起做用,不管傳入的參數是多少,都返回第一個參數的查詢結果。數據庫
##1.2 爲何出現這些問題## 在以前講解Mybatis的執行流程的時候提到,在開啓cache的前提下,Mybatis的executor會先從緩存裏讀取數據,讀取不到纔去數據庫查詢。問題就出在這裏,sql自動生成插件和分頁插件執行的時機是在statementhandler裏,而statementhandler是在executor以後執行的,不管sql自動生成插件和分頁插件都是經過改寫sql來實現的,executor在生成讀取cache的key(key由sql以及對應的參數值構成)時使用都是原始的sql
,這樣固然就出問題了。緩存
##1.3 解決問題## 找到問題的緣由後,解決起來就方便了。只要經過攔截器改寫executor裏生成key的方法,在生成能夠時使用自動生成的sql(對應sql自動生成插件)或加入分頁信息(對應分頁插件)就能夠了。app
#2 攔截器簽名#源碼分析
@Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})}) public class CacheInterceptor implements Interceptor { ... }
從簽名裏能夠看出,要攔截的目標類型是Executor(注意:type只能配置成接口類型
),攔截的方法是名稱爲query的方法。ui
#3 intercept實現#this
public Object intercept(Invocation invocation) throws Throwable { Executor executorProxy = (Executor) invocation.getTarget(); MetaObject metaExecutor = MetaObject.forObject(executorProxy, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY); // 分離代理對象鏈 while (metaExecutor.hasGetter("h")) { Object object = metaExecutor.getValue("h"); metaExecutor = MetaObject.forObject(object, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY); } // 分離最後一個代理對象的目標類 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); // 改寫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(); // 解決自動生成SQL,SQL語句爲空致使key生成錯誤的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)); } } } } // 當須要分頁查詢時,將page參數裏的當前頁和每頁數加到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實現#.net
public Object plugin(Object target) { // 當目標類是CachingExecutor類型時,才包裝目標類,否者直接返回目標自己,減小目標被代理的 // 次數 if (target instanceof CachingExecutor) { return Plugin.wrap(target, this); } else { return target; } }