Search in sources :

Example 11 with CacheKey

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

the class DefaultResultSetHandler method handleRowValuesForNestedResultMap.

//
// HANDLE NESTED RESULT MAPS
//
private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
    final DefaultResultContext<Object> resultContext = new DefaultResultContext<Object>();
    skipRows(rsw.getResultSet(), rowBounds);
    Object rowValue = previousRowValue;
    while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) {
        final ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null);
        final CacheKey rowKey = createRowKey(discriminatedResultMap, rsw, null);
        Object partialObject = nestedResultObjects.get(rowKey);
        // issue #577 && #542
        if (mappedStatement.isResultOrdered()) {
            if (partialObject == null && rowValue != null) {
                nestedResultObjects.clear();
                storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
            }
            rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, null, partialObject);
        } else {
            rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, null, partialObject);
            if (partialObject == null) {
                storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
            }
        }
    }
    if (rowValue != null && mappedStatement.isResultOrdered() && shouldProcessMoreRows(resultContext, rowBounds)) {
        storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
        previousRowValue = null;
    } else if (rowValue != null) {
        previousRowValue = rowValue;
    }
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) MetaObject(org.apache.ibatis.reflection.MetaObject) DefaultResultContext(org.apache.ibatis.executor.result.DefaultResultContext) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 12 with CacheKey

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

the class DefaultResultSetHandler method linkToParents.

// MULTIPLE RESULT SETS
private void linkToParents(ResultSet rs, ResultMapping parentMapping, Object rowValue) throws SQLException {
    CacheKey parentKey = createKeyForMultipleResults(rs, parentMapping, parentMapping.getColumn(), parentMapping.getForeignColumn());
    List<PendingRelation> parents = pendingRelations.get(parentKey);
    if (parents != null) {
        for (PendingRelation parent : parents) {
            if (parent != null && rowValue != null) {
                linkObjects(parent.metaObject, parent.propertyMapping, rowValue);
            }
        }
    }
}
Also used : CacheKey(org.apache.ibatis.cache.CacheKey)

Example 13 with CacheKey

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

the class DefaultResultSetHandler method getNestedQueryMappingValue.

private Object getNestedQueryMappingValue(ResultSet rs, MetaObject metaResultObject, ResultMapping propertyMapping, ResultLoaderMap lazyLoader, String columnPrefix) throws SQLException {
    final String nestedQueryId = propertyMapping.getNestedQueryId();
    final String property = propertyMapping.getProperty();
    final MappedStatement nestedQuery = configuration.getMappedStatement(nestedQueryId);
    final Class<?> nestedQueryParameterType = nestedQuery.getParameterMap().getType();
    final Object nestedQueryParameterObject = prepareParameterForNestedQuery(rs, propertyMapping, nestedQueryParameterType, columnPrefix);
    Object value = null;
    if (nestedQueryParameterObject != null) {
        final BoundSql nestedBoundSql = nestedQuery.getBoundSql(nestedQueryParameterObject);
        final CacheKey key = executor.createCacheKey(nestedQuery, nestedQueryParameterObject, RowBounds.DEFAULT, nestedBoundSql);
        final Class<?> targetType = propertyMapping.getJavaType();
        if (executor.isCached(nestedQuery, key)) {
            executor.deferLoad(nestedQuery, metaResultObject, property, key, targetType);
            value = DEFERED;
        } else {
            final ResultLoader resultLoader = new ResultLoader(configuration, executor, nestedQuery, nestedQueryParameterObject, targetType, key, nestedBoundSql);
            if (propertyMapping.isLazy()) {
                lazyLoader.addLoader(property, metaResultObject, resultLoader);
                value = DEFERED;
            } else {
                value = resultLoader.loadResult();
            }
        }
    }
    return value;
}
Also used : ResultLoader(org.apache.ibatis.executor.loader.ResultLoader) BoundSql(org.apache.ibatis.mapping.BoundSql) MetaObject(org.apache.ibatis.reflection.MetaObject) MappedStatement(org.apache.ibatis.mapping.MappedStatement) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 14 with CacheKey

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

the class DefaultResultSetHandler method createKeyForMultipleResults.

