private void createRowKeyForMappedProperties( ResultMap resultMap, ResultSetWrapper rsw, CacheKey cacheKey, List<ResultMapping> resultMappings, String columnPrefix) throws SQLException { for (ResultMapping resultMapping : resultMappings) { if (resultMapping.getNestedResultMapId() != null && resultMapping.getResultSet() == null) { // Issue #392 final ResultMap nestedResultMap = configuration.getResultMap(resultMapping.getNestedResultMapId()); createRowKeyForMappedProperties( nestedResultMap, rsw, cacheKey, nestedResultMap.getConstructorResultMappings(), prependPrefix(resultMapping.getColumnPrefix(), columnPrefix)); } else if (resultMapping.getNestedQueryId() == null) { final String column = prependPrefix(resultMapping.getColumn(), columnPrefix); final TypeHandler<?> th = resultMapping.getTypeHandler(); List<String> mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix); if (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH))) { // Issue #114 final Object value = th.getResult(rsw.getResultSet(), column); if (value != null) { cacheKey.update(column); cacheKey.update(value); } } } } }
private void createRowKeyForUnmappedProperties( ResultMap resultMap, ResultSetWrapper rsw, CacheKey cacheKey, String columnPrefix) throws SQLException { final MetaClass metaType = MetaClass.forClass(resultMap.getType()); List<String> unmappedColumnNames = rsw.getUnmappedColumnNames(resultMap, columnPrefix); for (String column : unmappedColumnNames) { String property = column; if (columnPrefix != null && columnPrefix.length() > 0) { // When columnPrefix is specified, // ignore columns without the prefix. if (column.toUpperCase(Locale.ENGLISH).startsWith(columnPrefix)) { property = column.substring(columnPrefix.length()); } else { continue; } } if (metaType.findProperty(property, configuration.isMapUnderscoreToCamelCase()) != null) { String value = rsw.getResultSet().getString(column); if (value != null) { cacheKey.update(column); cacheKey.update(value); } } } }
private void createRowKeyForMap(ResultSetWrapper rsw, CacheKey cacheKey) throws SQLException { List<String> columnNames = rsw.getColumnNames(); for (String columnName : columnNames) { final String value = rsw.getResultSet().getString(columnName); if (value != null) { cacheKey.update(columnName); cacheKey.update(value); } } }
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; }
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(); if (parameterMappings.size() > 0 && parameterObject != null) { TypeHandlerRegistry typeHandlerRegistry = ms.getConfiguration().getTypeHandlerRegistry(); if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { cacheKey.update(parameterObject); } else { MetaObject metaObject = configuration.newMetaObject(parameterObject); 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)); } } } } return cacheKey; }
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); } return cacheKey; }
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; }
/** * @description TODO * @createTime 2016-4-19 上午11:12:22 * @fileName CacheInterceptor.java * @author yaojiamin */ private CacheKey cloneCacheKey(CacheKey cacheKey) { try { return cacheKey.clone(); } catch (CloneNotSupportedException e) { logger.error("Failed to clone CacheKey!!!", e); } return cacheKey; }
/** * @description TODO * @createTime 2016-5-31 下午4:53:32 * @fileName CacheInterceptor.java * @author yaojiamin * @param boundSql * @param cacheKey */ @SuppressWarnings({}) public <E> List<E> query(Executor executor, CacheHandlerBean cacheHandlerBean) throws Exception { BoundSql boundSql = cacheHandlerBean.getMappedStatement().getBoundSql(cacheHandlerBean.getParameterObject()); CacheKey cacheKey = executor.createCacheKey( cacheHandlerBean.getMappedStatement(), cacheHandlerBean.getParameterObject(), cacheHandlerBean.getRowBounds(), boundSql); MetaObject metaPage = forObject(cacheHandlerBean.getParameterObject()); // 当需要分页查询时,将page参数里的当前页和每页数加到cachekey里 if (isNeedPagination(metaPage)) { executeQuery( cacheHandlerBean, createPageBoundSql(cacheHandlerBean, boundSql), cloneCacheKey(cacheKey), executor); Assert.isTrue(metaPage.getValue(Constants.TOTAL) != null, "Fail to count result!!!"); cacheKey.update(metaPage.getValue(Constants.PAGE_NO)); cacheKey.update(metaPage.getValue(Constants.PAGE_SIZE)); } return executeQuery(cacheHandlerBean, boundSql, cacheKey, executor); }