Browse Source

fix: saveOrUpdateBatch使用BatchExecutor.

聂秋秋 6 năm trước cách đây
mục cha
commit
f937972290

+ 4 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/h2/H2MvcCaseTest.java

@@ -254,4 +254,8 @@ public class H2MvcCaseTest extends AbstractH2UserTest {
         userMapper.selectMaps(new EntityWrapper<H2User>());
     }
 
+    @Test
+    public void testInsertOrUpdateBatch(){
+        insertOrUpdateBatchSimpleCase();
+    }
 }

+ 13 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/h2/base/AbstractH2UserTest.java

@@ -372,4 +372,17 @@ public abstract class AbstractH2UserTest extends H2Test {
         }
         System.out.println(pageResult.getTotal());
     }
+    
+    protected void insertOrUpdateBatchSimpleCase() {
+        List<H2User> h2Users = new ArrayList<>();
+        H2User user;
+        for (int i = 0; i < 10; i++) {
+            user = new H2User();
+            user.setAge(1);
+            user.setPrice(new BigDecimal("6" + i));
+            user.setDesc("insertOrUpdateBatch" + i);
+            h2Users.add(user);
+        }
+        Assert.assertTrue(userService.insertOrUpdateBatch(h2Users));
+    }
 }

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

@@ -144,8 +144,6 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
                 }
             }
             batchSqlSession.flushStatements();
-        } catch (Throwable e) {
-            throw new MybatisPlusException("Error: Cannot execute insertBatch Method. Cause", e);
         } finally {
             closeSqlSession(batchSqlSession);
         }
@@ -242,25 +240,38 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         if (CollectionUtils.isEmpty(entityList)) {
             throw new IllegalArgumentException("Error: entityList must not be empty");
         }
-        //todo 这里获取到sqlSessionBatch并无作用,先屏蔽,正确处理方式应该将entityList切割为insert,update两个集合,分别执行批量插入与批量更新.
-//        SqlSession batchSqlSession = sqlSessionBatch();
+        SqlSession batchSqlSession = sqlSessionBatch();
         try {
             int size = entityList.size();
+            Class<?> cls = null;
+            TableInfo tableInfo = null;
             for (int i = 0; i < size; i++) {
-                if (selective) {
-                    insertOrUpdate(entityList.get(i));
+                T entity = entityList.get(i);
+                if(i == 0){
+                    cls = entity.getClass();
+                    tableInfo = TableInfoHelper.getTableInfo(cls);
+                }
+                if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
+                    Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
+                    if (StringUtils.checkValNull(idVal)) {
+                        String sqlStatement = sqlStatement(selective ? SqlMethod.INSERT_ONE : SqlMethod.INSERT_ONE_ALL_COLUMN);
+                        batchSqlSession.insert(sqlStatement,entity);
+                    } else {
+                        String sqlStatement = sqlStatement(selective ? SqlMethod.UPDATE_BY_ID : SqlMethod.UPDATE_ALL_COLUMN_BY_ID);
+                        MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
+                        param.put("et", entity);
+                        batchSqlSession.update(sqlStatement, param);
+                    }
                 } else {
-                    insertOrUpdateAllColumn(entityList.get(i));
+                    throw new MybatisPlusException("Error:  Can not execute. Could not find @TableId.");
+                }
+                if (i >= 1 && i % batchSize == 0) {
+                    batchSqlSession.flushStatements();
                 }
-//                if (i >= 1 && i % batchSize == 0) {
-//                    batchSqlSession.flushStatements();
-//                }
             }
-//            batchSqlSession.flushStatements();
-        } catch (Throwable e) {
-            throw new MybatisPlusException("Error: Cannot execute insertOrUpdateBatch Method. Cause", e);
-        }finally {
-//            closeSqlSession(batchSqlSession);
+            batchSqlSession.flushStatements();
+        } finally {
+            closeSqlSession(batchSqlSession);
         }
         return true;
     }
@@ -366,9 +377,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
                 }
             }
             batchSqlSession.flushStatements();
-        } catch (Throwable e) {
-            throw new MybatisPlusException("Error: Cannot execute updateBatchById Method. Cause", e);
-        }finally {
+        } finally {
             closeSqlSession(batchSqlSession);
         }
         return true;