Search in sources :

Example 1 with CacheKey

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

the class BaseExecutor method createCacheKey.

@Override
public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {
    if (closed) {
        throw new ExecutorException("Executor was closed.");
    }
    CacheKey cacheKey = new CacheKey();
    cacheKey.update(ms.getId());
    cacheKey.update(rowBounds.getOffset());
    cacheKey.update(rowBounds.getLimit());
    cacheKey.update(boundSql.getSql());
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    TypeHandlerRegistry typeHandlerRegistry = ms.getConfiguration().getTypeHandlerRegistry();
    // mimic DefaultParameterHandler logic
    for (ParameterMapping parameterMapping : parameterMappings) {
        if (parameterMapping.getMode() != ParameterMode.OUT) {
            Object value;
            String propertyName = parameterMapping.getProperty();
            if (boundSql.hasAdditionalParameter(propertyName)) {
                value = boundSql.getAdditionalParameter(propertyName);
            } else if (parameterObject == null) {
                value = null;
            } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
                value = parameterObject;
            } else {
                MetaObject metaObject = configuration.newMetaObject(parameterObject);
                value = metaObject.getValue(propertyName);
            }
            cacheKey.update(value);
        }
    }
    if (configuration.getEnvironment() != null) {
        // issue #176
        cacheKey.update(configuration.getEnvironment().getId());
    }
    return cacheKey;
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 2 with CacheKey

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

the class BaseExecutor method query.

@Override
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
    BoundSql boundSql = ms.getBoundSql(parameter);
    CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql);
    return query(ms, parameter, rowBounds, resultHandler, key, boundSql);
}
Also used : BoundSql(org.apache.ibatis.mapping.BoundSql) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 3 with CacheKey

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

the class DefaultResultSetHandler method getNestedQueryConstructorValue.

//
// NESTED QUERY
//
private Object getNestedQueryConstructorValue(ResultSet rs, ResultMapping constructorMapping, String columnPrefix) throws SQLException {
    final String nestedQueryId = constructorMapping.getNestedQueryId();
    final MappedStatement nestedQuery = configuration.getMappedStatement(nestedQueryId);
    final Class<?> nestedQueryParameterType = nestedQuery.getParameterMap().getType();
    final Object nestedQueryParameterObject = prepareParameterForNestedQuery(rs, constructorMapping, 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 = constructorMapping.getJavaType();
        final ResultLoader resultLoader = new ResultLoader(configuration, executor, nestedQuery, nestedQueryParameterObject, targetType, key, nestedBoundSql);
        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 4 with CacheKey

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

the class CachingExecutor method query.

@Override
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
    BoundSql boundSql = ms.getBoundSql(parameterObject);
    CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
    return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}
Also used : BoundSql(org.apache.ibatis.mapping.BoundSql) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 5 with CacheKey

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

the class DefaultResultSetHandler method createRowKey.

//
// UNIQUE RESULT KEY
//
private CacheKey createRowKey(ResultMap resultMap, ResultSetWrapper rsw, String columnPrefix) throws SQLException {
    final CacheKey cacheKey = new CacheKey();
    cacheKey.update(resultMap.getId());
    List<ResultMapping> resultMappings = getResultMappingsForRowKey(resultMap);
    if (resultMappings.size() == 0) {
        if (Map.class.isAssignableFrom(resultMap.getType())) {
            createRowKeyForMap(rsw, cacheKey);
        } else {
            createRowKeyForUnmappedProperties(resultMap, rsw, cacheKey, columnPrefix);
        }
    } else {
        createRowKeyForMappedProperties(resultMap, rsw, cacheKey, resultMappings, columnPrefix);
    }
    if (cacheKey.getUpdateCount() < 2) {
        return CacheKey.NULL_CACHE_KEY;
    }
    return cacheKey;
}
Also used : ResultMapping(org.apache.ibatis.mapping.ResultMapping) 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