[Mybatis source code analysis (3)] the process of mybatis sqlSession execution

1. First understand some key interfaces and classes

(1) Four core components of Mybatis

  1. Executor: It is used to create a cache, manage the invocation of StatementHander, and provide a Configuration environment for StatementHandler.
  2. StatementHander: It is mainly used to create statement objects to interact with the database.
  3. ParamenterHandler: It is mainly used for the parameter configuration of the pre-compiled sql of the PreparedStatement object
  4. ResultSetHandler: It is mainly used to bind query results and entity classes

1. Executor interface ( executor is used to execute SQL statements )

There are mainly four implementation classes:

  • SimpleExecutor: Simple executor, all database operations are delegated to StatementHandler for processing
  • ReuseExecutor: Reuse executor, similar to SimpleExecutor, except that the JDBC statement is cached according to SQL. When encountering the same SQL, the creation of the statement is omitted. You need to manually execute flushStatement to clear the cache.
  • BatchExecutor: batch executor, using the batchUpdate method of JDBC, you need to manually execute the flushStatements of the SqlSession to execute SQL immediately.
  • CachingExecutor: Caching Executor

 

2. The StatementHandler interface ( used to process the Statement interface in JDBC )

  • SimpleStatementHandler: Corresponds to the Statement interface in JDBC, used for simple sql processing.
  • PreparedStatementHandler: corresponds to the interface of PreparedStatement precompiled sql in JDBC.
  • CallableStatementHandler: Corresponds to the CallableStatement interface in JDBC, used to execute the interface related to the stored procedure.
  • RoutingStatemengtHandler: The routing of the above three interfaces is only responsible for creating and calling.

 

3. ParameterHandler interface ( parameter processor, which dynamically assigns values ​​to sql statement parameters of PreparedStatement )

Parameter explanation:

  • getParameterObject: used to read parameters
  • setParameters: Used to assign values ​​to the parameters of PreparedStatement

 

4. ResultSetHandler interface ( used to process the result set, encapsulate the corresponding object according to the ResultType and ResultMap in the Mapper file and return it. )

Only DefaultResultSetHandler is an implementation class

  • handleResultSets: Map the result set (multiple result sets) generated after statement execution into a result list
  • handleCursorResultSets: handle cursor result sets
  • handleOutputParameters: handle the output parameters after the execution of the stored procedure

 

(2) Other key interfaces and classes

1. Configuration class: It is the core configuration class of the entire mybatis, all xml and other information will eventually be parsed to the Configuration class

 

2. MappedStatement class: used to store the mapper tag information in the mapper file.

3. ResultMap class: used to store the resultMap tag information in the mapper file

4. ResultMapping class: store the result information in the resultMap tag in the mapper file, used as a column mapping class for java and mysql

5. BoundSql class: used to process dynamic sql, return sql string

6, ResultSetWrapper class: used to wrap the ResultSet result set

7. ResultHandler interface : used for custom processing to return results, implemented using the select method of sqlSessionTemplate, used for streaming queries, to prevent big data from causing oom.


8. ResultContext interface : The only implementation class is DefaultResultContext. It is a parameter of the ResultHandler interface. Each ResultContext represents a piece of data, which can be obtained through getResultObject.

9. TypeHandler interface: type processor. Used to process the conversion between javaType and JdbcType, and also used to set parameter values ​​in PreparedStatement and to retrieve values ​​from ResultSet or CallableStatement.

10. RowBounds class: used for mybatis logical paging, the principle is to take out all the data, discard the previous offset data, and take the limit bar of the remaining data. Performance is not good. It is recommended to use the PageHelper paging plug-in. The bottom layer is to add a limit statement through an interceptor.


Second, the process of SqlSession execution

Whether we use Mapper to execute sql or sqlSessionTemplate, we will eventually execute the selectList method of DefaultSqlSession (list query)

Please refer to: https://blog.csdn.net/sumengnan/article/details/113953507 , here we start directly from the selectList method.

1. The selectList method of DefaultSqlSession

Get the MappedStatement information from the configuration (you can get it, because when the project starts, all the content in the mapper.xml file has been parsed into the configuration)

The type of executor is generated according to the configuration when the project is started, the default is Simple , if caching is enabled, it is cachingExecutor

 

2. The query method is SimpleExecutor's abstract class BaseExecutor method for query

If it exists in the cache, use handleLocallyCachedOutputParameters, otherwise use queryFromDatabase to query data from the database.

3. The queryFromDatabase method of BaseExecutor is used for query

Mainly used to cache some data, and then call the doQuery method

4. This doQuery method is the real method for subclasses to implement

4.1 build statementHandler

You can see that the RoutingStatementHandler is used as a static proxy class.

Then add a plug-in interceptor for custom processing after interception, for example: PageHepler paging plug-in

4.2, start preparing the Statement object

Get Connection database connection from transaction ( isn’t this the first step in JDBC? Get database connection first )

Initialize the statement: create a Statement object from the connection ( isn’t this the second step of JDBC? Create a Statement object )

If the Executor is PreparedExecutor, the prepareStatement object will be obtained, and the handler.parameterize(stmt) method will be executed.

To set the parameters, if it is of type String, the StringTypeHandler class will be executed. Use ps.setString(i, parameter); to set the parameter value ( isn’t this the third step of JDBC? Pre-compiled SQL parameter assignment )

 

5. Go back to the doQuery method in step 4 and continue to execute the handler.query method

Execute statement.execute(sql); to execute sql query. ( Isn't this the fourth step of JDBC? Execute the execute method to execute sql query )

6, continue to enter the handleResultSets method

There may be more than one result set ResultSet

6.1. Enter getFirstResultSet to get the result set ( isn’t this the fifth step of JDBC? Get the ResultSet result set )

Then wrap the result into the ResultSetWrapper class

7. Enter the HandleResultSet method to process the result set

If you do not have a custom ResultHandler to process the result set class, then the DefaultResultHandler is used by default to process the result set

8. Enter the HandlerRowValues ​​method to process each row of data

handleRowValuesForSimpleResultMap is a simple result map

handleRowValuesForNestedResultMap is a nested result map (multi-table query).

9. Enter simple result mapping

10. Put the processed result in the List collection of DefaultResultHandler and return

 

 

Guess you like

Origin blog.csdn.net/sumengnan/article/details/114186961