Executor
The interface can complete operations such as adding, deleting, retrieving, and transaction processing based on the following methods. In fact, all database operations in mybatis are implemented by calling these methods.
public interface Executor { ResultHandler NO_RESULT_HANDLER = null; // Data update operation, where data addition, deletion and update can be implemented by this method int update(MappedStatement ms, Object parameter) throws SQLException; // Data query operation, return result in list form <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException; // Data query operation, return result in list form /** * Perform query operations * @param ms mapping statement object * @param parameter parameter object * @param rowBounds Page turn restrictions * @param resultHandler ResultHandler * @param <E> Output result type * @return Query results * @throws SQLException */ <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException; // Data query operation, return result is cursor form <E> Cursor<E> queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds) throws SQLException; // Clean up the cache List<BatchResult> flushStatements() throws SQLException; // Submit transaction void commit(boolean required) throws SQLException; // Roll back the transaction void rollback(boolean required) throws SQLException; // Create cached key value for the current query CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql); // Is there a specified value for the local cache boolean isCached(MappedStatement ms, CacheKey key); // Clean up local cache void clearLocalCache(); // Lazy loading void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType); // Get transactions Transaction getTransaction(); // Turn off the actuator void close(boolean forceRollback); // Determine whether the actuator is turned off boolean isClosed(); // Set the executor packaging void setExecutorWrapper(Executor executor); }
BaseExecutor
It is an abstract class and uses the template pattern to implement some common basic functions of its subclasses, and hand over operations directly related to the subclass to the subclass for processing.
public abstract class BaseExecutor implements Executor { private static final Log log = (); protected Transaction transaction; protected Executor wrapper; protected ConcurrentLinkedQueue<DeferredLoad> deferredLoads; // Cache of query operation results protected PerpetualCache localCache; // Cache of output parameters of Callable query protected PerpetualCache localOutputParameterCache; protected Configuration configuration; protected int queryStack; private boolean closed; /** * Update database data, the three operations of INSERT/UPDATE/DELETE will call this method * @param ms mapping statement * @param parameter parameter object * @return Database operation results * @throws SQLException */ @Override public int update(MappedStatement ms, Object parameter) throws SQLException { ().resource(()) .activity("executing an update").object(()); if (closed) { // The actuator has been closed throw new ExecutorException("Executor was closed."); } // Clean up local cache clearLocalCache(); // Return to call subclass for operation return doUpdate(ms, parameter); } /** * Perform query operations * @param ms mapping statement object * @param parameter parameter object * @param rowBounds Page turn restrictions * @param resultHandler ResultHandler * @param <E> Output result type * @return Query results * @throws SQLException */ @Override public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException { BoundSql boundSql = (parameter); // Generate cached key CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql); return query(ms, parameter, rowBounds, resultHandler, key, boundSql); } /** * Query the data in the database * @param ms mapping statement * @param parameter parameter object * @param rowBounds Page turn restrictions * @param resultHandler ResultHandler * @param key cached key * @param boundSql query statement * @param <E> Result Type * @return Results list * @throws SQLException */ @SuppressWarnings("unchecked") @Override public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException { ().resource(()).activity("executing a query").object(()); if (closed) { // The actuator has been closed throw new ExecutorException("Executor was closed."); } if (queryStack == 0 && ()) { // New query stack and requires clear cache // Clear Level 1 cache clearLocalCache(); } List<E> list; try { queryStack++; // Try to get results from local cache list = resultHandler == null ? (List<E>) (key) : null; if (list != null) { // There are results in the local cache, so the CALLABLE statement needs to be bound to the IN/INOUT parameter. handleLocallyCachedOutputParameters(ms, key, parameter, boundSql); } else { //The local cache has no results, so the database needs to be queried list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql); } } finally { queryStack--; } if (queryStack == 0) { // Handling of lazy loading operations for (DeferredLoad deferredLoad : deferredLoads) { (); } (); // If the scope of the local cache is STATEMENT, clear the local cache immediately if (() == ) { clearLocalCache(); } } return list; } /** * Key to generate the cache of the query * @param ms mapping statement object * @param parameterObject parameter object * @param rowBounds Page turn restrictions * @param boundSql SQL statement after parsing * @return Generated key value */ @Override public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) { if (closed) { throw new ExecutorException("Executor was closed."); } // Create a CacheKey and update all query parameters in turn to write CacheKey cacheKey = new CacheKey(); (()); (()); (()); (()); List<ParameterMapping> parameterMappings = (); TypeHandlerRegistry typeHandlerRegistry = ().getTypeHandlerRegistry(); for (ParameterMapping parameterMapping : parameterMappings) { if (() != ) { Object value; String propertyName = (); if ((propertyName)) { value = (propertyName); } else if (parameterObject == null) { value = null; } else if ((())) { value = parameterObject; } else { MetaObject metaObject = (parameterObject); value = (propertyName); } (value); } } if (() != null) { (().getId()); } return cacheKey; } }
BaseExecutor has four implementation classes:
-
CloseExecutor
: Only the actuator that indicates that it has been turned off and has no other actual functions -
SimpleExecutor
: The simplest actuator -
BatchExecutor
: Executors that support batch execution function -
ReuseExecutor
: Executors that support Statement object multiplexing.
SimpleExecutor
,BatchExecutor
,ReuseExecutor
The choice of these three actuators ismybatis
The optional value is performed in the configuration file bysession
The ExecutorType definition in the package. These three executors mainly use StatementHandler to complete the creation of Statement objects, binding parameters, etc.
This is all about this article about the executor package executor function. For more related executor package content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!