Преглед на файлове

支持批量删除填充处理.

nieqiurong преди 1 година
родител
ревизия
f16da35bf6

+ 54 - 6
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/mapper/BaseMapper.java

@@ -43,7 +43,9 @@ import org.apache.ibatis.session.SqlSessionFactory;
 
 import java.io.Serializable;
 import java.lang.reflect.Proxy;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.function.BiPredicate;
@@ -125,22 +127,22 @@ public interface BaseMapper<T> extends Mapper<T> {
      * 根据 ID 删除
      *
      * @param useFill 是否填充
-     * @param id      主键ID
+     * @param obj      主键ID或实体
      * @since 3.5.7
      */
-    default int deleteById(Serializable id, boolean useFill) {
+    default int deleteById(Object obj, boolean useFill) {
         Class<?> entityClass = GenericTypeUtils.resolveTypeArguments(getClass(), BaseMapper.class)[0];
-        if (!entityClass.isAssignableFrom(id.getClass()) && useFill) {
+        if (!entityClass.isAssignableFrom(obj.getClass()) && useFill) {
             TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
             if (tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill()) {
                 T instance = tableInfo.newInstance();
-                tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), OgnlOps.convertValue(id, tableInfo.getKeyType()));
+                tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), OgnlOps.convertValue(obj, tableInfo.getKeyType()));
                 return this.deleteById(instance);
             }
         }
         MybatisMapperProxy<?> mybatisMapperProxy = (MybatisMapperProxy<?>) Proxy.getInvocationHandler(this);
         SqlSession sqlSession = mybatisMapperProxy.getSqlSession();
-        return sqlSession.delete(mybatisMapperProxy.getMapperInterface().getName() + Constants.DOT + SqlMethod.DELETE_BY_ID.getMethod(), id);
+        return sqlSession.delete(mybatisMapperProxy.getMapperInterface().getName() + Constants.DOT + SqlMethod.DELETE_BY_ID.getMethod(), obj);
     }
 
     /**
@@ -167,12 +169,58 @@ public interface BaseMapper<T> extends Mapper<T> {
      */
     int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
 
+
+    /**
+     * 删除(根据ID或实体 批量删除)
+     *
+     * @param idList 主键ID列表或实体列表(不能为 null 以及 empty)
+     * @since 3.5.7
+     */
+    default int deleteByIds(@Param(Constants.COLL) Collection<?> idList) {
+        return deleteByIds(idList, true);
+    }
+
+
     /**
      * 删除(根据ID或实体 批量删除)
      *
      * @param idList 主键ID列表或实体列表(不能为 null 以及 empty)
+     * @see #deleteByIds(Collection)
+     * @deprecated 3.5.7
+     */
+    @Deprecated
+    default int deleteBatchIds(@Param(Constants.COLL) Collection<?> idList) {
+        return deleteByIds(idList, true);
+    }
+
+    /**
+     * 删除(根据ID或实体 批量删除)
+     *
+     * @param collections 主键ID列表或实体列表(不能为 null 以及 empty)
+     * @param useFill     逻辑删除下是否填充
+     * @since 3.5.7
      */
-    int deleteBatchIds(@Param(Constants.COLL) Collection<?> idList);
+    default int deleteByIds(@Param(Constants.COLL) Collection<?> collections, boolean useFill) {
+        MybatisMapperProxy<?> mybatisMapperProxy = (MybatisMapperProxy<?>) Proxy.getInvocationHandler(this);
+        Class<?> entityClass = GenericTypeUtils.resolveTypeArguments(getClass(), BaseMapper.class)[0];
+        SqlSession sqlSession = mybatisMapperProxy.getSqlSession();
+        Class<?> mapperInterface = mybatisMapperProxy.getMapperInterface();
+        TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
+        List<Object> ids = new ArrayList<>(collections.size());
+        if (useFill && tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill()) {
+            for (Object obj : collections) {
+                if (entityClass.isAssignableFrom(obj.getClass())) {
+                    ids.add(tableInfo.getPropertyValue(obj, tableInfo.getKeyProperty()));
+                } else {
+                    ids.add(obj);
+                }
+            }
+            this.update(tableInfo.newInstance(), Wrappers.<T>update().in(tableInfo.getKeyColumn(), ids));
+        }
+        Map<String, Object> params = new HashMap<>();
+        params.put(Constants.COLL, collections);
+        return sqlSession.delete(mapperInterface.getName() + StringPool.DOT + SqlMethod.DELETE_BATCH_BY_IDS.getMethod(), params);
+    }
 
     /**
      * 根据 ID 修改

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

@@ -155,7 +155,7 @@ public interface IService<T> {
         if (CollectionUtils.isEmpty(list)) {
             return false;
         }
-        return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list));
+        return SqlHelper.retBool(getBaseMapper().deleteByIds(list));
     }
 
     /**
@@ -174,7 +174,7 @@ public interface IService<T> {
         if (useFill) {
             return removeBatchByIds(list, true);
         }
-        return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list));
+        return SqlHelper.retBool(getBaseMapper().deleteByIds(list));
     }
 
     /**
@@ -208,8 +208,11 @@ public interface IService<T> {
      * @param list      主键ID或实体列表
      * @param batchSize 批次大小
      * @return 删除结果
+     * @see #removeBatchByIds(Collection, boolean)
      * @since 3.5.0
+     * @deprecated 3.5.7
      */
