当前位置:网站首页>Yyds dry inventory executor package (executor function)

Yyds dry inventory executor package (executor function)

2022-06-26 07:36:00 Grey Wolf_ cxh

executor package ( Actuator function )

executor The functions provided by each sub package in the package , Ultimately, these functions are provided by Executor Interfaces and their implementation classes jointly provide external services .

Executor The interface can be added based on the following methods , Delete , Change query and transaction processing . in fact ,mybatis All database operations in are implemented by calling these methods .

      
      
public interface Executor {

ResultHandler NO_RESULT_HANDLER = null;

// Data update operation , Increase of data 、 Delete 、 Updates can be implemented by this method
int update(MappedStatement ms, Object parameter) throws SQLException;
// Data query operation , The returned result is in the form of a list
<E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException;
// Data query operation , The returned result is in the form of a list
/**
* Perform query operation
* @param ms Mapping statement objects
* @param parameter Parameter object
* @param rowBounds Page turning restrictions
* @param resultHandler Result processor
* @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 , The returned result is in cursor form
<E> Cursor<E> queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds) throws SQLException;
// Clean cache
List<BatchResult> flushStatements() throws SQLException;
// Commit transaction
void commit(boolean required) throws SQLException;
// Roll back the transaction
void rollback(boolean required) throws SQLException;
// Create cached key values for the current query
CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql);
// Whether the local cache has a specified value
boolean isCached(MappedStatement ms, CacheKey key);
// Clean up the local cache
void clearLocalCache();
// Lazy loading
void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType);
// Get transaction
Transaction getTransaction();
// Turn off the actuator
void close(boolean forceRollback);
// Judge whether the actuator is closed
boolean isClosed();
// Set the actuator packing
void setExecutorWrapper(Executor executor);

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.

BaseExecutor Is an abstract class , And the template mode is used , It implements some common basic functions of its subclasses , And the operations directly related to the subclass are handled by the subclass .

      
      
public abstract class BaseExecutor implements Executor {

private static final Log log = LogFactory.getLog(BaseExecutor.class);

protected Transaction transaction;
protected Executor wrapper;

protected ConcurrentLinkedQueue<DeferredLoad> deferredLoads;
// Result cache of query operation
protected PerpetualCache localCache;
// Callable Output parameter cache of query
protected PerpetualCache localOutputParameterCache;
protected Configuration configuration;

protected int queryStack;
private boolean closed;




/**
* Update database data ,INSERT/UPDATE/DELETE All three operations call this method
* @param ms Mapping statements
* @param parameter Parameter object
* @return Database operation results
* @throws SQLException
*/
@Override
public int update(MappedStatement ms, Object parameter) throws SQLException {
ErrorContext.instance().resource(ms.getResource())
.activity("executing an update").object(ms.getId());
if (closed) {
// The actuator has been closed
throw new ExecutorException("Executor was closed.");
}
// Clean up the local cache
clearLocalCache();
// Return to call the subclass for operation
return doUpdate(ms, parameter);
}



/**
* Perform query operation
* @param ms Mapping statement objects
* @param parameter Parameter object
* @param rowBounds Page turning restrictions
* @param resultHandler Result processor
* @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 = ms.getBoundSql(parameter);
// Generate cached keys
CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql);
return query(ms, parameter, rowBounds, resultHandler, key, boundSql);
}

/**
* Query the data in the database
* @param ms Mapping statements
* @param parameter Parameter object
* @param rowBounds Page turning restrictions
* @param resultHandler Result processor
* @param key The cache keys
* @param boundSql Query statement
* @param <E> Result Type
* @return Result 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 {
ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
if (closed) {
// The actuator has been closed
throw new ExecutorException("Executor was closed.");
}
if (queryStack == 0 && ms.isFlushCacheRequired()) { // New query stack and request to clear cache
// Clear the first level cache
clearLocalCache();
}
List<E> list;
try {
queryStack++;
// Try getting results from the local cache
list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
if (list != null) {
// There are results in the local cache , For CALLABLE Statements also need to be bound to IN/INOUT Parameter
handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
} else {
// The local cache has no results , Therefore, you need to query the database
list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
}
} finally {
queryStack--;
}
if (queryStack == 0) {
// Processing of lazy loading operation
for (DeferredLoad deferredLoad : deferredLoads) {
deferredLoad.load();
}
deferredLoads.clear();
// If the scope of the local cache is STATEMENT, Clear the local cache immediately
if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
clearLocalCache();
}
}
return list;
}



/**
* Generate cached keys for queries
* @param ms Mapping statement objects
* @param parameterObject Parameter object
* @param rowBounds Page turning restrictions
* @param boundSql After parsing SQL sentence
* @return Generated key values
*/
@Override
public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {
if (closed) {
throw new ExecutorException("Executor was closed.");
}
// establish CacheKey, And all query parameters are updated and written in turn
CacheKey cacheKey = new CacheKey();
cacheKey.update(ms.getId());
cacheKey.update(rowBounds.getOffset());
cacheKey.update(rowBounds.getLimit());
cacheKey.update(boundSql.getSql());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
TypeHandlerRegistry typeHandlerRegistry = ms.getConfiguration().getTypeHandlerRegistry();
for (ParameterMapping parameterMapping : parameterMappings) {
if (parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
String propertyName = parameterMapping.getProperty();
if (boundSql.hasAdditionalParameter(propertyName)) {
value = boundSql.getAdditionalParameter(propertyName);
} else if (parameterObject == null) {
value = null;
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
value = parameterObject;
} else {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
value = metaObject.getValue(propertyName);
}
cacheKey.update(value);
}
}
if (configuration.getEnvironment() != null) {
cacheKey.update(configuration.getEnvironment().getId());
}
return cacheKey;
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.

BaseExecutor There are four implementation classes :

CloseExecutor : Only those actuators that indicate that they have closed , No other practical functions

SimpleExecutor : One of the simplest actuators

BatchExecutor : Actuators that support batch execution

ReuseExecutor : Support Statement Object reuse executor .

SimpleExecutor,BatchExecutor,ReuseExecutor The choice of these three actuators is in mybatis In the configuration file of , The optional values are defined by session In bag ExecutorType Definition , These three actuators are mainly based on StatementHandler Finish creating Statement object , Binding parameters, etc .


原网站

版权声明
本文为[Grey Wolf_ cxh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202171036580483.html