An overview,

  • Mybatis encapsulates each tag into a MappedStatement by parsing XML files, and then implements it through dynamic proxies when it is called

Initialization

  • Mybatis is created in builder mode

  • The first thing to know is that every MyBatis application is centered around an instance of SqlSessionFactory, which can be obtained through SqlSessionFactoryBuilder.

SqlSessionFactory and SqlSessionFactorybean

  • OpenSession method is to obtain a SqlSession object, complete the necessary database add, delete, change and check function;

  • GetConfiguration; To configure mapper mapping files, SQL parameters, return value types, cache, and other properties.


public interface SqlSessionFactory {

  SqlSession openSession(a);

  SqlSession openSession(boolean autoCommit);
  SqlSession openSession(Connection connection);
  SqlSession openSession(TransactionIsolationLevel level);

  SqlSession openSession(ExecutorType execType);
  SqlSession openSession(ExecutorType execType, boolean autoCommit);
  SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level);
  SqlSession openSession(ExecutorType execType, Connection connection);

  Configuration getConfiguration(a);

}
Copy the code
  • We use Mybatis with Spring in our daily development, so we leave the instantiation to Spring.

  • Org. Mybatis. Spring. SqlSessionFactoryBean, it implements the InitializingBean interface

  • This means that afterPropertiesSet() is called after the class is instantiated. It only has one method

public void afterPropertiesSet(a) throws Exception { 
    this.sqlSessionFactory = buildSqlSessionFactory(); 
}
Copy the code
  • BuildSqlSessionFactory. It can be seen in two parts:

  • 1. Load various components from the property property of the configuration file and parse the configuration into the Configuration file

  • 2. Load the Mapper file, parse SQL statements, encapsulate them into MappedStatement objects, and configure them in the Configuration file.

(1) MappedStatement object,

  • There are two most important attributes
    • id
      • The ID of the fully qualified class name plus the method name
    • sqlSource
      • The SqlSource object corresponding to the current SQL tag.
  • Store it inConfiguration#mappedStatementsObject, can be directly indexed to the corresponding sqlSource object by ID

2. Dao dynamic proxy

(1) Packet scanning path

@MapperScan({"com.mmzsblog.business.dao"})
Copy the code

(2) Start scanning

  • Concrete in BeanDefinitionRegistryPostProcessor () this class, to call doScan () method, which will be scanned to all Mapper interfaces, source code is not fine,, specific see themselves

(3) Register BeanDefinition

  • Register all scanned Mapper interfaces as BeanDefinition objects, which can then be initialized

(4) Dynamic proxy

  • In the setSqlSessionFactory method, sqlSession retrieves the SqlSessionTemplate instance. SqlSessionTemplate contains sqlSessionFactory and sqlSessionProxy. SqlSessionProxy is actually the proxy object of SqlSession interface, and actually calls the invoke method of proxy class.

Digression: What happens if we proactively implement the DAO interface and then configure mapper? In this case, there will be two dao interface implementation classes in the ioc container, and you need to specify which object to inject

3, perform

  • When we call the Dao interface method, we actually call the invoke method of the proxy object. In this case, what is actually called is the stuff inside the SqlSession
public class DefaultSqlSession implements SqlSession {

	public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
		try {
			MappedStatement ms = configuration.getMappedStatement(statement);
			returnexecutor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER); }}}Copy the code
  • Through the statementFully qualified type + method nameTake the MappedStatement object and execute the specific SQL through the Executor and return it.

Third, summary

  • SqlSource and dynamic label SqlNode
  • MappedStatement object
  • Spring factory beans and dynamic proxies
  • SqlSession and the executor

Reference:

  • Juejin. Cn/post / 684490…