4 large basic components Detailed Mybatis execution of SQL (illustrated)

1、Executor

sql actuator, which corresponds to the full path classes: org.apache.ibatis.executor.Executor.

FIG class 1.1 Executor

Here Insert Picture Description

  • Executor actuator (commit the transaction) based on the interface definition update (update or insert), query (query), commit, rollback (rollback the transaction). Next, an overview of some important ways:

    • int update (MappedStatement ms, Object parameter) throws SQLException update or insertion method, which parameters are as follows:, 1) MappedStatement ms: SQL statement mapping (Mapper.xml a method corresponding to a file for each target MappedStatement) 2) Object parameter: Parameter, usually a List collection.
    • <E> List <E> query (MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) query method, which parameters are as follows: 1) RowBounds: a boundary line, the main value of the paging parameters limit, offset. 2) ResultHandler resultHandler: results of the processor.
    • CacheKey createCacheKey (MappedStatement ms, Object parameterObj, RowBounds bounds, BoundSql bSql) to create the cache Key, Mybatis twelve cache Cache Key, Key can be seen that determined by the four parameters. 1) BoundSql boundSql: SQL statement can be obtained through the object.
  • CachingExecutor support SQL Result Cache actuators, pay attention to their application design patterns, the class will hold a delegate object of Executor, CachingExecutor focus on specific logic and cache, its final execution by SQL delegate object is achieved in that its internal delegate object to BaseExecutor implementation class.

  • BaseExecutor Executor underlying implementation class, which is an abstract class, with regard to query, update its specific implementation subclasses to achieve, the following four are the subclasses.

  • SimpleExecutor simple Executor executor.

  • BatchExecutor support Executor executor batch execution.

  • ClosedExecutor represents a closed Executor.

  • ReuseExecutor support reuse Statement, SQL-key, cache Statement object.

1.2 Creating Executor

In the Mybatis, created by the Executor Configuration object created by the specific code as follows:

Configuration#newExecitor

public Executor newExecutor(Transaction transaction) {
  return newExecutor(transaction, defaultExecutorType);   // @1
}


public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
  executorType = executorType == null ? defaultExecutorType : executorType;
  executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
  Executor executor;
  if (ExecutorType.BATCH == executorType) {   // @2
    executor = new BatchExecutor(this, transaction);
  } else if (ExecutorType.REUSE == executorType) {
    executor = new ReuseExecutor(this, transaction);
  } else {
    executor = new SimpleExecutor(this, transaction);
  }
  if (cacheEnabled) { // @3
    executor = new CachingExecutor(executor);
  }
  executor = (Executor) interceptorChain.pluginAll(executor);  // @4
  return executor;
}
复制代码

As can be seen from the above code, Executor created by the three key points are as follows: @ 1 Code: Default is ExecutorType ExecutorType.SIMPLE, i.e. default Executory created for SimpleExecutor. Code @ 2: Create Executory according to the value corresponding to the executorType. Code @ 3: cacheEnabled If true, the creation CachingExecutory, then hold Executor created above in its interior, cacheEnabled default is true, the Executor is created by default CachingExecutor, and its interior wrapped SimpleExecutor. Code @ 4: InterceptorChain.pluginAll create a proxy object for the executor, namely Mybatis of dismantling mechanism, detailed in this article series.

2、StatementHandler

Before learning StatementHandler, let's take a look at JDBC-related knowledge. JDBC and the two main target statement execution: java.sql.Statement, java.sql.PrepareStatement we should not be unfamiliar objects, execute methods of the object it is to execute SQL statements of the entrance, created by the Statement object java.sql.Connection objects . Mybatis of StatementHandler, is Mybatis create a Statement object processor that will take over StatementHandler create a Statement object.

2.1 StatementHandler类图

