转载

9.改造Cache插件

在前面的文章里,介绍了两个插件:根据注解实现的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自动生成插件)或加入分页信息(对应分页插件)就可以了。

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的方法。

3 intercept实现

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实现

public Object plugin(Object target) {  
    // 当目标类是CachingExecutor类型时,才包装目标类,否者直接返回目标本身,减少目标被代理的  
    // 次数  
    if (target instanceof CachingExecutor) {  
        return Plugin.wrap(target, this);  
    } else {  
        return target;  
    }  
}
正文到此结束
Loading...