Talk about the analysis of the Mapper.xml mapping file for the initialization of Mybatis

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

Talk about the analysis of the Mapper.xml mapping file for the initialization of Mybatis

After parsing the complete configuration file, the next step is to parse the Mapper file, which is parsed through XMLMapperBuilder:

The parse() method of XMLMapperBuilder:

public void parse() {
    if (!configuration.isResourceLoaded(resource)) {
      configurationElement(parser.evalNode("/mapper"));
      configuration.addLoadedResource(resource);
      bindMapperForNamespace();
    }

    parsePendingResultMaps();
    parsePendingCacheRefs();
    parsePendingStatements();
  }
复制代码
  1. If the current Mapper file is not loaded, call the configurationElement() method to parse the Mapper file
  2. Added to Configuration.loadedResources collection to prevent repeated loading
  3. Get the Mapper interface corresponding to the Mapper file and register it
  4. Handling tags that failed to parse
  5. Handling tags that failed to parse
  6. Handling SQL statements that fail to parse

Focus on the configurationElement() method of the XMLMapperBuilder class:

private void configurationElement(XNode context) {
    try {
      String namespace = context.getStringAttribute("namespace");
      if (namespace == null || namespace.isEmpty()) {
        throw new BuilderException("Mapper's namespace cannot be empty");
      }
      builderAssistant.setCurrentNamespace(namespace);
      cacheRefElement(context.evalNode("cache-ref"));
      cacheElement(context.evalNode("cache"));
      parameterMapElement(context.evalNodes("/mapper/parameterMap"));
      resultMapElements(context.evalNodes("/mapper/resultMap"));
      sqlElement(context.evalNodes("/mapper/sql"));
      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);
    }
  }
复制代码
  1. Parse the namespace attribute of the Mapper file
  2. Parse tag, this tag is used to refer to other Cache caches
  3. Parse tag, this tag is used to enable the secondary cache of Mybatis. The primary cache is enabled by default. In this method, the MapperBuilderAssistant class is parsed to complete the creation of the Cache, which is stored in the collection of Configuration.caches. The key of the collection is namespace, the value is the Cache object
  4. Parse the label, this label has been deprecated, generally use parameterTypeto define the class name of the parameter
  5. Parse the label, this label is the result map, all the sub-labels under the label are parsed and saved in the ResultMap object. Specifically, the type in the resultMap will be parsed first. Labels, including , , , etc. labels, these labels generate ResultMapping objects, then obtain properties such as id extends, build ResultMapResolver objects, create ResultMap objects and save them to the Configuration.resultMaps collection
  6. Parse the sql tag, this tag is used to define repeated sql fragments, parsed and saved in Configuration.sqlFragments
  7. Parse SQL nodes such as select>, , , etc. You must be familiar with these tags, that is, our sql statement for adding, deleting, modifying and checking, which is parsed through XMLStatementBuilder, which will first parse the tags, then parse the tags, and save them to the Configuration.keyGenerators collection Finally, the SqlSource object is created through the LanguageDriver.createSqlSource() method, and the MappedStatement object is constructed. The sqlSource of the MappedStatement records the sql statement, and the sqlCommandType records the type of the SQL statement, which is stored in the Configuration.mappedStatements collection.

Summarize

This article mainly talks about the analysis of Mapper mapping files, including tags such as namespace, cache, resultMap, sql, etc. In the end, these information will be saved in Configuration

Guess you like

Origin juejin.im/post/7079415745335001096