Home  >  Article  >  Java  >  How to configure the Mapper.xml mapping file for initialization of Java Mybatis

How to configure the Mapper.xml mapping file for initialization of Java Mybatis

王林
王林forward
2023-05-03 23:10:091381browse
Foreword:

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

Parse Mapper File entry

XMLMapperBuilder’s parse() method:

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

    parsePendingResultMaps();
    parsePendingCacheRefs();
    parsePendingStatements();
  }
  • If the current Mapper file has not been loaded, call the configurationElement() method to parse the Mapper file

  • Add to the Configuration.loadedResources collection to prevent repeated loading

  • Get the Mapper interface corresponding to the Mapper file and register it

  • Handling parsing failures68a9ca67c267b134c127fbeac6659d23tags

  • Handling parsing failuresc9f60baecdceda902422ce5608e73ae9tags

  • Handling SQL statements that fail to parse

Focus on the configurationElement() method of the XMLMapperBuilder class

Parsing Mapper files

The configurationElement() method of the MLMapperBuilder 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);
    }
  }
  • Parse the namespace attribute of the Mapper file

  • Parse c9f60baecdceda902422ce5608e73ae9 tag, this tag is used to reference other Cache caches

  • Parsing62aecd17e676a41d3547c3bf97bb07b0 tag, this The tag is used to enable the second-level cache of Mybatis. The first-level cache is enabled by default. In this method, the MapperBuilderAssistant class is parsed to complete the creation of the Cache, which is saved in the Configuration.caches collection. The key of the collection is namespace and the value is Cache object

  • Parsing4fb23b7f5df2ee11743b998ba01c37fd tag, this tag has been abandoned, generally use parameterType to define the class name of the parameter

  • Analysis68a9ca67c267b134c127fbeac6659d23 Tag, this tag is the result mapping, all sub-tags under its tag are parsed and stored in the ResultMap object. Specifically, the resultMap will be obtained first after parsing. The type in, type is the java object mapped to the result set, and then parses the sub-tags of the resultMap tag, including 3edd161182fff2dd0b855ad5d33f9b63, 53384f78b45ee9f1e3082cf378b9c5b4, 350c21220441cc2f18e31a24358fe33a, 8a69e10c947129bb1dfe1f677169415f and other tags , these tags generate ResultMapping objects, then get attributes such as id extends, build a ResultMapResolver object, create a ResultMap object and save it in the Configuration.resultMaps collection

  • Parse sql tags, this tag is used to define Repeated sql fragments are parsed and stored in Configuration.sqlFragments

  • Parsing221f08282418e2996498697df914ce4e, 3cdbca7b6e47052f0af62aa0d4c6123a, 48dd0c1f550330068948da43aff71ce0, 5cc62b85a20462d19109e58cc4ad0bf9 and other SQL nodes, these tags must be familiar to everyone. They are our add, delete, modify and query sql statements, which are parsed through XMLStatementBuilder. It will first parse the dcf91641426a34cf32ecc36140f28baf tag, and then parse d81480a879ba657d83c408e5fa1b948b tag is saved to the Configuration.keyGenerators collection. Finally, the SqlSource object is created through the LanguageDriver.createSqlSource() method to build the MappedStatement object. The sqlSource of the MappedStatement records the sql statement, and sqlCommandType records the type of the SQL statement, which is saved in

in Configuration.mappedStatements collection

The above is the detailed content of How to configure the Mapper.xml mapping file for initialization of Java Mybatis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete