/**
   * when executing a query operation 1. record this statement's id and it's corresponding Cache
   * Object into Global Caching Manager; 2. record this statement's id and
   *
   * @param invocation
   * @return
   * @throws Throwable
   */
  private Object processQuery(Invocation invocation) throws Throwable {
    Object result = invocation.proceed();
    if (cachingManager.isCacheEnabled()) {
      Object[] args = invocation.getArgs();
      MappedStatement mappedStatement = (MappedStatement) args[0];

      // 如果本条statementId表示的查询语句配置了 flushCache=true,则清空querCacheOnCommit缓存
      if (mappedStatement.isFlushCacheRequired()) {
        queryCacheOnCommit.clear();
      }
      // 如果本条statementId表示的查询语句配置了使用缓存,并且二级缓存不为空,则将StatementId
      // 和对应的二级缓存对象映射关系添加到全局缓存映射管理器中
      if (mappedStatement.isUseCache() && mappedStatement.getCache() != null) {
        cachingManager.appendStatementCacheMap(mappedStatement.getId(), mappedStatement.getCache());
      }

      Object parameter = args[1];
      RowBounds rowBounds = (RowBounds) args[2];
      Executor executor = (Executor) invocation.getTarget();
      BoundSql boundSql = mappedStatement.getBoundSql(parameter);

      // 记录本次查询所产生的CacheKey
      CacheKey cacheKey = executor.createCacheKey(mappedStatement, parameter, rowBounds, boundSql);
      queryCacheOnCommit.putElement(mappedStatement.getId(), cacheKey);
    }

    return result;
  }
 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;
 }
 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 = NO_VALUE;
   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);
     } else {
       final ResultLoader resultLoader =
           new ResultLoader(
               configuration,
               executor,
               nestedQuery,
               nestedQueryParameterObject,
               targetType,
               key,
               nestedBoundSql);
       if (propertyMapping.isLazy()) {
         lazyLoader.addLoader(property, metaResultObject, resultLoader);
       } else {
         value = resultLoader.loadResult();
       }
     }
   }
   return value;
 }
示例#4
0
 /**
  * @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);
 }