Search in sources :

Example 1 with Limitable

use of indi.mybatis.flying.models.Limitable in project mybatis.flying by limeng32.

the class EnhancedCachingInterceptor method createCacheKey.

private CacheKey createCacheKey(MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, BoundSql boundSql, Executor executor) {
    MetaObject metaObject = MetaObject.forObject(parameter, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
    /* 当需要分页查询时,将page参数里的当前页和每页数加到cachekey里 */
    if (metaObject.getOriginalObject() instanceof Conditionable) {
        CacheKey cacheKey = new CacheKey();
        cacheKey.update(mappedStatement.getId());
        cacheKey.update(rowBounds.getOffset());
        cacheKey.update(rowBounds.getLimit());
        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
        cacheKey.update(boundSql.getSql());
        if (parameterMappings.size() > 0 && parameter != null) {
            TypeHandlerRegistry typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
            if (typeHandlerRegistry.hasTypeHandler(parameter.getClass())) {
                cacheKey.update(parameter);
            } 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));
                    }
                }
            }
        }
        Sortable sorter = ((Conditionable) metaObject.getOriginalObject()).getSorter();
        if (sorter != null) {
            cacheKey.update(sorter.toSql());
        }
        Limitable limiter = ((Conditionable) metaObject.getOriginalObject()).getLimiter();
        if (limiter != null) {
            cacheKey.update(limiter.getPageNo());
            cacheKey.update(limiter.getPageSize());
        }
        return cacheKey;
    } else {
        return executor.createCacheKey(mappedStatement, parameter, rowBounds, boundSql);
    }
}
Also used : Conditionable(indi.mybatis.flying.models.Conditionable) TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) Sortable(indi.mybatis.flying.models.Sortable) Limitable(indi.mybatis.flying.models.Limitable) CacheKey(org.apache.ibatis.cache.CacheKey)

Aggregations

Conditionable (indi.mybatis.flying.models.Conditionable)1 Limitable (indi.mybatis.flying.models.Limitable)1 Sortable (indi.mybatis.flying.models.Sortable)1 CacheKey (org.apache.ibatis.cache.CacheKey)1 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)1