|
@@ -15,97 +15,35 @@
|
|
|
*/
|
|
|
package com.baomidou.mybatisplus.core.executor;
|
|
|
|
|
|
-import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
-import org.apache.ibatis.cache.CacheKey;
|
|
|
import org.apache.ibatis.cursor.Cursor;
|
|
|
-import org.apache.ibatis.executor.BaseExecutor;
|
|
|
import org.apache.ibatis.executor.BatchResult;
|
|
|
-import org.apache.ibatis.executor.ExecutorException;
|
|
|
import org.apache.ibatis.executor.statement.StatementHandler;
|
|
|
import org.apache.ibatis.logging.Log;
|
|
|
import org.apache.ibatis.mapping.BoundSql;
|
|
|
import org.apache.ibatis.mapping.MappedStatement;
|
|
|
-import org.apache.ibatis.mapping.ParameterMapping;
|
|
|
-import org.apache.ibatis.mapping.ParameterMode;
|
|
|
-import org.apache.ibatis.reflection.MetaObject;
|
|
|
import org.apache.ibatis.session.Configuration;
|
|
|
import org.apache.ibatis.session.ResultHandler;
|
|
|
import org.apache.ibatis.session.RowBounds;
|
|
|
import org.apache.ibatis.transaction.Transaction;
|
|
|
-import org.apache.ibatis.type.TypeHandlerRegistry;
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
import java.sql.SQLException;
|
|
|
import java.sql.Statement;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Optional;
|
|
|
|
|
|
/**
|
|
|
- * 重写执行器
|
|
|
- * {@link org.apache.ibatis.executor.SimpleExecutor}
|
|
|
+ * 重写执行器 {@link org.apache.ibatis.executor.SimpleExecutor}
|
|
|
*
|
|
|
* @author nieqiurong 2019/4/14.
|
|
|
*/
|
|
|
-public class MybatisSimpleExecutor extends BaseExecutor {
|
|
|
-
|
|
|
+public class MybatisSimpleExecutor extends MybatisBaseExecutor {
|
|
|
+
|
|
|
public MybatisSimpleExecutor(Configuration configuration, Transaction transaction) {
|
|
|
super(configuration, transaction);
|
|
|
}
|
|
|
-
|
|
|
- @Override
|
|
|
- public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {
|
|
|
- if (super.isClosed()) {
|
|
|
- throw new ExecutorException("Executor was closed.");
|
|
|
- }
|
|
|
- CacheKey cacheKey = new CacheKey();
|
|
|
- cacheKey.update(ms.getId());
|
|
|
- Object offset = rowBounds.getOffset();
|
|
|
- Object limit = rowBounds.getLimit();
|
|
|
- if (parameterObject instanceof Map) {
|
|
|
- Map<?, ?> parameterMap = (Map<?, ?>) parameterObject;
|
|
|
- Optional<? extends Map.Entry<?, ?>> optional = parameterMap.entrySet().stream().filter(entry -> entry.getValue() instanceof IPage).findFirst();
|
|
|
- if (optional.isPresent()) {
|
|
|
- IPage<?> page = (IPage) optional.get().getValue();
|
|
|
- offset = page.getCurrent();
|
|
|
- limit = page.getTotal();
|
|
|
- }
|
|
|
- } else if (parameterObject instanceof IPage) {
|
|
|
- IPage<?> page = (IPage) parameterObject;
|
|
|
- offset = page.getCurrent();
|
|
|
- limit = page.getTotal();
|
|
|
- }
|
|
|
- cacheKey.update(offset);
|
|
|
- cacheKey.update(limit);
|
|
|
- cacheKey.update(boundSql.getSql());
|
|
|
- List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
|
|
|
- TypeHandlerRegistry typeHandlerRegistry = ms.getConfiguration().getTypeHandlerRegistry();
|
|
|
- // mimic DefaultParameterHandler logic
|
|
|
- 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) {
|
|
|
- // issue #176
|
|
|
- cacheKey.update(configuration.getEnvironment().getId());
|
|
|
- }
|
|
|
- return cacheKey;
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
|
|
|
Statement stmt = null;
|
|
@@ -118,7 +56,7 @@ public class MybatisSimpleExecutor extends BaseExecutor {
|
|
|
closeStatement(stmt);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
|
|
|
Statement stmt = null;
|
|
@@ -131,7 +69,7 @@ public class MybatisSimpleExecutor extends BaseExecutor {
|
|
|
closeStatement(stmt);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql) throws SQLException {
|
|
|
Configuration configuration = ms.getConfiguration();
|
|
@@ -144,12 +82,12 @@ public class MybatisSimpleExecutor extends BaseExecutor {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public List<BatchResult> doFlushStatements(boolean isRollback) {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
private Statement prepareStatement(StatementHandler handler, Log statementLog, boolean isCursor) throws SQLException {
|
|
|
Statement stmt;
|
|
|
Connection connection = getConnection(statementLog);
|