Przeglądaj źródła

增强逻辑删除入参下参数不匹配导致的异常.

nieqiurong 1 rok temu
rodzic
commit
48a61582a4

+ 8 - 2
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/impl/ServiceImpl.java

@@ -32,6 +32,8 @@ import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.mybatis.spring.SqlSessionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.convert.ConversionService;
+import org.springframework.core.convert.support.DefaultConversionService;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.io.Serializable;
@@ -52,6 +54,8 @@ import java.util.function.Function;
 @SuppressWarnings("unchecked")
 public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
 
+    private final ConversionService conversionService = DefaultConversionService.getSharedInstance();
+
     protected Log log = LogFactory.getLog(getClass());
 
     @Autowired
@@ -292,7 +296,8 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         if (useFill && tableInfo.isWithLogicDelete()) {
             if (!entityClass.isAssignableFrom(id.getClass())) {
                 T instance = tableInfo.newInstance();
-                tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), id);
+                Object value = tableInfo.getKeyType() != id.getClass() ? conversionService.convert(id, tableInfo.getKeyType()) : id;
+                tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), value);
                 return removeById(instance);
             }
         }
@@ -317,7 +322,8 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
                     sqlSession.update(sqlStatement, e);
                 } else {
                     T instance = tableInfo.newInstance();
-                    tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), e);
+                    Object value = tableInfo.getKeyType() != e.getClass() ? conversionService.convert(e, tableInfo.getKeyType()) : e;
+                    tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), value);
                     sqlSession.update(sqlStatement, instance);
                 }
             } else {

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

@@ -637,6 +637,8 @@ class H2UserTest extends BaseTest {
         H2User h2User = new H2User(3L, "test");
         userService.removeById(1L);
         userService.removeById(1L, true);
+        userService.removeById(1, true);
+        userService.removeById("1", true);
         userService.removeById(1L, false);
         userService.removeById(h2User);
         userService.removeById(h2User, true);
@@ -645,7 +647,7 @@ class H2UserTest extends BaseTest {
         userService.removeBatchByIds(Arrays.asList(1L, 2L, h2User), 2);
         userService.removeBatchByIds(Arrays.asList(1L, 2L, h2User), true);
         userService.removeBatchByIds(Arrays.asList(1L, 2L, h2User), false);
-        userService.removeBatchByIds(Arrays.asList(1L, 2L, h2User), 2, true);
+        userService.removeBatchByIds(Arrays.asList(1L, 2L, 3, "3", h2User), 2, true);
         userService.removeBatchByIds(Arrays.asList(1L, 2L, h2User), 2, false);
     }