Java Code Examples for org.apache.ibatis.cache.CacheKey#update()

The following examples show how to use org.apache.ibatis.cache.CacheKey#update() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqlServerDialect.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
@Override
public String getPageSql(String sql, Page page, CacheKey pageKey) {
    //处理pageKey
    pageKey.update(page.getStartRow());
    pageKey.update(page.getPageSize());
    String cacheSql = CACHE_PAGESQL.get(sql);
    if (cacheSql == null) {
        cacheSql = sql;
        cacheSql = replaceSql.replace(cacheSql);
        cacheSql = pageSql.convertToPageSql(cacheSql, null, null);
        cacheSql = replaceSql.restore(cacheSql);
        CACHE_PAGESQL.put(sql, cacheSql);
    }
    cacheSql = cacheSql.replace(String.valueOf(Long.MIN_VALUE), String.valueOf(page.getStartRow()));
    cacheSql = cacheSql.replace(String.valueOf(Long.MAX_VALUE), String.valueOf(page.getPageSize()));
    return cacheSql;
}
 
Example 2
Source File: HerdDBDialect.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
@Override
public Object processPageParameter(MappedStatement ms, Map<String, Object> paramMap, Page page, BoundSql boundSql, CacheKey pageKey) {
    paramMap.put(PAGEPARAMETER_FIRST, page.getStartRow());
    paramMap.put(PAGEPARAMETER_SECOND, page.getPageSize());
    pageKey.update(page.getStartRow());
    pageKey.update(page.getPageSize());
    if (boundSql.getParameterMappings() != null) {
        List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>(boundSql.getParameterMappings());
        if (page.getStartRow() == 0) {
            newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, Integer.class).build());
        } else {
            newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_FIRST, Integer.class).build());
            newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, Integer.class).build());
        }
        MetaObject metaObject = MetaObjectUtil.forObject(boundSql);
        metaObject.setValue("parameterMappings", newParameterMappings);
    }
    return paramMap;
}
 
Example 3
Source File: DefaultResultSetHandler.java    From mybaties with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: MySqlRowBoundsDialect.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
@Override
public String getPageSql(String sql, RowBounds rowBounds, CacheKey pageKey) {
    StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14);
    sqlBuilder.append(sql);
    if (rowBounds.getOffset() == 0) {
        sqlBuilder.append(" LIMIT ");
        sqlBuilder.append(rowBounds.getLimit());
    } else {
        sqlBuilder.append(" LIMIT ");
        sqlBuilder.append(rowBounds.getOffset());
        sqlBuilder.append(",");
        sqlBuilder.append(rowBounds.getLimit());
        pageKey.update(rowBounds.getOffset());
    }
    pageKey.update(rowBounds.getLimit());
    return sqlBuilder.toString();
}
 
Example 5
Source File: HsqldbDialect.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
@Override
public Object processPageParameter(MappedStatement ms, Map<String, Object> paramMap, Page page, BoundSql boundSql, CacheKey pageKey) {
    paramMap.put(PAGEPARAMETER_FIRST, page.getPageSize());
    paramMap.put(PAGEPARAMETER_SECOND, page.getStartRow());
    //处理pageKey
    pageKey.update(page.getPageSize());
    pageKey.update(page.getStartRow());
    //处理参数配置
    if (boundSql.getParameterMappings() != null) {
        List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>(boundSql.getParameterMappings());
        if (page.getPageSize() > 0) {
            newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_FIRST, Integer.class).build());
        }
        if (page.getStartRow() > 0) {
            newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, Integer.class).build());
        }
        MetaObject metaObject = MetaObjectUtil.forObject(boundSql);
        metaObject.setValue("parameterMappings", newParameterMappings);
    }
    return paramMap;
}
 
Example 6
Source File: DefaultResultSetHandler.java    From mybaties with Apache License 2.0 6 votes vote down vote up
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.isEmpty()) {
      // 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);
      }
    }
  }
}
 
Example 7
Source File: HerdDBRowBoundsDialect.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
@Override
public String getPageSql(String sql, RowBounds rowBounds, CacheKey pageKey) {
    StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14);
    sqlBuilder.append(sql);
    if (rowBounds.getOffset() == 0) {
        sqlBuilder.append(" LIMIT ");
        sqlBuilder.append(rowBounds.getLimit());
    } else {
        sqlBuilder.append(" LIMIT ");
        sqlBuilder.append(rowBounds.getOffset());
        sqlBuilder.append(",");
        sqlBuilder.append(rowBounds.getLimit());
        pageKey.update(rowBounds.getOffset());
    }
    pageKey.update(rowBounds.getLimit());
    return sqlBuilder.toString();
}
 
