Java Code Examples for org.apache.ibatis.parsing.XNode#getChildren()

The following examples show how to use org.apache.ibatis.parsing.XNode#getChildren() . 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: XMLMapperBuilder.java    From mybatis with Apache License 2.0 6 votes vote down vote up
private Discriminator processDiscriminatorElement(XNode context, Class<?> resultType, List<ResultMapping> resultMappings) throws Exception {
  String column = context.getStringAttribute("column");
  String javaType = context.getStringAttribute("javaType");
  String jdbcType = context.getStringAttribute("jdbcType");
  String typeHandler = context.getStringAttribute("typeHandler");
  Class<?> javaTypeClass = resolveClass(javaType);
  @SuppressWarnings("unchecked")
  Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(typeHandler);
  JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
  Map<String, String> discriminatorMap = new HashMap<String, String>();
  for (XNode caseChild : context.getChildren()) {
    String value = caseChild.getStringAttribute("value");
    String resultMap = caseChild.getStringAttribute("resultMap", processNestedResultMappings(caseChild, resultMappings));
    discriminatorMap.put(value, resultMap);
  }
  return builderAssistant.buildDiscriminator(resultType, column, javaTypeClass, jdbcTypeEnum, typeHandlerClass, discriminatorMap);
}
 
Example 2
Source File: HierarchicalXMLConfigBuilder.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void environmentsElement(XNode context) throws Exception {
    if (context != null) {
        if (environment == null) {
            environment = context.getStringAttribute("default");
        }
        for (XNode child : context.getChildren()) {
            String id = child.getStringAttribute("id");
            if (isSpecifiedEnvironment(id)) {
                TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
                DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
                DataSource dataSource = dsFactory.getDataSource();
                Environment.Builder environmentBuilder = new Environment.Builder(id)
                        .transactionFactory(txFactory)
                        .dataSource(dataSource);
                configuration.setEnvironment(environmentBuilder.build());
            }
        }
    }
}
 
Example 3
Source File: HierarchicalXMLConfigBuilder.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void typeHandlerElement(XNode parent) throws Exception {
    if (parent != null) {
        for (XNode child : parent.getChildren()) {
            if ("package".equals(child.getName())) {
                String typeHandlerPackage = child.getStringAttribute("name");
                typeHandlerRegistry.register(typeHandlerPackage);
            } else {
                String javaTypeName = child.getStringAttribute("javaType");
                String jdbcTypeName = child.getStringAttribute("jdbcType");
                String handlerTypeName = child.getStringAttribute("handler");
                Class<?> javaTypeClass = resolveClass(javaTypeName);
                JdbcType jdbcType = resolveJdbcType(jdbcTypeName);
                Class<?> typeHandlerClass = resolveClass(handlerTypeName);
                if (javaTypeClass != null) {
                    if (jdbcType == null) {
                        typeHandlerRegistry.register(javaTypeClass, typeHandlerClass);
                    } else {
                        typeHandlerRegistry.register(javaTypeClass, jdbcType, typeHandlerClass);
                    }
                } else {
                    typeHandlerRegistry.register(typeHandlerClass);
                }
            }
        }
    }
}
 
Example 4
Source File: XMLConfigBuilder.java    From mybatis with Apache License 2.0 6 votes vote down vote up
private void environmentsElement(XNode context) throws Exception {
  if (context != null) {
    if (environment == null) {
      environment = context.getStringAttribute("default");
    }
    for (XNode child : context.getChildren()) {
      String id = child.getStringAttribute("id");
//循环比较id是否就是指定的environment
      if (isSpecifiedEnvironment(id)) {
        //7.1事务管理器
        TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
        //7.2数据源
        DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
        DataSource dataSource = dsFactory.getDataSource();
        Environment.Builder environmentBuilder = new Environment.Builder(id)
            .transactionFactory(txFactory)
            .dataSource(dataSource);
        configuration.setEnvironment(environmentBuilder.build());
      }
    }
  }
}
 