private CacheKey createKeyForMultipleResults(ResultSet rs, ResultMapping resultMapping, String names, String columns) throws SQLException {
    CacheKey cacheKey = new CacheKey();
    cacheKey.update(resultMapping);
    if (columns != null && names != null) {
        String[] columnsArray = columns.split(",");
        String[] namesArray = names.split(",");
        for (int i = 0; i < columnsArray.length; i++) {
            Object value = rs.getString(columnsArray[i]);
            if (value != null) {
                cacheKey.update(namesArray[i]);
                cacheKey.update(value);
            }
        }
    }
    return cacheKey;
}
Also used : MetaObject(org.apache.ibatis.reflection.MetaObject) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 15 with CacheKey

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

the class OffsetLimitInterceptor method intercept.

public Object intercept(final Invocation invocation) throws Throwable {
    final Executor executor = (Executor) invocation.getTarget();
    final Object[] queryArgs = invocation.getArgs();
    final MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
    final Object parameter = queryArgs[PARAMETER_INDEX];
    final RowBounds rowBounds = (RowBounds) queryArgs[ROWBOUNDS_INDEX];
    //DAO接口中没有传PageBounds参量
    if (!(rowBounds instanceof PageBounds)) {
        return invocation.proceed();
    }
    //DAO接口传有PageBounds参量
    PageBounds pageBounds = (PageBounds) rowBounds;
    if (pageBounds.notValid()) {
        return new PageList((List) invocation.proceed());
    }
    final Dialect dialect;
    try {
        Class clazz = Class.forName(dialectClass);
        Constructor constructor = clazz.getConstructor(MappedStatement.class, Object.class, PageBounds.class);
        dialect = (Dialect) constructor.newInstance(new Object[] { ms, parameter, pageBounds });
    } catch (Exception e) {
        throw new ClassNotFoundException("Cannot create dialect instance: " + dialectClass, e);
    }
    final BoundSql boundSql = ms.getBoundSql(parameter);
    queryArgs[MAPPED_STATEMENT_INDEX] = copyFromNewSql(ms, boundSql, dialect.getPageSQL(), dialect.getParameterMappings(), dialect.getParameterObject());
    queryArgs[PARAMETER_INDEX] = dialect.getParameterObject();
    queryArgs[ROWBOUNDS_INDEX] = new RowBounds();
    //采用同步方式,执行分页查询
    //修复bug: 提交后台线程破坏了spring 事务管理,参见TransactionSynchronizationManager
    Callable<List> queryThread = new Callable<List>() {

        public List call() throws Exception {
            return (List) invocation.proceed();
        }
    };
    Future<List> queryFuture = call(queryThread, false);
    //如果不需要count总的结果集,则直接返回分页查询结果
    if (!pageBounds.isIfCount()) {
        return new PageList(queryFuture.get());
    }
    //对总的结果集进行count
    Callable<Integer> countThread = new Callable<Integer>() {

        public Integer call() throws Exception {
            Cache cache = ms.getCache();
            Integer count = null;
            if (cache != null && ms.isUseCache() && ms.getConfiguration().isCacheEnabled()) {
                CacheKey cacheKey = executor.createCacheKey(ms, parameter, new RowBounds(), copyFromBoundSql(ms, boundSql, dialect.getCountSQL(), boundSql.getParameterMappings(), boundSql.getParameterObject()));
                count = (Integer) cache.getObject(cacheKey);
                if (count == null) {
                    count = SQLHelp.getCount(ms, parameter, boundSql, dialect);
                    cache.putObject(cacheKey, count);
                }
            } else {
                count = SQLHelp.getCount(ms, parameter, boundSql, dialect);
            }
            return count;
        }
    };
    Future<Integer> countFutrue = call(countThread, false);
    return new PageList(queryFuture.get(), countFutrue.get().intValue());
}
Also used : Constructor(java.lang.reflect.Constructor) RowBounds(org.apache.ibatis.session.RowBounds) Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) Dialect(com.github.walker.mybatis.paginator.dialect.Dialect) List(java.util.List) MappedStatement(org.apache.ibatis.mapping.MappedStatement) CacheKey(org.apache.ibatis.cache.CacheKey) Cache(org.apache.ibatis.cache.Cache)

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