Here Insert Picture Description

  • StatementHandler root interface, we focus on what method of its definition:

    • Statement prepare (Connection connection) to create a Statement object that this method creates a Statement object from the Connection object.
    • void parameterize (Statement statement) of Statement object parameterization, in particular PreapreStatement object.
    • void batch (Statement statement) batch execution SQL.
    • int update (Statement statement) update operation.
    • <E> List <E> query (Statement statement, ResultHandler resultHandler) queries.
    • BoundSql getBoundSql () Gets the SQL statement.
    • ParameterHandler getParameterHandler () corresponding to the acquired parameter processor.
  • BaseStatementHandler StatementHandler abstract implementation class, SimpleStatementHandler, PrepareStatementHandler, CallableStatementHandler its subclasses. We look at each look at examples of variables:

    • Configuration configuration Mybatis global configuration objects.
    • ObjectFactory objectFactory object factory.
    • TypeHandlerRegistry typeHandlerRegistry type registrar.
    • ResultSetHandler resultSetHandler result set Handler.
    • ParameterHandler parameterHandler parameter processor Handler.
    • Executor executor SQL actuators.
    • MappedStatement mappedStatement SQL statement mapping (Mapper.xml a file corresponding to each method MappedStatement object)
    • RowBounds rowBounds line boundary, the main value of the paging parameters limit, offset.
    • BoundSql boundSql SQL statement can be obtained through the object.
  • SimpleStatementHandler specific StatementHandler implementor, java.sql.Statement object creation processor.

  • Creating processor PrepareStatementHandler java.sql.PrepareStatement object.

  • CallableStatementHandler java.sql.CallableStatement objects created processor can be used to execute a stored procedure call Statement.

  • RoutingStatementHandler StatementHandler router, we look after its construction method, the class will be clear in the chest.

public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {

  switch (ms.getStatementType()) { // @1
    case STATEMENT:
      delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
      break;
    case PREPARED:
      delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
      break;
    case CALLABLE:
      delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
      break;
    default:
      throw new ExecutorException("Unknown statement type: " + ms.getStatementType());
  }

}
复制代码

Originally created corresponding StatementHandler according to statementType MappedStatement object.

2.2 Creating StatementHandler

Configuration#newStatementHandler

public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
  StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql); // @1
  statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler); // @2
  return statementHandler;
}
复制代码

Two key points of the method are as follows: @ 1 Code: RoutingStatementHandler create objects, then depending on the type of SQL statements to create corresponding object StatementHandler therein. Code @ 2: StatementHandler introducing dismantling mechanism, this part will be detailed in a subsequent article in the topic, skip here.

3 ParameterHandler

Parameter processor. Similarly, we first look at their class diagram.

FIG class 3.1 ParameterHandler

Here Insert Picture Description
This is relatively simple, is to deal with parameterized PreparedStatemet interface, you can also look at the way that the call chain (the section will detail the next one).
Here Insert Picture Description

3.2 Creating ParameterHandler

Configuration#newParameterHandler

public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
  ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
  parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);  // @1
  return parameterHandler;
}
复制代码

The same interface also supports plug-in mechanism.

4、ResultSetHandler

Handler processing results. We also look at their class diagram.

4.1 ResultSetHandler类图

Here Insert Picture Description
Jdbc ResultSet processing processor.

4.2 ResultSetHandler create

Configuration#newResultSetHandler

public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler,
    ResultHandler resultHandler, BoundSql boundSql) {
  ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
  resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
  return resultSetHandler;
}
复制代码

Also supports plug-in mechanism, we have a little look at what it calls the chain:

Here Insert Picture Description
It can be seen that the inlet is called SQL execution.

A "source code analysis Mybatis integration ShardingJdbc SQL execution process" under article as a pre-chapter, focusing on specific responsibilities Executor, StatementHandler, ParameterHandler, ResultSetHandler to class picture shows the base and on the role of the core methods in detail, and then describes in detail how these objects are created, and leads Mybatis dismantling mechanism.


The authors introduce: "RocketMQ Technology Insider," the author, maintain public number: middleware interested circles , mainly published a collection of java source code reading, JUC (java and contract), Netty, ElasticJob, Mycat, Dubbo, RocketMQ, mybaits other source.

Here Insert Picture Description

Guess you like

Origin juejin.im/post/5dd148175188254c635876d1