select|insert|update|delete tag parsing process in Mybatis source code - XXXmapper.xml
Prerequisite: Last time I talked about an article entitled uuuuuuuuuuu Resolution Map Label Resolution Process in Mybatis Source-XXXmapper.xml Now let's talk about how Mybatis parses the select|insert|update|delete tag in XXXmapper.xml file. Because these tags are in the same way, I will take the update tag as an example to introduce the process of parsing the update tag.
Here is the updateByPrimaryKeySelective method that needs to be parsed:
1. First enter the select|insert|update|delete parse entry: XML MapperBuilder# configurationElement.
2. The XML Statement Builder parseStatement Node is responsible for parsing the selected | insert | update | delete node before the single. It mainly takes the node attribute to parse the sub-node attribute of the node in XML Language Driver createSqlSource, parses the SqlSource object and registers SqlSource in the manager's house.
public void parseStatementNode() { // Get the id attribute of the current update tag: updateByPrimary Key Selective String id = context.getStringAttribute("id"); // Data source database Id configured on the label String databaseId = context.getStringAttribute("databaseId"); // The requiredDatabaseId is the default data source id. Only when the two IDs are equal can the tag be correctly parsed. if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) { return; } // The parameterType attribute configured on the tag: cn.edu.his.pay.model.entity.Admin String parameterType = context.getStringAttribute("parameterType"); // Reflections take this class of reflective Clas objects Class<?> parameterTypeClass = resolveClass(parameterType); String resultType = context.getStringAttribute("resultType"); // Getting nodeName is the corresponding label, such as update, insert, etc. sqlCommandType is very important, in fact, it is to determine which kind of operation. String nodeName = context.getNode().getNodeName(); SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH)); // Obviously now it's time to parse the update tag instead of Select, false boolean isSelect = sqlCommandType == SqlCommandType.SELECT; // *** Focus - Here's a further look SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass); String resultSets = context.getStringAttribute("resultSets"); String keyProperty = context.getStringAttribute("keyProperty"); String keyColumn = context.getStringAttribute("keyColumn"); // In the insert statement, we can define a < selectKey > tag under the Insert tag to generate the primary key id, as well as generate it ourselves. KeyGenerator keyGenerator; String keyStatementId = id + SelectKeyGenerator.SELECT_KEY_SUFFIX; keyStatementId = builderAssistant.applyCurrentNamespace(keyStatementId, true); if (configuration.hasKeyGenerator(keyStatementId)) { keyGenerator = configuration.getKeyGenerator(keyStatementId); } else { keyGenerator = context.getBooleanAttribute("useGeneratedKeys", configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType)) ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE; } // Encapsulate the parsed SqlSource into the butler's home builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass, resultSetTypeEnum, flushCache, useCache, resultOrdered, keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets); }
3. The importance of SqlSource is introduced in the code above. Now how SqlSource comes into being is more in-depth. At this time, we come to the method of XMLScript Builder parseScriptNode, which is the focus of parsing the current update tag. It can be seen that the following context is the node of the type of update that needs to be parsed at present.
Then get the information of the update node and call XMLScriptBuilder parseDynamicTags to parse the node. When parsing, the main task is to determine the attribute type under the current node, which is TEXT_NODE (text node), which is ELEMENT_NODE (element node). When a text node is a text node, add a StaticTextSqlNode directly to the contents (responsible for encapsulating node information) collection. Otherwise, we continue to parse the element nodes and call the processing class for the element nodes to parse recursively.
4. The effect map of set element node is no longer posted here, which is to get the information of the child node through the loop of the child node, and then parse the corresponding sub-node processor after judging what type of child node is. The parse DynamicTags method is used repeatedly and recursively. The following is the class diagram of Mybatis node processor.
5. After all the parsing, you will get an outermost SqlNode collection contents and wrap the contents into the Mixed SqlNode object, as follows: the whole Mixed SqlNode structure.
As shown in the structure diagram above, for a simple update tag, after parsing the configuration file, it will be encapsulated in three levels, corresponding to the update tag in XXXmapper.xml as follows:
6. Attach the whole class diagram relationship of packaging at last.