Example 8
Source File: MySqlDialect.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
@Override
public Object processPageParameter(MappedStatement ms, Map<String, Object> paramMap, Page page, BoundSql boundSql, CacheKey pageKey) {
    paramMap.put(PAGEPARAMETER_FIRST, page.getStartRow());
    paramMap.put(PAGEPARAMETER_SECOND, page.getPageSize());
    //处理pageKey
    pageKey.update(page.getStartRow());
    pageKey.update(page.getPageSize());
    //处理参数配置
    if (boundSql.getParameterMappings() != null) {
        List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>(boundSql.getParameterMappings());
        if (page.getStartRow() == 0) {
            newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, Integer.class).build());
        } else {
            newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_FIRST, Integer.class).build());
            newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, Integer.class).build());
        }
        MetaObject metaObject = MetaObjectUtil.forObject(boundSql);
        metaObject.setValue("parameterMappings", newParameterMappings);
    }
    return paramMap;
}
 
Example 9
Source File: DefaultResultSetHandler.java    From mybatis with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 10
Source File: DefaultResultSetHandler.java    From mybaties with Apache License 2.0 5 votes vote down vote up
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);
    }
  }
}
 
Example 11
Source File: SqlServerDialect.java    From Mybatis-PageHelper with MIT License 5 votes vote down vote up
/**
 * 分页查询,pageHelper转换SQL时报错with(nolock)不识别的问题,
 * 重写父类AbstractHelperDialect.getPageSql转换出错的方法。
 * 1. this.replaceSql.replace(sql);先转换成假的表名
 * 2. 然后进行SQL转换
 * 3. this.replaceSql.restore(sql);最后再恢复成真的with(nolock)
 */
@Override
public String getPageSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey pageKey) {
    String sql = boundSql.getSql();
    Page page = this.getLocalPage();
    String orderBy = page.getOrderBy();
    if (StringUtil.isNotEmpty(orderBy)) {
        pageKey.update(orderBy);
        sql = this.replaceSql.replace(sql);
        sql = OrderByParser.converToOrderBySql(sql, orderBy);
        sql = this.replaceSql.restore(sql);
    }

    return page.isOrderByOnly() ? sql : this.getPageSql(sql, page, pageKey);
}
 
Example 12
Source File: DefaultResultSetHandler.java    From mybatis with Apache License 2.0 5 votes vote down vote up
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);
    }
  }
}
 
Example 13
Source File: OracleDialect.java    From Mybatis-PageHelper with MIT License 5 votes vote down vote up
@Override
public Object processPageParameter(MappedStatement ms, Map<String, Object> paramMap, Page page, BoundSql boundSql, CacheKey pageKey) {
    paramMap.put(PAGEPARAMETER_FIRST, page.getEndRow());
    paramMap.put(PAGEPARAMETER_SECOND, page.getStartRow());
    //处理pageKey
    pageKey.update(page.getEndRow());
    pageKey.update(page.getStartRow());
    //处理参数配置
    handleParameter(boundSql, ms);
    return paramMap;
}
 
Example 14
Source File: DefaultResultSetHandler.java    From mybatis with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 15
Source File: SqlServer2012Dialect.java    From Mybatis-PageHelper with MIT License 5 votes vote down vote up
@Override
public Object processPageParameter(MappedStatement ms, Map<String, Object> paramMap, Page page, BoundSql boundSql, CacheKey pageKey) {
    paramMap.put(PAGEPARAMETER_FIRST, page.getStartRow());
    paramMap.put(PAGEPARAMETER_SECOND, page.getPageSize());
    //处理pageKey
    pageKey.update(page.getStartRow());
    pageKey.update(page.getPageSize());
    //处理参数配置
    handleParameter(boundSql, ms);
    return paramMap;
}
 
