Selaa lähdekoodia

修复saveOrUpdateBatch违反唯一约束bug,移除新增api方法。

https://github.com/baomidou/mybatis-plus/issues/2788
nieqiuqiu 4 vuotta sitten
vanhempi
commit
66f4334286

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

@@ -148,14 +148,17 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
         String keyProperty = tableInfo.getKeyProperty();
         Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
-        return SqlHelper.saveOrUpdateBatch(this.entityClass, this.log, entityList, batchSize, entity -> {
+        return SqlHelper.executeBatch(this.entityClass, this.log, entityList, batchSize, ((sqlSession, entity) -> {
             Object idVal = ReflectionKit.getFieldValue(entity, keyProperty);
-            return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal));
-        }, (sqlSession, entity) -> {
-            MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
-            param.put(Constants.ENTITY, entity);
-            sqlSession.update(tableInfo.getSqlStatement(SqlMethod.UPDATE_BY_ID.getMethod()), param);
-        });
+            if (StringUtils.checkValNull(idVal)
+                || CollectionUtils.isEmpty(sqlSession.selectList(tableInfo.getSqlStatement(SqlMethod.SELECT_BY_ID.getMethod()), entity))) {
+                sqlSession.insert(tableInfo.getSqlStatement(SqlMethod.INSERT_ONE.getMethod()), entity);
+            } else {
+                MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
+                param.put(Constants.ENTITY, entity);
+                sqlSession.update(tableInfo.getSqlStatement(SqlMethod.UPDATE_BY_ID.getMethod()), param);
+            }
+        }));
     }
 
     @Transactional(rollbackFor = Exception.class)

+ 0 - 25
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java

@@ -15,7 +15,6 @@
  */
 package com.baomidou.mybatisplus.extension.toolkit;
 
-import com.baomidou.mybatisplus.core.enums.SqlMethod;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
 import com.baomidou.mybatisplus.core.toolkit.Assert;
@@ -37,7 +36,6 @@ import java.util.List;
 import java.util.Objects;
 import java.util.function.BiConsumer;
 import java.util.function.Consumer;
-import java.util.function.Predicate;
 
 /**
  * SQL 辅助类
@@ -220,27 +218,4 @@ public final class SqlHelper {
         });
     }
 
-    /**
-     * 批量更新或保存
-     *
-     * @param entityClass 实体
-     * @param log         日志对象
-     * @param list        数据集合
-     * @param batchSize   批次大小
-     * @param predicate   predicate(新增条件) notNull
-     * @param consumer    consumer(更新处理) notNull
-     * @param <E>         E
-     * @return 操作结果
-     * @since 3.3.3
-     */
-    public static <E> boolean saveOrUpdateBatch(Class<?> entityClass, Log log, Collection<E> list, int batchSize, Predicate<E> predicate, BiConsumer<SqlSession, E> consumer) {
-        TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
-        return executeBatch(entityClass, log, list, batchSize, (sqlSession, entity) -> {
-            if (predicate.test(entity)) {
-                sqlSession.insert(tableInfo.getSqlStatement(SqlMethod.INSERT_ONE.getMethod()), entity);
-            } else {
-                consumer.accept(sqlSession, entity);
-            }
-        });
-    }
 }