|
@@ -35,13 +35,14 @@ import org.apache.ibatis.type.TypeException;
|
|
|
import org.apache.ibatis.type.TypeHandler;
|
|
|
import org.apache.ibatis.type.TypeHandlerRegistry;
|
|
|
|
|
|
-import java.lang.reflect.Array;
|
|
|
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.List;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -53,6 +54,8 @@ import java.util.Map;
|
|
|
*/
|
|
|
public class MybatisParameterHandler implements ParameterHandler {
|
|
|
|
|
|
+ public static final String[] COLLECTION_KEYS = new String[]{"collection", "coll", "list", "array"};
|
|
|
+
|
|
|
private final TypeHandlerRegistry typeHandlerRegistry;
|
|
|
private final MappedStatement mappedStatement;
|
|
|
private final Object parameterObject;
|
|
@@ -77,13 +80,7 @@ public class MybatisParameterHandler implements ParameterHandler {
|
|
|
if (SimpleTypeRegistry.isSimpleType(parameter.getClass())) {
|
|
|
return parameter;
|
|
|
}
|
|
|
- Collection<Object> parameters = getParameters(parameter);
|
|
|
- if (null != parameters) {
|
|
|
- // 感觉这里可以稍微优化一下,理论上都是同一个.
|
|
|
- parameters.forEach(this::process);
|
|
|
- } else {
|
|
|
- process(parameter);
|
|
|
- }
|
|
|
+ extractParameters(parameter).forEach(this::process);
|
|
|
}
|
|
|
return parameter;
|
|
|
}
|
|
@@ -185,8 +182,10 @@ public class MybatisParameterHandler implements ParameterHandler {
|
|
|
* </p>
|
|
|
*
|
|
|
* @return 集合参数
|
|
|
+ * @deprecated 3.5.3
|
|
|
*/
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
+ @Deprecated
|
|
|
protected Collection<Object> getParameters(Object parameterObject) {
|
|
|
Collection<Object> parameters = null;
|
|
|
if (parameterObject instanceof Collection) {
|
|
@@ -211,10 +210,40 @@ public class MybatisParameterHandler implements ParameterHandler {
|
|
|
return parameters;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 提取特殊key值 (只支持外层参数,嵌套参数不考虑)
|
|
|
+ * List<Map>虽然这种写法目前可以进去提取et,但不考虑再提取list等其他类型,只做简单参数提取
|
|
|
+ *
|
|
|
+ * @param parameterObject 参数
|
|
|
+ * @return 预期可能为填充参数值
|
|
|
+ */
|
|
|
+ @SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
+ private Collection<Object> extractParameters(Object parameterObject) {
|
|
|
+ Collection<Object> parameters = new ArrayList<>();
|
|
|
+ if (parameterObject instanceof Collection) {
|
|
|
+ parameters = (Collection) parameterObject;
|
|
|
+ } else if (ArrayUtils.isArray(parameterObject)) {
|
|
|
+ parameters = toCollection(parameterObject);
|
|
|
+ } else if (parameterObject instanceof Map) {
|
|
|
+ Map parameterMap = (Map) parameterObject;
|
|
|
+ if (parameterMap.containsKey(Constants.ENTITY)) {
|
|
|
+ parameters.add(parameterMap.get(Constants.ENTITY));
|
|
|
+ }
|
|
|
+ for (String key : COLLECTION_KEYS) {
|
|
|
+ if (parameterMap.containsKey(key)) {
|
|
|
+ parameters.addAll(toCollection(parameterMap.get(key)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ parameters.add(parameterObject);
|
|
|
+ }
|
|
|
+ return parameters;
|
|
|
+ }
|
|
|
+
|
|
|
@SuppressWarnings("unchecked")
|
|
|
protected Collection<Object> toCollection(Object value) {
|
|
|
if (value == null) {
|
|
|
- return null;
|
|
|
+ return Collections.emptyList();
|
|
|
}
|
|
|
// 只处理array和collection
|
|
|
if (ArrayUtils.isArray(value)) {
|
|
@@ -222,7 +251,7 @@ public class MybatisParameterHandler implements ParameterHandler {
|
|
|
} else if (Collection.class.isAssignableFrom(value.getClass())) {
|
|
|
return (Collection<Object>) value;
|
|
|
}
|
|
|
- return null;
|
|
|
+ return Collections.emptyList();
|
|
|
}
|
|
|
|
|
|
@Override
|