Example 5
Source File: XMLMapperBuilder.java    From Shop-for-JavaWeb with MIT License 6 votes vote down vote up
private Discriminator processDiscriminatorElement(XNode context,
		Class<?> resultType, List<ResultMapping> resultMappings)
		throws Exception {
	String column = context.getStringAttribute("column");
	String javaType = context.getStringAttribute("javaType");
	String jdbcType = context.getStringAttribute("jdbcType");
	String typeHandler = context.getStringAttribute("typeHandler");
	Class<?> javaTypeClass = resolveClass(javaType);
	@SuppressWarnings("unchecked")
	Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(typeHandler);
	JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
	Map<String, String> discriminatorMap = new HashMap<String, String>();
	for (XNode caseChild : context.getChildren()) {
		String value = caseChild.getStringAttribute("value");
		String resultMap = caseChild.getStringAttribute("resultMap",
				processNestedResultMappings(caseChild, resultMappings));
		discriminatorMap.put(value, resultMap);
	}
	return builderAssistant
			.buildDiscriminator(resultType, column, javaTypeClass,
					jdbcTypeEnum, typeHandlerClass, discriminatorMap);
}
 
Example 6
Source File: HierarchicalXMLConfigBuilder.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void typeAliasesElement(XNode parent) {
    if (parent != null) {
        for (XNode child : parent.getChildren()) {
            if ("package".equals(child.getName())) {
                String typeAliasPackage = child.getStringAttribute("name");
                configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
            } else {
                String alias = child.getStringAttribute("alias");
                String type = child.getStringAttribute("type");
                try {
                    Class<?> clazz = Resources.classForName(type);
                    if (alias == null) {
                        typeAliasRegistry.registerAlias(clazz);
                    } else {
                        typeAliasRegistry.registerAlias(alias, clazz);
                    }
                } catch (ClassNotFoundException e) {
                    throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
                }
            }
        }
    }
}
 
Example 7
Source File: XMLMapperBuilder.java    From mybaties with Apache License 2.0 6 votes vote down vote up
private Discriminator processDiscriminatorElement(XNode context, Class<?> resultType, List<ResultMapping> resultMappings) throws Exception {
  String column = context.getStringAttribute("column");
  String javaType = context.getStringAttribute("javaType");
  String jdbcType = context.getStringAttribute("jdbcType");
  String typeHandler = context.getStringAttribute("typeHandler");
  Class<?> javaTypeClass = resolveClass(javaType);
  @SuppressWarnings("unchecked")
  Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(typeHandler);
  JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
  Map<String, String> discriminatorMap = new HashMap<String, String>();
  for (XNode caseChild : context.getChildren()) {
    String value = caseChild.getStringAttribute("value");
    String resultMap = caseChild.getStringAttribute("resultMap", processNestedResultMappings(caseChild, resultMappings));
    discriminatorMap.put(value, resultMap);
  }
  return builderAssistant.buildDiscriminator(resultType, column, javaTypeClass, jdbcTypeEnum, typeHandlerClass, discriminatorMap);
}
 
Example 8
Source File: XMLConfigBuilder.java    From QuickProject with Apache License 2.0 6 votes vote down vote up
private void environmentsElement(XNode context) throws Exception {
  if (context != null) {
    if (environment == null) {
      environment = context.getStringAttribute("default");
    }
    for (XNode child : context.getChildren()) {
      String id = child.getStringAttribute("id");
      if (isSpecifiedEnvironment(id)) {
        TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
        DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
        DataSource dataSource = dsFactory.getDataSource();
        Environment.Builder environmentBuilder = new Environment.Builder(id)
            .transactionFactory(txFactory)
            .dataSource(dataSource);
        configuration.setEnvironment(environmentBuilder.build());
      }
    }
  }
}
 
Example 9
Source File: XMLConfigBuilder.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void typeHandlerElement(XNode parent) throws Exception {
  if (parent != null) {
    for (XNode child : parent.getChildren()) {
      //如果是package
      if ("package".equals(child.getName())) {
        String typeHandlerPackage = child.getStringAttribute("name");
        //(一)调用TypeHandlerRegistry.register,去包下找所有类
        typeHandlerRegistry.register(typeHandlerPackage);
      } else {
        //如果是typeHandler
        String javaTypeName = child.getStringAttribute("javaType");
        String jdbcTypeName = child.getStringAttribute("jdbcType");
        String handlerTypeName = child.getStringAttribute("handler");
        Class<?> javaTypeClass = resolveClass(javaTypeName);
        JdbcType jdbcType = resolveJdbcType(jdbcTypeName);
        Class<?> typeHandlerClass = resolveClass(handlerTypeName);
        //(二)调用TypeHandlerRegistry.register(以下是3种不同的参数形式)
        if (javaTypeClass != null) {
          if (jdbcType == null) {
            typeHandlerRegistry.register(javaTypeClass, typeHandlerClass);
          } else {
            typeHandlerRegistry.register(javaTypeClass, jdbcType, typeHandlerClass);
          }
        } else {
          typeHandlerRegistry.register(typeHandlerClass);
        }
      }
    }
  }
}
 
