Class XMLMapperBuilder

The categories involved include:
MapperBuilderAssistant:MapperThe file parsing auxiliary class includes methods for parsing various nodes.
BaseBuilder:XMLMapperBuilderWith the parent class of the MapperBuilder Assistant, three properties are saved: Configuration, TypeAlias Registry, and TypeHandlerRegistry.
TypeAliasRegistry:Alias correspond to each other, preserving information about strings and classes, such as String.
TypeHandlerRegistry:Correspondence between registered java.lang and java.sql type
Configuration:MapperAfter parsing the node, it stores the parsed attribute information.
XMLStatementBuilder:Parsing the SQL statement node class and parsing the mappedStatements attribute stored in Configuration
 
 
XMLMapperBuilderUsed to parse Mapper files, including namespace, parameterMap, resultMap, and various SQL (insert / uppdate / delete / select)
The parse () method is mainly used in the parsing process.
 
After parsing the Mapper file, it is stored in an instance of the Mapper Builder Assistant and its parent class, BaseBuilder, primarily the Configuration class
private final MapperBuilderAssistant builderAssistant;
 
Construction method//The first parameter is the InputStream input stream of Mapper.xml.//The second parameter is configuration information.//The third parameter is Mapper.xml resource location information.//The fourth parameter is the XML fragment parsed from Mapper.xml.
  public XMLMapperBuilder(InputStream inputStream, Configuration configuration, String resource, Map<String, XNode> sqlFragments) {
    this(new XPathParser(inputStream, true, configuration.getVariables(), new XMLMapperEntityResolver()),
        configuration, resource, sqlFragments);
  }

 

Analytical method:public void parse() {
//Determine whether the Mapper file has been loaded.
    if (!configuration.isResourceLoaded(resource)) {
//Parsing Mapper file
      configurationElement(parser.evalNode("/mapper"));
      configuration.addLoadedResource(resource);
      bindMapperForNamespace();
    }

    parsePendingResultMaps();
    parsePendingCacheRefs();
    parsePendingStatements();
  }

 

//Parsing the Mapper file method:
(Most of the content after parsing is stored in MapperBuilderAssistant- "BaseBuilder-.》ConfigurationIn the objectProperties under the Configuration class:)private void configurationElement(XNode context) {
    try {
      String namespace = context.getStringAttribute("namespace");
      if (namespace == null || namespace.equals("")) {
        throw new BuilderException("Mapper's namespace cannot be empty");
      }
      builderAssistant.setCurrentNamespace(namespace);
//Parsing cache-ref nodes
      cacheRefElement(context.evalNode("cache-ref"));
//Parsing cache nodes
      cacheElement(context.evalNode("cache"));
//Parsing parameterMap nodes
      parameterMapElement(context.evalNodes("/mapper/parameterMap"));
//Parsing resultMap nodes
      resultMapElements(context.evalNodes("/mapper/resultMap"));
//Parsing SQL nodes
      sqlElement(context.evalNodes("/mapper/sql"));
//Parsing select|insert|update|delete nodes
      buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
    } catch (Exception e) {
      throw new BuilderException("Error parsing Mapper XML. The XML location is '" + resource + "'. Cause: " + e, e);
    }
  }

 

Leave a Reply

Your email address will not be published. Required fields are marked *