浏览代码

BaseMapper新增deleteById(T entity)方法

nieqiuqiu 4 年之前
父节点
当前提交
033eec9391

+ 1 - 4
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -348,10 +348,7 @@ public abstract class AbstractMethod implements Constants {
             return null;
         }
         /* 缓存逻辑处理 */
-        boolean isSelect = false;
-        if (sqlCommandType == SqlCommandType.SELECT) {
-            isSelect = true;
-        }
+        boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
         return builderAssistant.addMappedStatement(id, sqlSource, StatementType.PREPARED, sqlCommandType,
             null, null, null, parameterType, resultMap, resultType,
             null, !isSelect, isSelect, false, keyGenerator, keyProperty, keyColumn,

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

@@ -17,10 +17,18 @@ package com.baomidou.mybatisplus.core.injector.methods;
 
 import com.baomidou.mybatisplus.core.enums.SqlMethod;
 import com.baomidou.mybatisplus.core.injector.AbstractMethod;
+import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
 import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.mapping.SqlSource;
 
+import java.util.List;
+
+import static java.util.stream.Collectors.joining;
+import static java.util.stream.Collectors.toList;
+
 /**
  * 根据 ID 删除
  *
@@ -34,9 +42,20 @@ public class DeleteById extends AbstractMethod {
         String sql;
         SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_ID;
         if (tableInfo.isWithLogicDelete()) {
-            sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo),
-                tableInfo.getKeyColumn(), tableInfo.getKeyProperty(),
-                tableInfo.getLogicDeleteSql(true, true));
+            List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream()
+                .filter(TableFieldInfo::isWithUpdateFill)
+                .collect(toList());
+            if (CollectionUtils.isNotEmpty(fieldInfos)) {
+                String sqlSet = "SET " + SqlScriptUtils.convertIf(fieldInfos.stream()
+                    .map(i -> i.getSqlSet(EMPTY)).collect(joining(EMPTY)), "!@com.baomidou.mybatisplus.core.toolkit.ReflectionKit@isPrimitiveOrWrapper(_parameter.getClass())", true)
+                    + tableInfo.getLogicDeleteSql(false, false);
+                sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(),
+                    tableInfo.getKeyProperty(), tableInfo.getLogicDeleteSql(true, true));
+            } else {
+                sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo),
+                    tableInfo.getKeyColumn(), tableInfo.getKeyProperty(),
+                    tableInfo.getLogicDeleteSql(true, true));
+            }
             SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Object.class);
             return addUpdateMappedStatement(mapperClass, modelClass, getMethod(sqlMethod), sqlSource);
         } else {

+ 8 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/mapper/BaseMapper.java

@@ -98,6 +98,14 @@ public interface BaseMapper<T> extends Mapper<T> {
      */
     int deleteById(Serializable id);
 
+    /**
+     * 根据实体(ID)删除
+     *
+     * @param entity 实体对象
+     * @since 3.4.4
+     */
+    int deleteById(T entity);
+
     /**
      * 根据 columnMap 条件,删除记录
      *

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

@@ -39,9 +39,11 @@ 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
  */
 @SuppressWarnings("serial")
+@Deprecated
 public class LogicDeleteByIdWithFill extends AbstractMethod {
 
     @Override

+ 10 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/IService.java

@@ -108,6 +108,16 @@ public interface IService<T> {
         return SqlHelper.retBool(getBaseMapper().deleteById(id));
     }
 
+    /**
+     * 根据实体(ID)删除
+     *
+     * @param entity 实体
+     * @since 3.4.4
+     */
+    default boolean removeById(T entity) {
+        return SqlHelper.retBool(getBaseMapper().deleteById(entity));
+    }
+
     /**
      * 根据 columnMap 条件,删除记录
      *

+ 7 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2StudentMapperTest.java

@@ -25,6 +25,7 @@ import com.baomidou.mybatisplus.test.h2.entity.H2Student;
 import com.baomidou.mybatisplus.test.h2.enums.GenderEnum;
 import com.baomidou.mybatisplus.test.h2.enums.GradeEnum;
 import com.baomidou.mybatisplus.test.h2.mapper.H2StudentMapper;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.MethodOrderer;
 import org.junit.jupiter.api.Order;
 import org.junit.jupiter.api.Test;
@@ -104,4 +105,10 @@ class H2StudentMapperTest extends BaseTest {
         System.out.println(wrapper2.getSqlSegment());
     }
 
+    @Test
+    void testDeleteByIdWithEntity() {
+        H2Student h2Student = new H2Student(111L, "测试根据实体删除", 12);
+        studentMapper.insert(h2Student);
+        Assertions.assertEquals(studentMapper.deleteById(h2Student), 1);
+    }
 }

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

@@ -498,6 +498,18 @@ class H2UserTest extends BaseTest {
         Assertions.assertTrue(userService.update(lambdaUpdateWrapper.eq(H2User::getName, "小红")));
     }
 
+    @Test
+    void testLogicDelWithFill() {
+        H2User h2User = new H2User("逻辑删除(根据ID)不填充", AgeEnum.TWO);
+        userService.save(h2User);
+        userService.removeById(h2User.getTestId());
+        Assertions.assertNull(h2User.getLastUpdatedDt());
+        h2User = new H2User("测试逻辑(根据实体)删除填充", AgeEnum.TWO);
+        userService.save(h2User);
+        userService.removeById(h2User);
+        Assertions.assertNotNull(h2User.getLastUpdatedDt());
+    }
+
     /**
      * 观察 {@link com.baomidou.mybatisplus.core.toolkit.LambdaUtils#extract(SFunction)}
      */

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

@@ -34,6 +34,13 @@ public class LogicDelTest extends BaseDbTest<EntityMapper> {
             assertThat(entity).isNotNull();
             assertThat(entity.getDeleted()).isTrue();
         });
+
+        doTest(mapper -> {
+            Entity entity = new Entity();
+            entity.setName("测试根据实体删除");
+            mapper.insert(entity);
+            assertThat(mapper.deleteById(entity)).isEqualTo(1);
+        });
     }
 
     @Override