Search in sources :

Example 6 with CacheKey

use of org.apache.ibatis.cache.CacheKey in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method addPendingChildRelation.

private void addPendingChildRelation(ResultSet rs, MetaObject metaResultObject, ResultMapping parentMapping) throws SQLException {
    CacheKey cacheKey = createKeyForMultipleResults(rs, parentMapping, parentMapping.getColumn(), parentMapping.getColumn());
    PendingRelation deferLoad = new PendingRelation();
    deferLoad.metaObject = metaResultObject;
    deferLoad.propertyMapping = parentMapping;
    List<PendingRelation> relations = pendingRelations.get(cacheKey);
    // issue #255
    if (relations == null) {
        relations = new ArrayList<DefaultResultSetHandler.PendingRelation>();
        pendingRelations.put(cacheKey, relations);
    }
    relations.add(deferLoad);
    ResultMapping previous = nextResultMaps.get(parentMapping.getResultSet());
    if (previous == null) {
        nextResultMaps.put(parentMapping.getResultSet(), parentMapping);
    } else {
        if (!previous.equals(parentMapping)) {
            throw new ExecutorException("Two different properties are mapped to the same resultSet");
        }
    }
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) ResultMapping(org.apache.ibatis.mapping.ResultMapping) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 7 with CacheKey

use of org.apache.ibatis.cache.CacheKey in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method applyNestedResultMappings.

//
// NESTED RESULT MAP (JOIN MAPPING)
//
private boolean applyNestedResultMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String parentPrefix, CacheKey parentRowKey, boolean newObject) {
    boolean foundValues = false;
    for (ResultMapping resultMapping : resultMap.getPropertyResultMappings()) {
        final String nestedResultMapId = resultMapping.getNestedResultMapId();
        if (nestedResultMapId != null && resultMapping.getResultSet() == null) {
            try {
                final String columnPrefix = getColumnPrefix(parentPrefix, resultMapping);
                final ResultMap nestedResultMap = getNestedResultMap(rsw.getResultSet(), nestedResultMapId, columnPrefix);
                if (resultMapping.getColumnPrefix() == null) {
                    // try to fill circular reference only when columnPrefix
                    // is not specified for the nested result map (issue #215)
                    Object ancestorObject = ancestorObjects.get(nestedResultMapId);
                    if (ancestorObject != null) {
                        if (newObject) {
                            // issue #385
                            linkObjects(metaObject, resultMapping, ancestorObject);
                        }
                        continue;
                    }
                }
                final CacheKey rowKey = createRowKey(nestedResultMap, rsw, columnPrefix);
                final CacheKey combinedKey = combineKeys(rowKey, parentRowKey);
                Object rowValue = nestedResultObjects.get(combinedKey);
                boolean knownValue = (rowValue != null);
                // mandatory
                instantiateCollectionPropertyIfAppropriate(resultMapping, metaObject);
                if (anyNotNullColumnHasValue(resultMapping, columnPrefix, rsw)) {
                    rowValue = getRowValue(rsw, nestedResultMap, combinedKey, columnPrefix, rowValue);
                    if (rowValue != null && !knownValue) {
                        linkObjects(metaObject, resultMapping, rowValue);
                        foundValues = true;
                    }
                }
            } catch (SQLException e) {
                throw new ExecutorException("Error getting nested result map values for '" + resultMapping.getProperty() + "'.  Cause: " + e, e);
            }
        }
    }
    return foundValues;
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ExecutorException(org.apache.ibatis.executor.ExecutorException) SQLException(java.sql.SQLException) ResultMapping(org.apache.ibatis.mapping.ResultMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 8 with CacheKey

use of org.apache.ibatis.cache.CacheKey in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method combineKeys.

private CacheKey combineKeys(CacheKey rowKey, CacheKey parentRowKey) {
    if (rowKey.getUpdateCount() > 1 && parentRowKey.getUpdateCount() > 1) {
        CacheKey combinedKey;
        try {
            combinedKey = rowKey.clone();
        } catch (CloneNotSupportedException e) {
            throw new ExecutorException("Error cloning cache key.  Cause: " + e, e);
        }
        combinedKey.update(parentRowKey);
        return combinedKey;
    }
    return CacheKey.NULL_CACHE_KEY;
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 9 with CacheKey

use of org.apache.ibatis.cache.CacheKey in project Mybatis-PageHelper by pagehelper.

the class PageInterceptor method intercept.

@Override
public Object intercept(Invocation invocation) throws Throwable {
    try {
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[0];
        Object parameter = args[1];
        RowBounds rowBounds = (RowBounds) args[2];
        ResultHandler resultHandler = (ResultHandler) args[3];
        Executor executor = (Executor) invocation.getTarget();
        CacheKey cacheKey;
        BoundSql boundSql;
        //由于逻辑关系,只会进入一次
        if (args.length == 4) {
            //4 个参数时
            boundSql = ms.getBoundSql(parameter);
            cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
        } else {
            //6 个参数时
            cacheKey = (CacheKey) args[4];
            boundSql = (BoundSql) args[5];
        }
        List resultList;
        //调用方法判断是否需要进行分页,如果不需要,直接返回结果
        if (!dialect.skip(ms, parameter, rowBounds)) {
            //反射获取动态参数
            Map<String, Object> additionalParameters = (Map<String, Object>) additionalParametersField.get(boundSql);
            //判断是否需要进行 count 查询
            if (dialect.beforeCount(ms, parameter, rowBounds)) {
                //创建 count 查询的缓存 key
                CacheKey countKey = executor.createCacheKey(ms, parameter, RowBounds.DEFAULT, boundSql);
                countKey.update(MSUtils.COUNT);
                MappedStatement countMs = msCountMap.get(countKey);
                if (countMs == null) {
                    //根据当前的 ms 创建一个返回值为 Long 类型的 ms
                    countMs = MSUtils.newCountMappedStatement(ms);
                    msCountMap.put(countKey, countMs);
                }
                //调用方言获取 count sql
                String countSql = dialect.getCountSql(ms, boundSql, parameter, rowBounds, countKey);
                BoundSql countBoundSql = new BoundSql(ms.getConfiguration(), countSql, boundSql.getParameterMappings(), parameter);
                //当使用动态 SQL 时,可能会产生临时的参数,这些参数需要手动设置到新的 BoundSql 中
                for (String key : additionalParameters.keySet()) {
                    countBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
                }
                //执行 count 查询
                Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);
                Long count = (Long) ((List) countResultList).get(0);
                //返回 true 时继续分页查询,false 时直接返回
                if (!dialect.afterCount(count, parameter, rowBounds)) {
                    //当查询总数为 0 时,直接返回空的结果
                    return dialect.afterPage(new ArrayList(), parameter, rowBounds);
                }
            }
            //判断是否需要进行分页查询
            if (dialect.beforePage(ms, parameter, rowBounds)) {
                //生成分页的缓存 key
                CacheKey pageKey = cacheKey;
                //处理参数对象
                parameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey);
                //调用方言获取分页 sql
                String pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, pageKey);
                BoundSql pageBoundSql = new BoundSql(ms.getConfiguration(), pageSql, boundSql.getParameterMappings(), parameter);
                //设置动态参数
                for (String key : additionalParameters.keySet()) {
                    pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
                }
                //执行分页查询
                resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql);
            } else {
                //不执行分页的情况下,也不执行内存分页
                resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql);
            }
        } else {
            //rowBounds用参数值,不使用分页插件处理时,仍然支持默认的内存分页
            resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
        }
        return dialect.afterPage(resultList, parameter, rowBounds);
    } finally {
        dialect.afterAll();
    }
}
Also used : ArrayList(java.util.ArrayList) RowBounds(org.apache.ibatis.session.RowBounds) ResultHandler(org.apache.ibatis.session.ResultHandler) Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) ArrayList(java.util.ArrayList) List(java.util.List) MappedStatement(org.apache.ibatis.mapping.MappedStatement) Map(java.util.Map) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 10 with CacheKey

