Forráskód Böngészése

支持多参数提取填充情况.

https://github.com/baomidou/mybatis-plus/issues/5447
nieqiurong 1 éve
szülő
commit
34e2f3fa30

+ 39 - 10
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisParameterHandler.java

@@ -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

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

@@ -745,4 +745,30 @@ class H2UserTest extends BaseTest {
         h2StudentMapper.insertFillByCustomMethod13(map);
         list.forEach(user -> Assertions.assertNotNull(user.getTestType()));
     }
+
+    @Test
+    void testUpdateFill(){
+        Map<String, Object> map;
+        H2User h2User;
+        h2User = new H2User();
+        map = new HashMap<>();
+        map.put("et", h2User);
+        map.put("list", Arrays.asList(1L, 2L, 3L));
+        h2StudentMapper.updateFillByCustomMethod1(map);
+        Assertions.assertNotNull(h2User.getLastUpdatedDt());
+
+        h2User = new H2User();
+        h2StudentMapper.updateFillByCustomMethod2(Arrays.asList(1L, 2L, 3L), h2User);
+        Assertions.assertNotNull(h2User.getLastUpdatedDt());
+
+        h2User = new H2User();
+        h2StudentMapper.updateFillByCustomMethod3(Arrays.asList(1L, 2L, 3L), h2User);
+        Assertions.assertNull(h2User.getLastUpdatedDt());
+
+        h2User = new H2User();
+        h2StudentMapper.updateFillByCustomMethod4(Arrays.asList(1L, 2L, 3L), h2User);
+        Assertions.assertNotNull(h2User.getLastUpdatedDt());
+
+    }
+
 }

+ 8 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/mapper/H2StudentMapper.java

@@ -55,4 +55,12 @@ public interface H2StudentMapper extends SuperMapper<H2Student> {
 
     long insertFillByCustomMethod13(Map<String, Object> paramMap);
 
+    long updateFillByCustomMethod1(Map<String, Object> paramMap);
+
+    long updateFillByCustomMethod2(@Param("coll") Collection<Long> ids, @Param("et") H2User h2User);
+
+    long updateFillByCustomMethod3(@Param("coll") Collection<Long> ids, @Param("user") H2User h2User);
+
+    long updateFillByCustomMethod4(@Param("colls") Collection<Long> ids, @Param("et") H2User h2User);
+
 }

+ 32 - 0
mybatis-plus/src/test/resources/com/baomidou/mybatisplus/test/h2/mapper/H2StudentMapper.xml

@@ -86,4 +86,36 @@
         </foreach>
     </insert>
 
+    <update id="updateFillByCustomMethod1">
+        update h2user set deleted = 1,last_updated_dt = #{et.lastUpdatedDt} where test_id in (
+        <foreach collection="list" item="col" separator=",">
+            #{col}
+        </foreach>
+        )
+    </update>
+
+    <update id="updateFillByCustomMethod2">
+        update h2user set deleted = 1,last_updated_dt = #{et.lastUpdatedDt} where test_id in (
+        <foreach collection="coll" item="col" separator=",">
+            #{col}
+        </foreach>
+        )
+    </update>
+
+    <update id="updateFillByCustomMethod3">
+        update h2user set deleted = 1,last_updated_dt = #{user.lastUpdatedDt} where test_id in (
+        <foreach collection="coll" item="col" separator=",">
+            #{col}
+        </foreach>
+        )
+    </update>
+
+    <update id="updateFillByCustomMethod4">
+        update h2user set deleted = 1,last_updated_dt = #{et.lastUpdatedDt} where test_id in (
+        <foreach collection="colls" item="col" separator=",">
+            #{col}
+        </foreach>
+        )
+    </update>
+
 </mapper>