nieqiurong 1 год назад
Родитель
Сommit
77e1bc5ffd

+ 3 - 53
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisParameterHandler.java

@@ -24,31 +24,21 @@ import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
 import com.baomidou.mybatisplus.core.toolkit.Constants;
 import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
-import org.apache.ibatis.executor.ErrorContext;
-import org.apache.ibatis.executor.parameter.ParameterHandler;
 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.mapping.SqlCommandType;
 import org.apache.ibatis.reflection.MetaObject;
+import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
 import org.apache.ibatis.session.Configuration;
-import org.apache.ibatis.type.JdbcType;
 import org.apache.ibatis.type.SimpleTypeRegistry;
-import org.apache.ibatis.type.TypeException;
-import org.apache.ibatis.type.TypeHandler;
-import org.apache.ibatis.type.TypeHandlerRegistry;
 
 import java.math.BigDecimal;
 import java.math.BigInteger;
-import java.sql.PreparedStatement;
-import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -58,7 +48,7 @@ import java.util.Set;
  * @author nieqiuqiu 2020/6/5
  * @since 3.4.0
  */
-public class MybatisParameterHandler implements ParameterHandler {
+public class MybatisParameterHandler extends DefaultParameterHandler {
 
     /**
      * 填充的key值
@@ -69,17 +59,12 @@ public class MybatisParameterHandler implements ParameterHandler {
     @Deprecated
     public static final String[] COLLECTION_KEYS = new String[]{"collection", "coll", "list", "array"};
 
-    private final TypeHandlerRegistry typeHandlerRegistry;
-    private final MappedStatement mappedStatement;
     private final Object parameterObject;
-    private final BoundSql boundSql;
     private final Configuration configuration;
     private final SqlCommandType sqlCommandType;
 
     public MybatisParameterHandler(MappedStatement mappedStatement, Object parameter, BoundSql boundSql) {
-        this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
-        this.mappedStatement = mappedStatement;
-        this.boundSql = boundSql;
+        super(mappedStatement, parameter, boundSql);
         this.configuration = mappedStatement.getConfiguration();
         this.sqlCommandType = mappedStatement.getSqlCommandType();
         this.parameterObject = processParameter(parameter);
@@ -266,39 +251,4 @@ public class MybatisParameterHandler implements ParameterHandler {
         }
     }
 
-    @Override
-    @SuppressWarnings("unchecked")
-    public void setParameters(PreparedStatement ps) {
-        ErrorContext.instance().activity("setting parameters").object(this.mappedStatement.getParameterMap().getId());
-        List<ParameterMapping> parameterMappings = this.boundSql.getParameterMappings();
-        if (parameterMappings != null) {
-            for (int i = 0; i < parameterMappings.size(); i++) {
-                ParameterMapping parameterMapping = parameterMappings.get(i);
-                if (parameterMapping.getMode() != ParameterMode.OUT) {
-                    Object value;
-                    String propertyName = parameterMapping.getProperty();
-                    if (this.boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params
-                        value = this.boundSql.getAdditionalParameter(propertyName);
-                    } else if (this.parameterObject == null) {
-                        value = null;
-                    } else if (this.typeHandlerRegistry.hasTypeHandler(this.parameterObject.getClass())) {
-                        value = parameterObject;
-                    } else {
-                        MetaObject metaObject = this.configuration.newMetaObject(this.parameterObject);
-                        value = metaObject.getValue(propertyName);
-                    }
-                    TypeHandler typeHandler = parameterMapping.getTypeHandler();
-                    JdbcType jdbcType = parameterMapping.getJdbcType();
-                    if (value == null && jdbcType == null) {
-                        jdbcType = this.configuration.getJdbcTypeForNull();
-                    }
-                    try {
-                        typeHandler.setParameter(ps, i + 1, value, jdbcType);
-                    } catch (TypeException | SQLException e) {
-                        throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
-                    }
-                }
-            }
-        }
-    }
 }