Analysis of Mybatis's SQL execution process source code

Analysis of Mybatis's SQL execution process source code

Overview

The source code can be used to execute, generate code that can be recognized by the machine, and its functions can be referenced through the open source source code.

importance

1. The SQL execution in mybatis does not matter what the returned result is. You also need to know how the result came from and what processing has been done. Only when you know this principle, you can better understand the problem when you encounter a problem. In that link.
2. It can better extend the application, can reuse the code, reduce the development cost and time.
3. Learning the design ideas can be applied in other applications.

step

To read the source code, you can start with test cases. For each source code, there are many test cases written by masters. We can use the test cases written by masters to test the pre-close function, so that we can know the operating principle and some design ideas. .

Step 1 Test case request

Analysis of Mybatis's SQL execution process source code

Step 2 Perform the MapperProxy object. The MapperProxy proxy is a jdk dynamic proxy. According to the incoming mapper interface, a proxy object is dynamically generated. The proxy object also implements the InvocationHandler interface.

Analysis of Mybatis's SQL execution process source code

Analysis of Mybatis's SQL execution process source code

Analysis of Mybatis's SQL execution process source code

Step 3: Go to the MapperMethod class, initialize the SqlCommand static internal class through the constructor, and compare it with the method signature of the configuration file. Is there a corresponding method?

Analysis of Mybatis's SQL execution process source code

Step 4 execute execute select the executed Sqlsession

Analysis of Mybatis's SQL execution process source code
Analysis of Mybatis's SQL execution process source code

Step 5 Enter DefaultSqlsession, select the executor to execute, first select the cache executor, the cache does not select the default simple executor, if mybatis has a configuration, select the configured executor and set the preprocessing parameters.

Analysis of Mybatis's SQL execution process source code
Analysis of Mybatis's SQL execution process source code

Step 6 Enter the declaration processing object for query and return the result set

Analysis of Mybatis's SQL execution process source code

The request of the process can be summarized as: test case request-"MapperPrxoy(invoker)-"MapperMthod(execute)-"Sqlsession(executor.query())-"SimpleExecutor(MappedStatement.doQuery())-"StatementHandler(PreparedStatementHandler)-" ResultsetHandler->return result set

-"End

to sum up

1. Mybatis uses the dynamic proxy of jdk, generates a specific proxy object when the program is executed, and executes related logic. Some logic can be executed before and after the proxy method, and the class method can be enhanced without changing the logic of the original class. Function, dynamic proxy is especially important when writing low-level code;
2. It is widely used such as the management of things, the principle of spring aop, the interception of login permissions, the unified output of logs, and the time-consuming statistics of each api request.
3. Studying the source code is not for research purposes. It is necessary to learn the ideas in it, such as dynamic agent, factory mode, builder mode, and use these principles to increase the maintainability, reusability, flexibility, and reliability of the program.

Guess you like

Origin blog.51cto.com/xxdeelon/2539818
Recommended