+    @Deprecated
     default boolean removeBatchByIds(Collection<?> list, int batchSize) {
         throw new UnsupportedOperationException("不支持的方法!");
     }
@@ -221,8 +224,11 @@ public interface IService<T> {
      * @param batchSize 批次大小
      * @param useFill   是否启用填充(为true的情况,会将入参转换实体进行delete删除)
      * @return 删除结果
+     * @see #removeBatchByIds(Collection, boolean)
      * @since 3.5.0
+     * @deprecated 3.5.7
      */
+    @Deprecated
     default boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) {
         throw new UnsupportedOperationException("不支持的方法!");
     }

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

@@ -298,10 +298,6 @@ public abstract class ServiceImpl<M extends BaseMapper<T>, T> implements IServic
 
     @Override
     public boolean removeById(Serializable id) {
-        TableInfo tableInfo = TableInfoHelper.getTableInfo(getEntityClass());
-        if (tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill()) {
-            return removeById(id, true);
-        }
         return SqlHelper.retBool(getBaseMapper().deleteById(id));
     }
 
@@ -311,11 +307,7 @@ public abstract class ServiceImpl<M extends BaseMapper<T>, T> implements IServic
         if (CollectionUtils.isEmpty(list)) {
             return false;
         }
-        TableInfo tableInfo = TableInfoHelper.getTableInfo(getEntityClass());
-        if (tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill()) {
-            return removeBatchByIds(list, true);
-        }
-        return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list));
+        return SqlHelper.retBool(getBaseMapper().deleteByIds(list));
     }
 
     @Override
@@ -335,29 +327,13 @@ public abstract class ServiceImpl<M extends BaseMapper<T>, T> implements IServic
     @Override
     @Transactional(rollbackFor = Exception.class)
     public boolean removeBatchByIds(Collection<?> list, int batchSize) {
-        TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
-        return removeBatchByIds(list, batchSize, tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill());
+        return SqlHelper.retBool(getBaseMapper().deleteByIds(list));
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
     public boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) {
-        String sqlStatement = getSqlStatement(SqlMethod.DELETE_BY_ID);
-        TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
-        return executeBatch(list, batchSize, (sqlSession, e) -> {
-            if (useFill && tableInfo.isWithLogicDelete()) {
-                if (entityClass.isAssignableFrom(e.getClass())) {
-                    sqlSession.update(sqlStatement, e);
-                } else {
-                    T instance = tableInfo.newInstance();
-                    Object value = tableInfo.getKeyType() != e.getClass() ? conversionService.convert(e, tableInfo.getKeyType()) : e;
-                    tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), value);
-                    sqlSession.update(sqlStatement, instance);
-                }
-            } else {
-                sqlSession.update(sqlStatement, e);
-            }
-        });
+        return SqlHelper.retBool(getBaseMapper().deleteByIds(list, useFill));
     }
 
 }

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

@@ -313,8 +313,18 @@ class H2UserMapperTest extends BaseTest {
         Assertions.assertEquals(userMapper.selectById(id).getName(), "testSaveOrUpdateBatchMapper4-1");
     }
 
-
-
+    @Test
+    void testRemoveByIds() {
+        Assertions.assertEquals(userMapper.deleteByIds(List.of(666666661, "2")), userMapper.deleteByIds(List.of(666666661, "2"), false));
+        H2User h2User = new H2User("testRemoveByIds");
+        userMapper.insert(h2User);
+        userMapper.deleteByIds(List.of(h2User));
+        Assertions.assertNotNull(userMapper.getById(h2User.getTestId()).getLastUpdatedDt());
+        h2User = new H2User("testRemoveByIds");
+        userMapper.insert(h2User);
+        userMapper.deleteByIds(List.of(h2User), false);
+        Assertions.assertNull(userMapper.getById(h2User.getTestId()).getLastUpdatedDt());
+    }
 
     @Test
     void testSaveOrUpdateBatch2() {