Example 10
Source File: XMLScriptBuilder.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void handleWhenOtherwiseNodes(XNode chooseSqlNode, List<SqlNode> ifSqlNodes, List<SqlNode> defaultSqlNodes) {
  List<XNode> children = chooseSqlNode.getChildren();
  for (XNode child : children) {
    String nodeName = child.getNode().getNodeName();
    NodeHandler handler = nodeHandlers(nodeName);
    if (handler instanceof IfHandler) {
      handler.handleNode(child, ifSqlNodes);
    } else if (handler instanceof OtherwiseHandler) {
      handler.handleNode(child, defaultSqlNodes);
    }
  }
}
 
Example 11
Source File: XMLConfigBuilder.java    From QuickProject with Apache License 2.0 5 votes vote down vote up
private void pluginElement(XNode parent) throws Exception {
  if (parent != null) {
    for (XNode child : parent.getChildren()) {
      String interceptor = child.getStringAttribute("interceptor");
      Properties properties = child.getChildrenAsProperties();
      Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();
      interceptorInstance.setProperties(properties);
      configuration.addInterceptor(interceptorInstance);
    }
  }
}
 
Example 12
Source File: XMLConfigBuilder.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void pluginElement(XNode parent) throws Exception {
  if (parent != null) {
    for (XNode child : parent.getChildren()) {
      String interceptor = child.getStringAttribute("interceptor");
      Properties properties = child.getChildrenAsProperties();
      Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();
      interceptorInstance.setProperties(properties);
      //调用InterceptorChain.addInterceptor
      configuration.addInterceptor(interceptorInstance);
    }
  }
}
 
Example 13
Source File: XMLConfigBuilder.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private void pluginElement(XNode parent) throws Exception {
  if (parent != null) {
    for (XNode child : parent.getChildren()) {
      String interceptor = child.getStringAttribute("interceptor");
      Properties properties = child.getChildrenAsProperties();
      Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();
      interceptorInstance.setProperties(properties);
      //调用InterceptorChain.addInterceptor
      configuration.addInterceptor(interceptorInstance);
    }
  }
}
 
Example 14
Source File: XMLScriptBuilder.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private void handleWhenOtherwiseNodes(XNode chooseSqlNode, List<SqlNode> ifSqlNodes, List<SqlNode> defaultSqlNodes) {
  List<XNode> children = chooseSqlNode.getChildren();
  for (XNode child : children) {
    String nodeName = child.getNode().getNodeName();
    NodeHandler handler = nodeHandlers(nodeName);
    if (handler instanceof IfHandler) {
      handler.handleNode(child, ifSqlNodes);
    } else if (handler instanceof OtherwiseHandler) {
      handler.handleNode(child, defaultSqlNodes);
    }
  }
}
 
Example 15
Source File: XMLMapperBuilder.java    From Shop-for-JavaWeb with MIT License 5 votes vote down vote up
private void processConstructorElement(XNode resultChild,
		Class<?> resultType, List<ResultMapping> resultMappings)
		throws Exception {
	List<XNode> argChildren = resultChild.getChildren();
	for (XNode argChild : argChildren) {
		ArrayList<ResultFlag> flags = new ArrayList<ResultFlag>();
		flags.add(ResultFlag.CONSTRUCTOR);
		if ("idArg".equals(argChild.getName())) {
			flags.add(ResultFlag.ID);
		}
		resultMappings.add(buildResultMappingFromContext(argChild,
				resultType, flags));
	}
}
 
Example 16
Source File: MapperScanApplication.java    From mumu with Apache License 2.0 5 votes vote down vote up
/**
 * 加载别名
 * @param configuration
 */
