Ejemplo n.º 1
0
 private void settingsElement(XNode context) throws Exception {
   if (context != null) {
     Properties props = context.getChildrenAsProperties();
     // Check that all settings are known to the configuration class
     MetaClass metaConfig = MetaClass.forClass(Configuration.class);
     for (Object key : props.keySet()) {
       if (!metaConfig.hasSetter(String.valueOf(key))) {
         throw new BuilderException(
             "The setting "
                 + key
                 + " is not known.  Make sure you spelled it correctly (case sensitive).");
       }
     }
     configuration.setAutoMappingBehavior(
         AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
     configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));
     configuration.setProxyFactory(
         (ProxyFactory) createInstance(props.getProperty("proxyFactory")));
     configuration.setLazyLoadingEnabled(
         booleanValueOf(props.getProperty("lazyLoadingEnabled"), false));
     configuration.setAggressiveLazyLoading(
         booleanValueOf(props.getProperty("aggressiveLazyLoading"), true));
     configuration.setMultipleResultSetsEnabled(
         booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true));
     configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
     configuration.setUseGeneratedKeys(
         booleanValueOf(props.getProperty("useGeneratedKeys"), false));
     configuration.setDefaultExecutorType(
         ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
     configuration.setDefaultStatementTimeout(
         integerValueOf(props.getProperty("defaultStatementTimeout"), null));
     configuration.setMapUnderscoreToCamelCase(
         booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
     configuration.setSafeRowBoundsEnabled(
         booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
     configuration.setLocalCacheScope(
         LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));
     configuration.setJdbcTypeForNull(
         JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));
     configuration.setLazyLoadTriggerMethods(
         stringSetValueOf(
             props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
     configuration.setSafeResultHandlerEnabled(
         booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
     configuration.setDefaultScriptingLanguage(
         resolveClass(props.getProperty("defaultScriptingLanguage")));
     configuration.setCallSettersOnNulls(
         booleanValueOf(props.getProperty("callSettersOnNulls"), false));
     configuration.setLogPrefix(props.getProperty("logPrefix"));
     configuration.setLogImpl(resolveClass(props.getProperty("logImpl")));
     configuration.setConfigurationFactory(
         resolveClass(props.getProperty("configurationFactory")));
   }
 }
 private ParameterMapping oldParseMapping(
     String token, Class parameterClass, TypeHandlerRegistry typeHandlerRegistry) {
   if (token.indexOf(PARAM_DELIM) > -1) {
     StringTokenizer paramParser = new StringTokenizer(token, PARAM_DELIM, true);
     int n1 = paramParser.countTokens();
     if (n1 == 3) {
       String name = paramParser.nextToken();
       paramParser.nextToken(); // ignore ":"
       String type = paramParser.nextToken();
       TypeHandler handler;
       if (parameterClass == null) {
         handler = typeHandlerRegistry.getUnknownTypeHandler();
       } else {
         handler = resolveTypeHandler(parameterClass, name, null, JdbcType.valueOf(type));
       }
       ParameterMapping.Builder mapping =
           new ParameterMapping.Builder(configuration, name, handler);
       mapping.jdbcType(JdbcType.valueOf(type));
       return mapping.build();
     } else if (n1 >= 5) {
       throw new UnsupportedOperationException(
           "iBATIS 3 does not support null value substitution.");
     } else {
       throw new SqlMapException("Incorrect inline parameter map format: " + token);
     }
   } else {
     TypeHandler handler;
     if (parameterClass == null) {
       handler = typeHandlerRegistry.getUnknownTypeHandler();
     } else {
       handler = resolveTypeHandler(parameterClass, token, null, null);
     }
     ParameterMapping.Builder mapping =
         new ParameterMapping.Builder(configuration, token, handler);
     return mapping.build();
   }
 }
  private ParameterMapping newParseMapping(String token, Class parameterClass) {

    // #propertyName,javaType=string,jdbcType=VARCHAR,mode=IN,nullValue=N/A,handler=string,numericScale=2#

    StringTokenizer paramParser = new StringTokenizer(token, "=, ", false);
    String propertyName = paramParser.nextToken();
    TypeHandler typeHandler = null;
    Class javaType = null;
    JdbcType jdbcType = null;
    ParameterMode parameterMode = null;
    Integer numericScale = null;

    while (paramParser.hasMoreTokens()) {
      String field = paramParser.nextToken();
      if (paramParser.hasMoreTokens()) {
        String value = paramParser.nextToken();
        if ("javaType".equals(field)) {
          try {
            javaType = typeAliasRegistry.resolveAlias(value);
          } catch (Exception e) {
            throw new SqlMapException("Error loading javaType class");
          }
        } else if ("jdbcType".equals(field)) {
          jdbcType = JdbcType.valueOf(value);
        } else if ("mode".equals(field)) {
          parameterMode = ParameterMode.valueOf(value);
        } else if ("nullValue".equals(field)) {
          throw new UnsupportedOperationException(
              "iBATIS 3 does not support null value substitution.");
        } else if ("handler".equals(field)) {
          try {
            Object impl = typeAliasRegistry.resolveAlias(value).newInstance();
            typeHandler = ((TypeHandler) impl);
          } catch (Exception e) {
            throw new SqlMapException(
                "Error loading class specified by handler field in " + token + ".  Cause: " + e, e);
          }
        } else if ("numericScale".equals(field)) {
          try {
            numericScale = Integer.valueOf(value);
            if (numericScale < 0) {
              throw new SqlMapException(
                  "Value specified for numericScale must be greater than or equal to zero");
            }
          } catch (NumberFormatException e) {
            throw new SqlMapException("Value specified for numericScale is not a valid Integer");
          }
        } else {
          throw new SqlMapException(
              "Unrecognized parameter mapping field: '" + field + "' in " + token);
        }
      } else {
        throw new SqlMapException(
            "Incorrect inline parameter map format (missmatched name=value pairs): " + token);
      }
    }

    if (typeHandler == null) {
      if (parameterClass == null) {
        typeHandler = typeHandlerRegistry.getUnknownTypeHandler();
      } else {
        String javaTypeString = javaType == null ? null : javaType.getName();
        typeHandler = resolveTypeHandler(parameterClass, propertyName, javaTypeString, jdbcType);
      }
    }

    if (propertyName != null && propertyName.startsWith("[")) {
      propertyName = "_collection" + propertyName;
    }

    ParameterMapping.Builder mapping =
        new ParameterMapping.Builder(configuration, propertyName, typeHandler);
    mapping.javaType(javaType);
    mapping.jdbcType(jdbcType);
    mapping.mode(parameterMode);
    mapping.numericScale(numericScale);

    return mapping.build();
  }