Exemple #1
0
 private void setCacheProperties(Cache cache) {
   if (properties != null) {
     MetaObject metaCache = SystemMetaObject.forObject(cache);
     for (Map.Entry<Object, Object> entry : properties.entrySet()) {
       String name = (String) entry.getKey();
       String value = (String) entry.getValue();
       if (metaCache.hasSetter(name)) {
         Class<?> type = metaCache.getSetterType(name);
         if (String.class == type) {
           metaCache.setValue(name, value);
         } else if (int.class == type || Integer.class == type) {
           metaCache.setValue(name, Integer.valueOf(value));
         } else if (long.class == type || Long.class == type) {
           metaCache.setValue(name, Long.valueOf(value));
         } else if (short.class == type || Short.class == type) {
           metaCache.setValue(name, Short.valueOf(value));
         } else if (byte.class == type || Byte.class == type) {
           metaCache.setValue(name, Byte.valueOf(value));
         } else if (float.class == type || Float.class == type) {
           metaCache.setValue(name, Float.valueOf(value));
         } else if (boolean.class == type || Boolean.class == type) {
           metaCache.setValue(name, Boolean.valueOf(value));
         } else if (double.class == type || Double.class == type) {
           metaCache.setValue(name, Double.valueOf(value));
         } else {
           throw new CacheException(
               "Unsupported property type for cache: '" + name + "' of type " + type);
         }
       }
     }
   }
 }
 public PageDynamicSqlSource(DynamicSqlSource sqlSource, Parser parser) {
   MetaObject metaObject = SystemMetaObject.forObject(sqlSource);
   this.configuration = (Configuration) metaObject.getValue("configuration");
   this.rootSqlNode = (SqlNode) metaObject.getValue("rootSqlNode");
   this.original = sqlSource;
   this.parser = parser;
 }
 public PageStaticSqlSource(StaticSqlSource sqlSource, Parser parser) {
   MetaObject metaObject = SystemMetaObject.forObject(sqlSource);
   this.sql = (String) metaObject.getValue("sql");
   this.parameterMappings = (List<ParameterMapping>) metaObject.getValue("parameterMappings");
   this.configuration = (Configuration) metaObject.getValue("configuration");
   this.original = sqlSource;
   this.parser = parser;
 }
 /**
  * 对象中获取分页参数
  *
  * @param params
  * @return
  */
 public static Page getPageFromObject(Object params) {
   int pageNum;
   int pageSize;
   MetaObject paramsObject = null;
   if (params == null) {
     throw new NullPointerException("分页查询参数params不能为空!");
   }
   if (hasRequest && requestClass.isAssignableFrom(params.getClass())) {
     try {
       paramsObject = SystemMetaObject.forObject(getParameterMap.invoke(params, new Object[] {}));
     } catch (Exception e) {
       // 忽略
     }
   } else {
     paramsObject = SystemMetaObject.forObject(params);
   }
   if (paramsObject == null) {
     throw new NullPointerException("分页查询参数params处理失败!");
   }
   try {
     pageNum = Integer.parseInt(String.valueOf(getParamValue(paramsObject, "pageNum", true)));
     pageSize = Integer.parseInt(String.valueOf(getParamValue(paramsObject, "pageSize", true)));
   } catch (NumberFormatException e) {
     throw new IllegalArgumentException("分页参数不是合法的数字类型!");
   }
   Object _count = getParamValue(paramsObject, "count", false);
   boolean count = true;
   if (_count != null) {
     count = Boolean.valueOf(String.valueOf(_count));
   }
   Page page = new Page(pageNum, pageSize, count);
   Object reasonable = getParamValue(paramsObject, "reasonable", false);
   if (reasonable != null) {
     page.setReasonable(Boolean.valueOf(String.valueOf(reasonable)));
   }
   Object pageSizeZero = getParamValue(paramsObject, "pageSizeZero", false);
   if (pageSizeZero != null) {
     page.setPageSizeZero(Boolean.valueOf(String.valueOf(pageSizeZero)));
   }
   return page;
 }