private void handleTypeAlias(Configuration configuration, XNode root) {
	log.info("load alias message...........................");
	TypeAliasRegistry typeAliasRegistry = configuration.getTypeAliasRegistry();
	XNode parent = root.evalNode("typeAliases");
	if(parent!=null){
		for (XNode child : parent.getChildren()) {
			if ("package".equals(child.getName())) {
				String typeAliasPackage = child.getStringAttribute("name");
				configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
				log.info("package:"+typeAliasPackage);
			} else {
				String alias = child.getStringAttribute("alias");
				String type = child.getStringAttribute("type");
				try {
					Class<?> clazz = Resources.classForName(type);
					if (alias == null) {
						typeAliasRegistry.registerAlias(clazz);
					} else {
						typeAliasRegistry.registerAlias(alias, clazz);
					}
					log.info("alias:"+alias+"   type:"+clazz);
				} catch (ClassNotFoundException e) {
					throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
				}
			}
		}
	}
}
 
Example 17
Source File: HierarchicalXMLConfigBuilder.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void pluginElement(XNode parent) throws Exception {
    if (parent != null) {
        for (XNode child : parent.getChildren()) {
            String interceptor = child.getStringAttribute("interceptor");
            Properties properties = child.getChildrenAsProperties();
            Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();
            interceptorInstance.setProperties(properties);
            configuration.addInterceptor(interceptorInstance);
        }
    }
}
 
Example 18
Source File: XMLMapperBuilder.java    From mybaties with Apache License 2.0 4 votes vote down vote up
private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> additionalResultMappings) throws Exception {
    //错误上下文
//取得标示符   ("resultMap[userResultMap]")
//    <resultMap id="userResultMap" type="User">
//      <id property="id" column="user_id" />
//      <result property="username" column="username"/>
//      <result property="password" column="password"/>
//    </resultMap>
    ErrorContext.instance().activity("processing " + resultMapNode.getValueBasedIdentifier());
    String id = resultMapNode.getStringAttribute("id",
        resultMapNode.getValueBasedIdentifier());
    //一般拿type就可以了,后面3个难道是兼容老的代码?
    String type = resultMapNode.getStringAttribute("type",
        resultMapNode.getStringAttribute("ofType",
            resultMapNode.getStringAttribute("resultType",
                resultMapNode.getStringAttribute("javaType"))));
    //高级功能,还支持继承?
//  <resultMap id="carResult" type="Car" extends="vehicleResult">
//    <result property="doorCount" column="door_count" />
//  </resultMap>
    String extend = resultMapNode.getStringAttribute("extends");
    //autoMapping
    Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping");
    Class<?> typeClass = resolveClass(type);
    Discriminator discriminator = null;
    List<ResultMapping> resultMappings = new ArrayList<ResultMapping>();
    resultMappings.addAll(additionalResultMappings);
    List<XNode> resultChildren = resultMapNode.getChildren();
    for (XNode resultChild : resultChildren) {
      if ("constructor".equals(resultChild.getName())) {
        //解析result map的constructor
        processConstructorElement(resultChild, typeClass, resultMappings);
      } else if ("discriminator".equals(resultChild.getName())) {
        //解析result map的discriminator
        discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings);
      } else {
        List<ResultFlag> flags = new ArrayList<ResultFlag>();
        if ("id".equals(resultChild.getName())) {
          flags.add(ResultFlag.ID);
        }
        //调5.1.1 buildResultMappingFromContext,得到ResultMapping
        resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags));
      }
    }
    //最后再调ResultMapResolver得到ResultMap
    ResultMapResolver resultMapResolver = new ResultMapResolver(builderAssistant, id, typeClass, extend, discriminator, resultMappings, autoMapping);
    try {
      return resultMapResolver.resolve();
    } catch (IncompleteElementException  e) {
      configuration.addIncompleteResultMap(resultMapResolver);
      throw e;
    }
  }
 
