preface

Any framework requires a series of initializations before use, and MyBatis is no exception. This chapter introduces the initialization process of MyBatis in detail through the following points.

  1. What does initialization of MyBatis do
  2. MyBatis creates a Configuration object based on an XML Configuration file
  3. Manually load the XML Configuration file and create the Configuration object to complete the initialization. Create and use the SqlSessionFactory object
  4. Design patterns involved

What does MyBatis initialization do

The initialization of any framework is nothing more than loading the configuration information needed for its own runtime. The configuration information of MyBatis probably contains the following information, and its high-level structure is as follows:

X configuration configuration

* the properties attribute

X Settings set

× typeAliases Type name

TypeHandlers type handler

× objectFactory indicates the objectFactory

* plugins plugin

X environments environment

× environment Environment variables

× transactionManager transactionManager

* dataSource

X mapper

The above configuration information of MyBatis will be configured in the XML configuration file, so, these information is loaded into MyBatis internal, how to maintain MyBatis?

MyBatis took a very straightforward and simple way – using org. Apache. Ibatis. Session. The Configuration object as a container all Configuration information, The Configuration object is organized almost exactly the same as an XML Configuration file (of course, the Configuration object is not limited to that, it also creates objects for internal use in MyBatis, such as Executors, which will be discussed in a future article). As shown below:


MyBatis according to the initialization of the Configuration information, this time users can use MyBatis database operation.

It can be said that the process of initialization of MyBatis is the process of creating a Configuration object.

MyBatis can be initialized in two ways:

  • Xml-based Configuration file: All the Configuration information of MyBatis is stored in an XML file. MyBatis assembles the Configuration information into an internal Configuration object by loading and storing the XML Configuration file
  • Based on Java API: This method does not use XML Configuration files, MyBatis users need to manually create a Configuration object in Java code, and then enter the Configuration parameter set into the Configuration object

(PS: What is the specific configuration information of MyBatis, and what is the meaning respectively, is not the scope of this article)

Next, we will take a closer look at how MyBatis builds a Configuration object from a Configuration file and uses it by initializing MyBatis using an XML Configuration file.

MyBatis creates a Configuration object based on the XML Configuration file

Let’s start with a simple example of how to initialize MyBatis and what it initializes. Look at the following code:


MyBatis use experience of readers will know, the role of the above statement is executed. Com foo bean. BlogMapper. QueryAllBlogInfo defined SQL statements, return a List the result set. In general, the above code goes through three processes: mybatis initialization, SqlSession creation, SQL statement execution and return results.

The function of the above code is to create the SqlSessionFactory object from the mybatis-config. XML configuration file, and then generate the SqlSession, execute the SQL statement. SqlSessionFactory SqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

Now let’s see what happens in the third sentence.

MyBatis initialization process:

The SqlSessionFactoryBuilder generates a Configuration object from the incoming data stream, and then creates a default SqlSessionFactory instance from the Configuration object.

The basic initialization process is shown in the sequence diagram below:


As shown in the figure above, myBatis initialization goes through the following simple steps:

1. Call the build(inputStream) method of SqlSessionFactoryBuilder;

2. SqlSessionFactoryBuilder creates an XMLConfigBuilder object based on inputStream inputStream, etc.

3. SqlSessionFactoryBuilder calls the parse() method of the XMLConfigBuilder object;

4. The XMLConfigBuilder object returns the Configuration object.

5. SqlSessionFactoryBuilder creates a DefaultSessionFactory object from the Configuration object.

6. SqlSessionFactoryBuilder returns the DefaultSessionFactory object to the Client for use by the Client.

The code for SqlSessionFactoryBuilder looks like this:



In the initialization process above, the following objects are involved:

  • SqlSessionFactoryBuilder: constructor of SqlSessionFactory, used to create SqlSessionFactory, using the Builder design pattern
  • Configuration: This object is all the MyBatis Configuration information in the mybatis-config. XML file
  • SqlSessionFactory: SqlSessionFactory class that creates SqlSession objects in the form of factories, using the Factory design pattern
  • XmlConfigParser: Parses the mybatis-config. XML Configuration file into a Configuration object that is used by SqlSessonFactoryBuilder to create the SqlSessionFactory

The process of creating a Configuration object

Following the basic MyBatis initialization discussion above, when SqlSessionFactoryBuilder executes the build() method, the parse() method of XMLConfigBuilder is called, and the Configuration object is returned. So how does the parse() method process the XML file to generate a Configuration object?

1. XMLConfigBuilder converts the XML configuration file information into a Document object, and the XML configuration definition file DTD into an XMLMapperEntityResolver object, and then encapsulates both into an XpathParser object. The purpose of XpathParser is to provide operations to get basic DOM Node information based on Xpath expressions. As shown below:



2. XMLConfigBuilder then calls the parse() method: It takes the Node object corresponding to the < Configuration > Node from XPathParser and parses the child nodes of this Node:

properties, settings, typeAliases,typeHandlers, objectFactory, objectWrapperFactory, plugins, environments,databaseIdProvider, mappers



Note: Another very important point in the above code is the method mapperElements(root.evalnode (“mappers”) to parse the XML configuration file child <mappers>, which parses our configured mapper.xml configuration file, Mapper configuration file can be said to be the core of MyBatis, MyBatis features and concepts are reflected in the configuration and design of this Mapper, we will discuss it in the subsequent article, please look forward to ~

3. Resolve the values and set them in the Configuration object.

EnvironmentsElement (root.evalNode(“environments”)); environmentsElement(root.evalNode(“environments”)); environmentsElement(root.evalNode(“environments”)); environmentsElement(root.evalNode(“environments”)); To do this, you can parse the information for Environments and set it to a Configuration object:



4. Return to the Configuration object

We refined the sequence diagram of the above basic process of MyBatis initialization.



Create an SqlSessionFactory object after initialization

We can use XMLConfigBuilder to manually parse the XML Configuration file to create a Configuration object as follows:


Four, the design mode involved

The initialization process involves creating various objects, so some creative design patterns are used. In the process of initialization, Builder mode is used more.

Application of Builder mode 1: creation of SqlSessionFactory

When creating a SqlSessionFactory, different parameters are provided depending on the situation. The parameter combinations can be as follows:




Since the construction parameters are variable, we can create a constructor for it to separate the construction process from the representation of SqlSessionFactory:


MyBatis separates SqlSessionFactoryBuilder and SqlSessionFactory from each other.

Builder mode application 2: database connection Environment object creation

When XMLConfigParser parses the Mybatis XML Configuration file node <environment> during the construction of the Configuration object, there is the following code:


Inside the Environment, the static inner Builder class is defined:




Above is this article “in-depth understanding of mybatis principle” mybatis initialization mechanism detailed explanation of all content, I hope to help you! If the above content is not appropriate, please also point out the reader, common discussion, common progress!