Exemple #5
0
 private Cache setStandardDecorators(Cache cache) {
   try {
     MetaObject metaCache = SystemMetaObject.forObject(cache);
     if (size != null && metaCache.hasSetter("size")) {
       metaCache.setValue("size", size);
     }
     if (clearInterval != null) {
       cache = new ScheduledCache(cache);
       ((ScheduledCache) cache).setClearInterval(clearInterval);
     }
     if (readWrite) {
       cache = new SerializedCache(cache);
     }
     cache = new LoggingCache(cache);
     cache = new SynchronizedCache(cache);
     return cache;
   } catch (Exception e) {
     throw new CacheException("Error building standard cache decorators.  Cause: " + e, e);
   }
 }
 /**
  * 修改SqlSource
  *
  * @param ms
  * @param parser
  * @throws Throwable
  */
 public static void processMappedStatement(MappedStatement ms, Parser parser) throws Throwable {
   SqlSource sqlSource = ms.getSqlSource();
   MetaObject msObject = SystemMetaObject.forObject(ms);
   SqlSource tempSqlSource = sqlSource;
   if (sqlSource instanceof OrderBySqlSource) {
     tempSqlSource = ((OrderBySqlSource) tempSqlSource).getOriginal();
   }
   SqlSource pageSqlSource;
   if (tempSqlSource instanceof StaticSqlSource) {
     pageSqlSource = new PageStaticSqlSource((StaticSqlSource) tempSqlSource, parser);
   } else if (tempSqlSource instanceof RawSqlSource) {
     pageSqlSource = new PageRawSqlSource((RawSqlSource) tempSqlSource, parser);
   } else if (tempSqlSource instanceof ProviderSqlSource) {
     pageSqlSource = new PageProviderSqlSource((ProviderSqlSource) tempSqlSource, parser);
   } else if (tempSqlSource instanceof DynamicSqlSource) {
     pageSqlSource = new PageDynamicSqlSource((DynamicSqlSource) tempSqlSource, parser);
   } else {
     throw new RuntimeException("无法处理该类型[" + sqlSource.getClass() + "]的SqlSource");
   }
   msObject.setValue("sqlSource", pageSqlSource);
   // 由于count查询需要修改返回值,因此这里要创建一个Count查询的MS
   msCountMap.put(ms.getId(), MSUtils.newCountMappedStatement(ms));
 }
  public Object intercept(Invocation invocation) throws Throwable {

    RoutingStatementHandler statementHandler = (RoutingStatementHandler) invocation.getTarget();
    BoundSql boundSql = statementHandler.getBoundSql();
    MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);

    BaseStatementHandler delegate =
        (BaseStatementHandler) ReflectHelper.getValueByFieldName(statementHandler, "delegate");
    MappedStatement mappedStatement =
        (MappedStatement) ReflectHelper.getValueByFieldName(delegate, "mappedStatement");
    Connection connection = (Connection) invocation.getArgs()[0];

    // 获取当前准备执行的行边界
    RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");

    // 判断是否设置分页
    if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
      return invocation.proceed();
    }

    Configuration configuration =
        (Configuration) metaStatementHandler.getValue("delegate.configuration");
    Dialect.Type databaseType = null;
    // 获取当前的数据库方言
    try {

      databaseType =
          Dialect.Type.valueOf(configuration.getVariables().getProperty("dialect").toUpperCase());

    } catch (Exception e) {
      // ignore
      throw e;
    }

    if (databaseType == null) {
      throw new RuntimeException(
          "the value of the dialect property in configuration.xml is not defined : "
              + configuration.getVariables().getProperty("dialect"));
    }

    Dialect dialect = null;
    // 判断数据库方言
    switch (databaseType) {
      case ORACLE:
        dialect = new OracleDialect();
    }
    // 原始sql
    String originalSql = (String) metaStatementHandler.getValue("delegate.boundSql.sql");
    // 统计总数
    int count = getCount(connection, boundSql, configuration, mappedStatement);
    Object obj = boundSql.getParameterObject();
    if (null != obj && obj instanceof Map) {
      Map parameterObject = (Map) boundSql.getParameterObject();
      parameterObject.put("_dataCount", count);
    } else {
      throw new NullPointerException("service中getGridData的condition参数不能为空!");
    }
    // 设置拼接好的分页sql
    metaStatementHandler.setValue(
        "delegate.boundSql.sql",
        dialect.getPageSql(originalSql, rowBounds.getOffset(), rowBounds.getLimit()));

    // 重置行边界
    metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);

    metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);

    if (log.isDebugEnabled()) {
      log.debug("生成分页SQL : " + boundSql.getSql());
    }

    return invocation.proceed();
  }