Mybatis entry-framework principle, mybatis entry framework
Before talking about the principle of mybatis, we need to know what mybatis is. Mybatis is a persistent layer framework. Is an incomplete ORM framework. Because it requires programmers to write SQL statements themselves. However, it can also map the input and output of objects and database tables.
Compared with the hibernate framework, mybatis is easier to learn and has a relatively low learning cost. This allows programmers to focus on SQL statements, because SQL statements in mybatis are separately stored in xml files. It is also convenient to modify and optimize SQL statements and is suitable for projects with many changes in requirements. Because programmers in China have different levels of knowledge about hibernate, mybatis is often used as a persistent layer Framework for Internet e-commerce projects. In order to speed up database access.
- Principle of mybatis framework
For the overall mybatis framework, first upload a picture. Then, use this figure to show the principle of the mybatis framework.
We know that the work of the persistence layer is to deal with the database, store our data into the database from the system, or retrieve the content in the database. This is what the persistence layer framework does. In mybatis, mapped statement (underlying encapsulated object) and Executor (Executor) are transparent to users. That is to say, we do not need to know how the input parameters are encapsulated by mapped statement into a parameter that can be used by other SQL statements in the database. You do not need to know how the Executor operates the database through a series of executions within the sqlSession.
What we need to do is to create an sqlSession (Session linked to a database) object and provide some methods for programmers through this sqlSession interface, input the corresponding ing file (this file package contains the SQL statement we want to execute) and corresponding parameters. In mybatis, the content of the input ing file and the input parameters are parsed, and then the SQL statement is sent through the executor. Then, encapsulate the result through the underlying encapsulation object. Return to the caller of the sqlSession method.
Note that sqlSession does not automatically commit transactions by default. Therefore, when we connect to the database, if you need to submit transactions, You need to manually commit them. In addition, when manually submitting SQL statements, we need to establish a connection with the database. So the question is, how does our sqlSession come from? When does it establish a link with the database?
In our mybatis, there is a session factory, which is a globally only one factory. It is used to create a sqlsession. The data source object connected to the database is created through the sqlSessionFactory and assigned to the sqlSession session object. Therefore, sqlSessionFactory is generally configured as a singleton. All our configurations of mybatais are not read by sqlSessionFactory. In mybatis, SqlSessionFactoryBuilder reads the configurations, it reads a lot of configuration information. After reading the running environment information of mybatis, it creates a SqlSessionFactory. Because these read environments are not the core of mybatis, they are not shown in the figure.
So where are these configuration files written? We generally write it in the SqlMapConfig. xml file. (It is the global configuration file of mybatis with an unfixed name) and the mybatis runtime environment, such as data sources and transactions, is configured.
So how does mybatis execute during running?
Mybatis Execution Process
First, we configure the mybatis configuration file, sqlmapConfig. xml (although the name is not fixed, it is a convention ).
Then, load the runtime environment of mybatis through the configuration file, and create the session factory of SqlSessionFactory (which can be created by Singleton, multiple examples are also supported, but resources are wasted heavily ).
Next, create a session through the SqlSessionFactory ). This interface is thread unsafe, so it is recommended to apply it in the method body.
The rest is that the sqlSession method is called to operate the database. After the operation is complete, perform the commit () method of the transaction. Then release the resource and close the sqlSession.