After parsing the global configuration file, the next step is to parse the Mapper file, which is parsed through XMLMapperBuilder
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 failures<resultMap>
tags
Handling parsing failures<cache-ref>
tags
Handling SQL statements that fail to parse
Focus on the configurationElement() method of the XMLMapperBuilder class
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 <cache-ref>
tag, this tag is used to reference other Cache caches
Parsing<cache>
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
Parsing<parameterMap>
tag, this tag has been abandoned, generally use parameterType
to define the class name of the parameter
Analysis<resultMap>
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 <constructor>, <id>, <result>, <collection>
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
Parsing<select>, <insert>, <update>, <delete>
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 <include>
tag, and then parse <selectKey>
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
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!