소스 검색

修复逻辑删除在字段填充下存在字段重复

https://github.com/baomidou/mybatis-plus/issues/4148
聂秋荣 3 년 전
부모
커밋
a46268c35b

+ 4 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteById.java

@@ -36,11 +36,11 @@ import static java.util.stream.Collectors.toList;
  * @since 2018-04-06
  */
 public class DeleteById extends AbstractMethod {
-    
+
     public DeleteById() {
         super("deleteById");
     }
-    
+
     /**
      * @param name 方法名
      * @since 3.4.4
@@ -48,7 +48,7 @@ public class DeleteById extends AbstractMethod {
     public DeleteById(String name) {
         super(name);
     }
-    
+
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         String sql;
@@ -56,6 +56,7 @@ public class DeleteById extends AbstractMethod {
         if (tableInfo.isWithLogicDelete()) {
             List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream()
                 .filter(TableFieldInfo::isWithUpdateFill)
+                .filter(f -> !f.isLogicDelete())
                 .collect(toList());
             if (CollectionUtils.isNotEmpty(fieldInfos)) {
                 String sqlSet = "SET " + SqlScriptUtils.convertIf(fieldInfos.stream()

+ 1 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableFieldInfo.java

@@ -110,6 +110,7 @@ public class TableFieldInfo implements Constants {
     /**
      * 是否是逻辑删除字段
      */
+    @Getter
     private boolean logicDelete = false;
     /**
      * 逻辑删除值

+ 5 - 4
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDeleteByIdWithFill.java

@@ -39,17 +39,17 @@ import static java.util.stream.Collectors.toList;
  * </p>
  *
  * @author miemie
- * @deprecated 3.4.4 {@link com.baomidou.mybatisplus.core.injector.methods.DeleteById}
  * @since 2018-11-09
+ * @deprecated 3.4.4 {@link com.baomidou.mybatisplus.core.injector.methods.DeleteById}
  */
 @SuppressWarnings("serial")
 @Deprecated
 public class LogicDeleteByIdWithFill extends AbstractMethod {
-    
+
     public LogicDeleteByIdWithFill() {
         super("deleteByIdWithFill");
     }
-    
+
     /**
      * @param name 方法名
      * @since 3.4.4
@@ -57,7 +57,7 @@ public class LogicDeleteByIdWithFill extends AbstractMethod {
     public LogicDeleteByIdWithFill(String name) {
         super(name);
     }
-    
+
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         String sql;
@@ -65,6 +65,7 @@ public class LogicDeleteByIdWithFill extends AbstractMethod {
         if (tableInfo.isWithLogicDelete()) {
             List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream()
                 .filter(TableFieldInfo::isWithUpdateFill)
+                .filter(f -> !f.isLogicDelete())
                 .collect(toList());
             if (CollectionUtils.isNotEmpty(fieldInfos)) {
                 String sqlSet = "SET " + fieldInfos.stream().map(i -> i.getSqlSet(EMPTY)).collect(joining(EMPTY))

+ 2 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/MybatisMapperRegistryTest.java

@@ -86,7 +86,7 @@ class MybatisMapperRegistryTest extends BaseTest {
             H2StudentMapper studentMapper = mapperRegistry.getMapper(H2StudentMapper.class, sqlSession);
 
             Assertions.assertTrue(configuration.hasStatement(H2StudentMapper.class.getName() + ".selectById"));
-            studentMapper.selectById(1);
+            studentMapper.selectById(1L);
 
             Field field = mapperRegistry.getClass().getDeclaredField("knownMappers");
             field.setAccessible(true);
@@ -99,7 +99,7 @@ class MybatisMapperRegistryTest extends BaseTest {
             Map<Method, ?> methodCache = mybatisMapperProxyFactory.getMethodCache();
             Assertions.assertTrue(methodCache.isEmpty());
 
-            h2StudentChildrenMapper.selectById(2);
+            h2StudentChildrenMapper.selectById(2L);
             methodCache = mybatisMapperProxyFactory.getMethodCache();
             Assertions.assertFalse(methodCache.isEmpty());
         }

+ 3 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/logicdel/Entity.java

@@ -1,5 +1,7 @@
 package com.baomidou.mybatisplus.test.logicdel;
 
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableLogic;
 import lombok.Data;
 
@@ -17,6 +19,7 @@ public class Entity implements Serializable {
 
     private String name;
 
+    @TableField(fill = FieldFill.UPDATE)
     @TableLogic(delval = "true", value = "false")
     private Boolean deleted;
 }

+ 2 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/logicdel/LogicDelTest.java

@@ -18,8 +18,8 @@ public class LogicDelTest extends BaseDbTest<EntityMapper> {
     @Test
     void logicDel() {
         doTestAutoCommit(i -> {
-            int delete = i.deleteById(1);
-            assertThat(delete).isEqualTo(1);
+            int delete = i.deleteById(1L);
+            assertThat(delete).isEqualTo(1L);
 
             delete = i.delete(Wrappers.<Entity>lambdaQuery().eq(Entity::getId, 2));
             assertThat(delete).isEqualTo(1);