Browse Source

优化填充逻辑(不限制填充key值,嵌套太多Map的不考虑).

nieqiurong 1 năm trước cách đây
mục cha
commit
c9fb4d625b

+ 13 - 54
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisParameterHandler.java

@@ -26,7 +26,11 @@ 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.*;
+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.session.Configuration;
 import org.apache.ibatis.type.JdbcType;
@@ -59,20 +63,12 @@ public class MybatisParameterHandler implements ParameterHandler {
     /**
      * 填充的key值
      *
-     * @see #getCollectionFillKeys()
      * @since 3.5.3.2
      * @deprecated 3.5.4
      */
     @Deprecated
     public static final String[] COLLECTION_KEYS = new String[]{"collection", "coll", "list", "array"};
 
-    /**
-     * 需要填充的集合key值
-     *
-     * @since 3.5.4
-     */
-    private static final Set<String> COLLECTION_FILL_KEYS = new HashSet<>(Arrays.asList(COLLECTION_KEYS));
-
     private final TypeHandlerRegistry typeHandlerRegistry;
     private final MappedStatement mappedStatement;
     private final Object parameterObject;
@@ -243,29 +239,13 @@ public class MybatisParameterHandler implements ParameterHandler {
         } else if (parameterObject instanceof Map) {
             Collection<Object> parameters = new ArrayList<>();
             Map<String, Object> parameterMap = (Map) parameterObject;
-            if (parameterMap.containsKey(Constants.ENTITY)) {
-                parameters.add(parameterMap.get(Constants.ENTITY));
-            }
-            Set<Collection<Object>> objectSet = new HashSet<>();
-            if (parameterMap.size() > COLLECTION_FILL_KEYS.size()) {
-                for (String key : COLLECTION_FILL_KEYS) {
-                    if (parameterMap.containsKey(key)) {
-                        Collection<Object> collection = toCollection(parameterMap.get(key));
-                        if (objectSet.add(collection)) {
-                            parameters.addAll(collection);
-                        }
-                    }
+            Set<Object> objectSet = new HashSet<>();
+            parameterMap.forEach((k, v) -> {
+                if (objectSet.add(v)) {
+                    Collection<Object> collection = toCollection(v);
+                    parameters.addAll(collection);
                 }
-            } else {
-                parameterMap.forEach((k, v) -> {
-                    if (COLLECTION_FILL_KEYS.contains(k)) {
-                        Collection<Object> collection = toCollection(v);
-                        if (objectSet.add(collection)) {
-                            parameters.addAll(collection);
-                        }
-                    }
-                });
-            }
+            });
             return parameters;
         } else {
             return Collections.singleton(parameterObject);
@@ -277,13 +257,13 @@ public class MybatisParameterHandler implements ParameterHandler {
         if (value == null) {
             return Collections.emptyList();
         }
-        // 只处理array和collection
         if (ArrayUtils.isArray(value)) {
             return Arrays.asList((Object[]) value);
         } else if (Collection.class.isAssignableFrom(value.getClass())) {
             return (Collection<Object>) value;
+        } else {
+            return Collections.singletonList(value);
         }
-        return Collections.emptyList();
     }
 
     @Override
@@ -321,25 +301,4 @@ public class MybatisParameterHandler implements ParameterHandler {
             }
         }
     }
-
-    /**
-     * 获取集合特殊key值填充列表
-     *
-     * @return key值列表
-     * @since 3.5.4
-     */
-    public static Set<String> getCollectionFillKeys() {
-        return Collections.unmodifiableSet(COLLECTION_FILL_KEYS);
-    }
-
-    /**
-     * 添加特殊集合Key值填充处理
-     *
-     * @param keys 特殊集合key值
-     * @since 3.5.4
-     */
-    public static void addCollectionFillKeys(String... keys) {
-        COLLECTION_FILL_KEYS.addAll(Arrays.asList(keys));
-    }
-
 }

+ 3 - 3
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/MybatisParameterHandlerTest.java

@@ -122,9 +122,9 @@ class MybatisParameterHandlerTest {
         Model model2 = new Model();
         params1.put("arrays", new Model[]{model1, model2});
         new MybatisParameterHandler(mappedStatement, params1, boundSql);
-        assertThat(model1.getId()).isNull();
-        assertThat(model2.getId()).isNull();
-        assertThat(model1.getInsertOperator()).isNull();
+        assertThat(model1.getId()).isNotNull();
+        assertThat(model2.getId()).isNotNull();
+        assertThat(model1.getInsertOperator()).isNotNull();
         assertThat(model1.getUpdateOperator()).isNull();
 
         model1 = new Model();

+ 2 - 8
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/fill/FillTest.java

@@ -1,10 +1,8 @@
 package com.baomidou.mybatisplus.test.fill;
 
-import com.baomidou.mybatisplus.core.MybatisParameterHandler;
 import com.baomidou.mybatisplus.core.batch.MybatisBatch;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
-import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.baomidou.mybatisplus.core.toolkit.MybatisBatchUtils;
 import com.baomidou.mybatisplus.test.BaseDbTest;
 import org.apache.ibatis.reflection.MetaObject;
@@ -20,10 +18,6 @@ import java.util.List;
  */
 public class FillTest extends BaseDbTest<FillMapper> {
 
-    static {
-        System.out.println("---------增加一个特殊Key值填充处理列表--------------");
-        MybatisParameterHandler.addCollectionFillKeys("mpList");
-    }
     @Test
     void testInsert() {
         doTest(mapper -> {
@@ -58,10 +52,10 @@ public class FillTest extends BaseDbTest<FillMapper> {
     @Test
     void testInsertBatch3() {
         doTest(mapper -> {
-            var entityList = new ArrayList<>(Arrays.asList(new FillEntity(IdWorker.getId()), new FillEntity(IdWorker.getId())));
+            var entityList = new ArrayList<>(Arrays.asList(new FillEntity(), new FillEntity()));
             mapper.insertBatch3(entityList);
             for (FillEntity entity : entityList) {
-                assertEntity(entity, null, 0);
+                assertEntity(entity, "insertAdmin", 1);
             }
         });
     }

+ 1 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2UserTest.java

@@ -780,7 +780,7 @@ class H2UserTest extends BaseTest {
 
         h2User = new H2User();
         h2StudentMapper.updateFillByCustomMethod3(Arrays.asList(1L, 2L, 3L), h2User);
-        Assertions.assertNull(h2User.getLastUpdatedDt());
+        Assertions.assertNotNull(h2User.getLastUpdatedDt());
 
         h2User = new H2User();
         h2StudentMapper.updateFillByCustomMethod4(Arrays.asList(1L, 2L, 3L), h2User);