Mybatis How to understand
Using the introduction of the official website ：
MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records.
Generally speaking, it can be divided into three parts
- It's a custom support SQL, A persistence framework for stored procedures and advanced mappings ;
- Almost everything has been eliminated JDBC Code and manual setting of parameters and retrieval results .
- You can use simple XML or Annotations To configure and map primitives ,Map Interface and Java POJO（Plain Old Java Objects） To database records
This is a Mybatis The most classic architecture diagram , How to understand this picture ？
- The first thing you see is red Configuration object , The object is to mybatis The configuration file ,mapper file , as well as java Code annotations are packaged as a whole .
- And then it's going to be with mapper Interface for association ;
- Then when mapper Interface receives call request , Will receive incoming SQL Of ID And incoming objects （ The three on the left “ wheel ”）,mybatis Will be based on sql Of id Find the corresponding MapperStatement（ One sql It's just one. statement） And analyze , The final analysis is to be executed sql. adopt mapper Statements To operate
- And then you'll get sql And the parameters are taken to the database for execution , Get the final result , Then the result is configured according to the mapping , Convert to three on the right “ wheel ”, Output .
Specific implementation process
- First of all, we need Mybatis Global configuration file for sqlMapConfig.xml And mapping files Mapper.xml, These two kinds of files will be read into the frame diagram when the program is initialized Configuration In the object , Carry out package processing .
- And then it will pass Configuration Object to create SqlSessionFactory Call back to the factory ;
- Then passed SqlSessionFactory Let's talk about the establishment of the factory sqlSession.sqlSession It's a programmer oriented interface , Programmers can sqlSession Operating the database .
- And then call sqlSession How to operate the database , If you need to commit a transaction , You need to perform sqlSession Of commit() Method ;
Yes sqlSession The understanding of the
The execution process above , It can be abstracted into this picture , This picture seems to be very brief . We can see , With sqlSession As a boundary , It's divided into two parts ：
- from Configuration To SqlSession It can be used as an operation manual or configuration , Controls the mybatis The initialization , as well as sqlSession The direction of ;
- from SqlSession To DB It can be an operation DB part , This part is how to operate the database , And right jdbc Package processing .
- therefore ,SqlSession It can be understood as a connecting link between the preceding and the following , How to create sqlSession And guide SqlSession How to operate , Yes SqlSession The specific operation of .
sqlSession A link between the preceding and the following , We can see through the code ：
sqlSession Class is injected with two objects , One is configuration, One is executor. Why inject these two objects ？ because configuration Contains all configurations , Can guide sqlSession Implementation process of , and executor Can replace sqlSession To execute sql.
So the last picture can be abstracted like this ：
How to have this picture to understand mybatis How about the operation process of ？ Only three problems need to be solved ：
- sqlSession How did it come about ？ as well as xml How does it correspond to configuration Of ？sqlSession How to get
- mapper Interface and maper.xml How is it mapped ？mapper Interface and mapper How files map
- mapper How the interface operates the database ？mapper How the interface operates the database
about mybatis The understanding of the , Finally, we can condense it into a picture like this . When reading a Book , We often say , A book is thin from thick , From thin to thick , It's really understanding . The same is true of learning frameworks , From a complete framework to a simple one uml chart , And then from a simple uml The diagram extends to the complete framework , So we really have the framework .
本文为[Wooden pine cat]所创，转载请带上原文链接，感谢