예제 #1
0
 private void mapperElement(XNode parent) throws Exception {
   if (parent != null) {
     for (XNode child : parent.getChildren()) {
       if ("package".equals(child.getName())) {
         String mapperPackage = child.getStringAttribute("name");
         configuration.addMappers(mapperPackage);
       } else {
         String resource = child.getStringAttribute("resource");
         String url = child.getStringAttribute("url");
         String mapperClass = child.getStringAttribute("class");
         if (resource != null && url == null && mapperClass == null) {
           ErrorContext.instance().resource(resource);
           InputStream inputStream = Resources.getResourceAsStream(resource);
           XMLMapperBuilder mapperParser =
               new XMLMapperBuilder(
                   inputStream, configuration, resource, configuration.getSqlFragments());
           mapperParser.parse();
         } else if (resource == null && url != null && mapperClass == null) {
           ErrorContext.instance().resource(url);
           InputStream inputStream = Resources.getUrlAsStream(url);
           XMLMapperBuilder mapperParser =
               new XMLMapperBuilder(
                   inputStream, configuration, url, configuration.getSqlFragments());
           mapperParser.parse();
         } else if (resource == null && url == null && mapperClass != null) {
           Class<?> mapperInterface = Resources.classForName(mapperClass);
           configuration.addMapper(mapperInterface);
         } else {
           throw new BuilderException(
               "A mapper element may only specify a url, resource or class, but not more than one.");
         }
       }
     }
   }
 }
예제 #2
0
 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);
         }
       }
     }
   }
 }
예제 #3
0
 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);
     }
   }
 }
예제 #4
0
 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));
   }
 }
예제 #5
0
 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());
       }
     }
   }
 }
예제 #6
0
 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;
   }
 }
예제 #7
0
 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);
 }
예제 #8
0
 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);
         }
       }
     }
   }
 }