Example 16
Source File: AbstractHelperDialect.java    From Mybatis-PageHelper with MIT License 5 votes vote down vote up
@Override
public String getPageSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey pageKey) {
    String sql = boundSql.getSql();
    Page page = getLocalPage();
    //支持 order by
    String orderBy = page.getOrderBy();
    if (StringUtil.isNotEmpty(orderBy)) {
        pageKey.update(orderBy);
        sql = OrderByParser.converToOrderBySql(sql, orderBy);
    }
    if (page.isOrderByOnly()) {
        return sql;
    }
    return getPageSql(sql, page, pageKey);
}
 
Example 17
Source File: SqlServer2012RowBoundsDialect.java    From Mybatis-PageHelper with MIT License 5 votes vote down vote up
@Override
public String getPageSql(String sql, RowBounds rowBounds, CacheKey pageKey) {
    StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14);
    sqlBuilder.append(sql);
    sqlBuilder.append(" OFFSET ");
    sqlBuilder.append(rowBounds.getOffset());
    sqlBuilder.append(" ROWS ");
    pageKey.update(rowBounds.getOffset());
    sqlBuilder.append(" FETCH NEXT ");
    sqlBuilder.append(rowBounds.getLimit());
    sqlBuilder.append(" ROWS ONLY");
    pageKey.update(rowBounds.getLimit());
    return sqlBuilder.toString();
}
 
Example 18
Source File: MyBatisCacheTestDriver.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static CacheKey createKey() {

      final CacheKey cacheKey = new CacheKey();
      cacheKey.update("org.mybatis.example.AccountMapper.getAccountByUsername");
      cacheKey.update("Key Object 1");
      cacheKey.update("Key Object 2");
      return cacheKey;
   }
 
Example 19
Source File: PaginationHandler.java    From sqlhelper with GNU Lesser General Public License v3.0 4 votes vote down vote up
private int executeCount(final MappedStatement ms, final Object parameter, final RowBounds rowBounds, final ResultHandler resultHandler, final Executor executor, final BoundSql boundSql) throws Throwable {
    final PagingRequestContext requestContext = PAGING_CONTEXT.get();
    final PagingRequest request = PAGING_CONTEXT.getPagingRequest();
    final String countStatementId = this.getCountStatementId(request, ms.getId());
    int count;
    BoundSql countBoundSql = null;
    try {
        MappedStatement countStatement = this.extractCountStatementFromConfiguration(ms.getConfiguration(), countStatementId);
        if (countStatement != null) {
            final CacheKey countKey = executor.createCacheKey(countStatement, parameter, RowBounds.DEFAULT, boundSql);
            countKey.update(request.getPageNo());
            countKey.update(request.getPageSize());
            countBoundSql = countStatement.getBoundSql(parameter);
            requestContext.set(MybatisSqlRequestContextKeys.COUNT_SQL, countBoundSql);
            final Object countResultList = executor.query(countStatement, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);
            count = ((Number) ((List) countResultList).get(0)).intValue();
        } else {
            String querySql = boundSql.getSql();
            SQLStatementInstrumentor instrumentor = SqlHelperMybatisPlugin.getInstrumentor();
            final String countSql = instrumentor.countSql(querySql, request.getCountColumn());
            countStatement = this.customCountStatement(ms, countStatementId, querySql, request);

            final CacheKey countKey2 = executor.createCacheKey(countStatement, parameter, RowBounds.DEFAULT, boundSql);
            countKey2.update(request.getPageNo());
            countKey2.update(request.getPageSize());

            countBoundSql = MybatisUtils.rebuildBoundSql(countSql, countStatement.getConfiguration(), boundSql);
            requestContext.set(MybatisSqlRequestContextKeys.COUNT_SQL, countBoundSql);
            final Object countResultList2 = executor.query(countStatement, parameter, RowBounds.DEFAULT, resultHandler, countKey2, countBoundSql);
            count = ((Number) ((List) countResultList2).get(0)).intValue();
        }
    } catch (Throwable ex) {
        if (countBoundSql != null) {
            logger.error("error occur when execute count sql [{}], error: {}", countBoundSql.getSql(), ex.getMessage(), ex);
        }
        throw ex;
    } finally {
        requestContext.set(MybatisSqlRequestContextKeys.COUNT_SQL, null);
    }
    return count;
}
 
Example 20
Source File: MyBatisCacheKeyTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 3 votes vote down vote up
public void setUp() throws Exception {

      super.setUp();

      final CacheKey cacheKey = new CacheKey();
      cacheKey.update("Object 1");
      cacheKey.update("Object 2");

      myBatisCacheKey = new MyBatisCacheKey(cacheKey);
   }