【深入淺出MyBatis系列九】改造Cache插件

陶邦仁 發(fā)布于 2015/12/25 15:28

原文鏈接

系列目錄

在前面的文章里,介紹了兩個(gè)插件:根據(jù)注解實(shí)現(xiàn)的sql自動(dòng)生成插件和分頁插件。這兩個(gè)插件在沒有開啟cache的情況下可以很好的使用,但開啟cache后卻出現(xiàn)了一些問題,為了解決這些問題,編寫了攔截cache的插件,通過這個(gè)攔截器修正了這些問題。

1 問題

1.1 什么問題

最容易出現(xiàn)的問題是開啟cache后,分頁查詢時(shí)無論查詢哪一頁都返回第一頁的數(shù)據(jù)。另外,使用sql自動(dòng)生成插件生成get方法的sql時(shí),傳入的參數(shù)不起作用,無論傳入的參數(shù)是多少,都返回第一個(gè)參數(shù)的查詢結(jié)果。

1.2 為什么出現(xiàn)這些問題

在之前講解Mybatis的執(zhí)行流程的時(shí)候提到,在開啟cache的前提下,Mybatis的executor會(huì)先從緩存里讀取數(shù)據(jù),讀取不到才去數(shù)據(jù)庫查詢。問題就出在這里,sql自動(dòng)生成插件和分頁插件執(zhí)行的時(shí)機(jī)是在statementhandler里,而statementhandler是在executor之后執(zhí)行的,無論sql自動(dòng)生成插件和分頁插件都是通過改寫sql來實(shí)現(xiàn)的,executor在生成讀取cache的key(key由sql以及對(duì)應(yīng)的參數(shù)值構(gòu)成)時(shí)使用都是原始的sql,這樣當(dāng)然就出問題了。

1.3 解決問題

找到問題的原因后,解決起來就方便了。只要通過攔截器改寫executor里生成key的方法,在生成可以時(shí)使用自動(dòng)生成的sql(對(duì)應(yīng)sql自動(dòng)生成插件)或加入分頁信息(對(duì)應(yīng)分頁插件)就可以了。

2 攔截器簽名

@Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})  
public class CacheInterceptor implements Interceptor {  
    ...  
}

從簽名里可以看出,要攔截的目標(biāo)類型是Executor(注意:type只能配置成接口類型),攔截的方法是名稱為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)類  
        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();  
        // 解決自動(dòng)生成SQL,SQL語句為空導(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)需要分頁查詢時(shí),將page參數(shù)里的當(dāng)前頁和每頁數(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)類是CachingExecutor類型時(shí),才包裝目標(biāo)類,否者直接返回目標(biāo)本身,減少目標(biāo)被代理的  
    // 次數(shù)  
    if (target instanceof CachingExecutor) {  
        return Plugin.wrap(target, this);  
    } else {  
        return target;  
    }  
}

? 著作權(quán)歸作者所有

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容