use of org.apache.ibatis.cache.CacheKey in project Mybatis-PageHelper by pagehelper.

the class QueryInterceptor method intercept.

@Override
public Object intercept(Invocation invocation) throws Throwable {
    Object[] args = invocation.getArgs();
    MappedStatement ms = (MappedStatement) args[0];
    Object parameter = args[1];
    RowBounds rowBounds = (RowBounds) args[2];
    ResultHandler resultHandler = (ResultHandler) args[3];
    Executor executor = (Executor) invocation.getTarget();
    CacheKey cacheKey;
    BoundSql boundSql;
    //由于逻辑关系,只会进入一次
    if (args.length == 4) {
        //4 个参数时
        boundSql = ms.getBoundSql(parameter);
        cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
    } else {
        //6 个参数时
        cacheKey = (CacheKey) args[4];
        boundSql = (BoundSql) args[5];
    }
    //注:下面的方法可以根据自己的逻辑调用多次,在分页插件中,count 和 page 各调用了一次
    return executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
}
Also used : Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) RowBounds(org.apache.ibatis.session.RowBounds) MappedStatement(org.apache.ibatis.mapping.MappedStatement) ResultHandler(org.apache.ibatis.session.ResultHandler) CacheKey(org.apache.ibatis.cache.CacheKey)

Aggregations

CacheKey (org.apache.ibatis.cache.CacheKey)15 BoundSql (org.apache.ibatis.mapping.BoundSql)7 MetaObject (org.apache.ibatis.reflection.MetaObject)6 MappedStatement (org.apache.ibatis.mapping.MappedStatement)5 Executor (org.apache.ibatis.executor.Executor)3 ExecutorException (org.apache.ibatis.executor.ExecutorException)3 ResultMapping (org.apache.ibatis.mapping.ResultMapping)3 RowBounds (org.apache.ibatis.session.RowBounds)3 List (java.util.List)2 ResultLoader (org.apache.ibatis.executor.loader.ResultLoader)2 ResultMap (org.apache.ibatis.mapping.ResultMap)2 ResultHandler (org.apache.ibatis.session.ResultHandler)2 Dialect (com.github.walker.mybatis.paginator.dialect.Dialect)1 Constructor (java.lang.reflect.Constructor)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Cache (org.apache.ibatis.cache.Cache)1 DefaultResultContext (org.apache.ibatis.executor.result.DefaultResultContext)1 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)1