This is the 20th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Mybatis initializes source code parsing based on XML configuration

Mybatis initialization source code parsing

Mybatis through Resource. GetResourceAsStream () method loads MybatisXML configuration file into a byte input stream, and then through the SqlSessionFactoryBuilder is () method in the build () method of parsing the configuration file.

Mybatis initialization when all Mybatis configuration information will be loaded into memory, using org. Apache. Ibatis. Session. The configuation instances to maintain

  • The Configuation object is solved first

The structure of the Configuration object is almost identical to that of an XML Configuration file. Review what configuration tags are in XML:

- Properties - Settings - typeAliases - typeHandlers - objectFactory - mappersCopy the code

Configuration also has corresponding object attributes to encapsulate them. In other words, the essence of initializing Configuration file information is to create a Configuration object and encapsulate parsed XML data into internal Configuration attributes.

ParseConfiguration pass in the top-level tag of Mybatis configuration file for parsing

methods role
propertiesElement(root.evalNode(“properties”)) parsing<properties/>The label
Propertiessettings=settingsAsProperties(root.evalNode(“settings”)); parsing< Settings / >The label
loadCustomVfs(settings) Load the custom VFS implementation classes
typeAliasesElement(root.evalNode(“typeAliases”)); parsing<plugins />The label
objectFactoryElement(root.evalNode(“objectFactory”)) parsing<objectFactory />The label
objectWrapperFactoryElement(root.evalNode(“objectWrapperFactory”)) parsing<objectWrapperFactory />The label
reflectorFactoryElement(root.evalNode(“reflectorFactory”)) parsing<reflectorFactory />The label
settingsElement(settings) The assignment<settings />To the Configuration properties
environmentsElement(root.evalNode(“environments”)) parsing< environments / >The label
databaseldProviderElement(root.evalNode(“databaseldProvider”)) parsing<databaseIdProvider />The label
typeHandlerElement(root.evalNode(“typeHandlers”)) parsing<typeHandlers />The label
mapperElement(root.evalNode(“mappers”)) parsing<mappers />The label

Obtain all parsed information from the XML configuration file by ParseConfiguration.

  • MappedStatemen

MappedStatement and Mapper in the configuration file is a select corresponding/update/insert/delete node. The labels configured in mapper are encapsulated in this object, which is mainly used to describe an SQL statement.

Initialization process

In reviewing the loading of the configuration file described earlier, the various tags in mybatis-config.xm l are parsed, where the Mappers tag is used to import the mapper.xml file or the directory where the Mapper interface is configured.

select * from user where id=#{id}</select>
Copy the code

Such a SELECT tag is parsed into an MappedStatement object during Configuration file initialization and stored in the mappedStatements property of the Configuration object. MappedStatements is a HashMap. Key = fully qualified class name + method name, value = corresponding MappedStatement object.

In configuration, the corresponding property is

Map<String, MappedStatement> mappedStatements =new StrictMap<MappedStatement>("Mapped Statements collection")
Copy the code
  • Processing in XMLConfigBuilder
private void parseConfiguration(XNode root) { try {
// omit processing of other tags
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration.
Cause:"+ e, e); }}Copy the code

The parsing of the XML configuration file is now complete, returning to the overloaded build method called in Step 2

// 5. Overloaded methods called
public SqlSessionFactory build(Configuration config) {
// Create the DefaultSqlSessionFactory object and pass in the Configuration object.
return new DefaultSqlSessionFactory(config);
Copy the code

To this Mybatis based on XMl configuration initialization source code analysis completed, Mybatis initialization is the XMl file parsing into a piece of elements. The specific elements are then passed to the build method for SQL statement execution.