瀏覽代碼

comment https://gitee.com/baomidou/mybatis-plus/issues/IMYWH 临时处理下,暂时还是使用手动开启会话.

聂秋秋 6 年之前
父節點
當前提交
05c0118c95

+ 2 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlHelper.java

@@ -52,7 +52,8 @@ public final class SqlHelper {
      * @return SqlSession
      */
     public static SqlSession sqlSessionBatch(Class<?> clazz) {
-        return SqlSessionUtils.getSqlSession(GlobalConfigUtils.currentSessionFactory(clazz), ExecutorType.BATCH, null);
+        //todo 暂时让能用先,但日志会显示Closing non transactional SqlSession,因为这个并没有绑定.
+        return GlobalConfigUtils.currentSessionFactory(clazz).openSession(ExecutorType.BATCH);
     }
 
     /**

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

@@ -312,4 +312,20 @@ public class H2UserTest extends BaseTest {
         Assert.assertTrue(userService.saveOrUpdateBatch(Arrays.asList(new H2User(1010L,"batch1010"),new H2User("batch1011"),new H2User(1010L,"batch1010"),new H2User("batch1015"))));
         Assert.assertTrue(userService.saveOrUpdateBatch(Arrays.asList(new H2User(1010L,"batch1010A"),new H2User("batch1011A"),new H2User(1010L,"batch1010"),new H2User("batch1016")),1));
     }
+    
+    @Test
+    public void testSimpleAndBatch(){
+        Assert.assertTrue(userService.save(new H2User("testSimpleAndBatch1",0)));
+        Assert.assertTrue(userService.saveOrUpdateBatch(Arrays.asList(new H2User("testSimpleAndBatch2"),new H2User("testSimpleAndBatch3"),new H2User("testSimpleAndBatch4")),1));
+    }
+    
+    @Test
+    public void testSimpleAndBatchTransactional(){
+        try {
+            userService.testSimpleAndBatchTransactional();
+        }catch (MybatisPlusException e){
+            List<H2User> list = userService.list(new QueryWrapper<H2User>().like("name", "simpleAndBatchTx"));
+            Assert.assertTrue(CollectionUtils.isEmpty(list));
+        }
+    }
 }

+ 2 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/service/IH2UserService.java

@@ -53,4 +53,6 @@ public interface IH2UserService extends IService<H2User> {
     void testSimpleTransactional();
     
     void testSaveOrUpdateBatchTransactional();
+    
+    void testSimpleAndBatchTransactional();
 }

+ 9 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/service/impl/H2UserServiceImpl.java

@@ -116,4 +116,13 @@ public class H2UserServiceImpl extends ServiceImpl<H2UserMapper, H2User> impleme
         saveOrUpdateBatch(Arrays.asList(new H2User("savOrUpdate4"),new H2User("savOrUpdate5"),new H2User("savOrUpdate6")),1);
         throw new MybatisPlusException("测试普通插入事务回滚");
     }
+    
+    @Override
+    @Transactional(rollbackFor = RuntimeException.class)
+    public void testSimpleAndBatchTransactional() {
+        save(new H2User("simpleAndBatchTx1",0));
+        saveBatch(Arrays.asList(new H2User("simpleAndBatchTx2"),new H2User("simpleAndBatchTx3"),new H2User("simpleAndBatchTx4")),1);
+        saveOrUpdateBatch(Arrays.asList(new H2User("simpleAndBatchTx5"),new H2User("simpleAndBatchTx6"),new H2User("simpleAndBatchTx7")),1);
+        throw new MybatisPlusException("测试事务回滚");
+    }
 }