Example 19
Source File: XMLMapperBuilder.java    From Shop-for-JavaWeb with MIT License 4 votes vote down vote up
private ResultMap resultMapElement(XNode resultMapNode,
		List<ResultMapping> additionalResultMappings) throws Exception {
	ErrorContext.instance().activity(
			"processing " + resultMapNode.getValueBasedIdentifier());
	String id = resultMapNode.getStringAttribute("id",
			resultMapNode.getValueBasedIdentifier());
	String type = resultMapNode.getStringAttribute("type", resultMapNode
			.getStringAttribute("ofType", resultMapNode.getStringAttribute(
					"resultType",
					resultMapNode.getStringAttribute("javaType"))));
	String extend = resultMapNode.getStringAttribute("extends");
	Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping");
	Class<?> typeClass = resolveClass(type);
	Discriminator discriminator = null;
	List<ResultMapping> resultMappings = new ArrayList<ResultMapping>();
	resultMappings.addAll(additionalResultMappings);
	List<XNode> resultChildren = resultMapNode.getChildren();
	for (XNode resultChild : resultChildren) {
		if ("constructor".equals(resultChild.getName())) {
			processConstructorElement(resultChild, typeClass,
					resultMappings);
		} else if ("discriminator".equals(resultChild.getName())) {
			discriminator = processDiscriminatorElement(resultChild,
					typeClass, resultMappings);
		} else {
			ArrayList<ResultFlag> flags = new ArrayList<ResultFlag>();
			if ("id".equals(resultChild.getName())) {
				flags.add(ResultFlag.ID);
			}
			resultMappings.add(buildResultMappingFromContext(resultChild,
					typeClass, flags));
		}
	}
	ResultMapResolver resultMapResolver = new ResultMapResolver(
			builderAssistant, id, typeClass, extend, discriminator,
			resultMappings, autoMapping);
	try {
		return resultMapResolver.resolve();
	} catch (IncompleteElementException e) {
		configuration.addIncompleteResultMap(resultMapResolver);
		throw e;
	}
}
 
Example 20
Source File: XMLMapperBuilder.java    From mybatis with Apache License 2.0 4 votes vote down vote up
private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> additionalResultMappings) throws Exception {
    //错误上下文
//取得标示符   ("resultMap[userResultMap]")
//    <resultMap id="userResultMap" type="User">
//      <id property="id" column="user_id" />
//      <result property="username" column="username"/>
//      <result property="password" column="password"/>
//    </resultMap>
    ErrorContext.instance().activity("processing " + resultMapNode.getValueBasedIdentifier());
    String id = resultMapNode.getStringAttribute("id",
        resultMapNode.getValueBasedIdentifier());
    //一般拿type就可以了,后面3个难道是兼容老的代码?
    String type = resultMapNode.getStringAttribute("type",
        resultMapNode.getStringAttribute("ofType",
            resultMapNode.getStringAttribute("resultType",
                resultMapNode.getStringAttribute("javaType"))));
    //高级功能,还支持继承?
//  <resultMap id="carResult" type="Car" extends="vehicleResult">
//    <result property="doorCount" column="door_count" />
//  </resultMap>
    String extend = resultMapNode.getStringAttribute("extends");
    //autoMapping
    Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping");
    Class<?> typeClass = resolveClass(type);
    Discriminator discriminator = null;
    List<ResultMapping> resultMappings = new ArrayList<ResultMapping>();
    resultMappings.addAll(additionalResultMappings);
    List<XNode> resultChildren = resultMapNode.getChildren();
    for (XNode resultChild : resultChildren) {
      if ("constructor".equals(resultChild.getName())) {
        //解析result map的constructor
        processConstructorElement(resultChild, typeClass, resultMappings);
      } else if ("discriminator".equals(resultChild.getName())) {
        //解析result map的discriminator
        discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings);
      } else {
        List<ResultFlag> flags = new ArrayList<ResultFlag>();
        if ("id".equals(resultChild.getName())) {
          flags.add(ResultFlag.ID);
        }
        //调5.1.1 buildResultMappingFromContext,得到ResultMapping
        resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags));
      }
    }
    //最后再调ResultMapResolver得到ResultMap
    ResultMapResolver resultMapResolver = new ResultMapResolver(builderAssistant, id, typeClass, extend, discriminator, resultMappings, autoMapping);
    try {
      return resultMapResolver.resolve();
    } catch (IncompleteElementException  e) {
      configuration.addIncompleteResultMap(resultMapResolver);
      throw e;
    }
  }