|
@@ -22,6 +22,7 @@ import com.baomidou.mybatisplus.core.toolkit.MybatisBatchUtils;
|
|
|
import com.baomidou.mybatisplus.test.h2.entity.H2User;
|
|
|
import com.baomidou.mybatisplus.test.h2.enums.AgeEnum;
|
|
|
import com.baomidou.mybatisplus.test.h2.mapper.H2UserMapper;
|
|
|
+import org.apache.ibatis.exceptions.PersistenceException;
|
|
|
import org.apache.ibatis.io.Resources;
|
|
|
import org.apache.ibatis.jdbc.ScriptRunner;
|
|
|
import org.apache.ibatis.session.Configuration;
|
|
@@ -39,7 +40,6 @@ import java.io.IOException;
|
|
|
import java.io.Reader;
|
|
|
import java.sql.Connection;
|
|
|
import java.sql.SQLException;
|
|
|
-import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@@ -73,56 +73,100 @@ class MybatisTest {
|
|
|
|
|
|
|
|
|
@Test
|
|
|
- void test(){
|
|
|
- SqlSession sqlSession = sqlSessionFactory.openSession(true);
|
|
|
- H2UserMapper mapper = sqlSession.getMapper(H2UserMapper.class);
|
|
|
- Assertions.assertEquals(mapper.myInsertWithNameVersion("test", 2), 1);
|
|
|
- Assertions.assertEquals(mapper.insert(new H2User("test")), 1);
|
|
|
- Assertions.assertEquals(mapper.selectCount(new QueryWrapper<H2User>().lambda().eq(H2User::getName, "test")), 2);
|
|
|
- Assertions.assertEquals(mapper.delete(new QueryWrapper<H2User>().lambda().eq(H2User::getName, "test")), 2);
|
|
|
- H2User h2User = new H2User(66L, "66666", AgeEnum.THREE, 666);
|
|
|
- Assertions.assertEquals(mapper.insert(h2User), 1);
|
|
|
- h2User.setName("7777777777");
|
|
|
- H2User user = mapper.selectById(66L);
|
|
|
- Assertions.assertNotNull(user);
|
|
|
- Assertions.assertEquals(user.getAge(), AgeEnum.THREE);
|
|
|
- Assertions.assertNotNull(user.getTestType());
|
|
|
- Assertions.assertEquals(mapper.updateById(new H2User(66L, "777777")), 1);
|
|
|
- Assertions.assertEquals(mapper.deleteById(66L), 1);
|
|
|
- Assertions.assertNull(mapper.selectById(66L));
|
|
|
- sqlSession.close();
|
|
|
-
|
|
|
- System.out.println("------------------批量测试开始-----------------------------");
|
|
|
- sqlSession = sqlSessionFactory.openSession();
|
|
|
- mapper = sqlSession.getMapper(H2UserMapper.class);
|
|
|
- List<H2User> userList = Arrays.asList(new H2User(1000L, "测试"), new H2User(1001L, "测试"));
|
|
|
- try {
|
|
|
- MybatisBatchUtils.execute(sqlSessionFactory, userList, H2UserMapper.class.getName() + ".insert", parameter -> {
|
|
|
- if (parameter.getTestId() == 1001L) {
|
|
|
- throw new RuntimeException("报错了");
|
|
|
- }
|
|
|
- return parameter;
|
|
|
- });
|
|
|
- } catch (Exception exception) {
|
|
|
+ void test() {
|
|
|
+ try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
|
|
|
+ H2UserMapper mapper = sqlSession.getMapper(H2UserMapper.class);
|
|
|
+ Assertions.assertEquals(mapper.myInsertWithNameVersion("test", 2), 1);
|
|
|
+ Assertions.assertEquals(mapper.insert(new H2User("test")), 1);
|
|
|
+ Assertions.assertEquals(mapper.selectCount(new QueryWrapper<H2User>().lambda().eq(H2User::getName, "test")), 2);
|
|
|
+ Assertions.assertEquals(mapper.delete(new QueryWrapper<H2User>().lambda().eq(H2User::getName, "test")), 2);
|
|
|
+ H2User h2User = new H2User(66L, "66666", AgeEnum.THREE, 666);
|
|
|
+ Assertions.assertEquals(mapper.insert(h2User), 1);
|
|
|
+ h2User.setName("7777777777");
|
|
|
+ H2User user = mapper.selectById(66L);
|
|
|
+ Assertions.assertNotNull(user);
|
|
|
+ Assertions.assertEquals(user.getAge(), AgeEnum.THREE);
|
|
|
+ Assertions.assertNotNull(user.getTestType());
|
|
|
+ Assertions.assertEquals(mapper.updateById(new H2User(66L, "777777")), 1);
|
|
|
+ Assertions.assertEquals(mapper.deleteById(66L), 1);
|
|
|
+ Assertions.assertNull(mapper.selectById(66L));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testBatchAutoCommitFalse() {
|
|
|
+ var userList = List.of(new H2User(2000L, "测试"), new H2User(2001L, "测试"));
|
|
|
+ MybatisBatchUtils.execute(sqlSessionFactory, userList, H2UserMapper.class.getName() + ".insert");
|
|
|
+ try (var sqlSession = sqlSessionFactory.openSession()) {
|
|
|
+ var mapper = sqlSession.getMapper(H2UserMapper.class);
|
|
|
for (H2User u : userList) {
|
|
|
- Assertions.assertNull(mapper.selectById(u.getTestId()));
|
|
|
+ Assertions.assertNotNull(mapper.selectById(u.getTestId()));
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- userList = Arrays.asList(new H2User(2000L, "测试"), new H2User(2001L, "测试"));
|
|
|
- MybatisBatchUtils.execute(sqlSessionFactory, userList, H2UserMapper.class.getName() + ".insert");
|
|
|
- for (H2User u : userList) {
|
|
|
- Assertions.assertNotNull(mapper.selectById(u.getTestId()));
|
|
|
+ @Test
|
|
|
+ void testBatchAutoCommitFalseOnException1() {
|
|
|
+ List<H2User> userList = List.of(new H2User(1000L, "测试"), new H2User(1000L, "测试"));
|
|
|
+ Assertions.assertThrowsExactly(PersistenceException.class, () -> MybatisBatchUtils.execute(sqlSessionFactory, userList, H2UserMapper.class.getName() + ".insert"));
|
|
|
+ try (var sqlSession = sqlSessionFactory.openSession()) {
|
|
|
+ var mapper = sqlSession.getMapper(H2UserMapper.class);
|
|
|
+ Assertions.assertNull(mapper.selectById(1000L));
|
|
|
}
|
|
|
- // 自动提交事务
|
|
|
- userList = Arrays.asList(new H2User(3000L,"测试"),new H2User(3001L,"测试"));
|
|
|
- sqlSession = sqlSessionFactory.openSession();
|
|
|
- mapper = sqlSession.getMapper(H2UserMapper.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testBatchAutoCommitFalseOnException2() {
|
|
|
+ List<H2User> userList = List.of(new H2User(1010L, "测试"), new H2User(1011L, "测试"));
|
|
|
+ Assertions.assertThrowsExactly(RuntimeException.class, () -> MybatisBatchUtils.execute(sqlSessionFactory, userList, H2UserMapper.class.getName() + ".insert", parameter -> {
|
|
|
+ if (parameter.getTestId() == 1011L) {
|
|
|
+ throw new RuntimeException("出异常了");
|
|
|
+ }
|
|
|
+ return parameter;
|
|
|
+ }));
|
|
|
+ try (var sqlSession = sqlSessionFactory.openSession()) {
|
|
|
+ var mapper = sqlSession.getMapper(H2UserMapper.class);
|
|
|
+ Assertions.assertNull(mapper.selectById(1010L));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testBatchAutoCommitTrue() {
|
|
|
+ var userList = List.of(new H2User(3000L, "测试"), new H2User(3001L, "测试"));
|
|
|
MybatisBatchUtils.execute(sqlSessionFactory, userList, true, H2UserMapper.class.getName() + ".insert");
|
|
|
- for (H2User u : userList) {
|
|
|
- Assertions.assertNotNull(mapper.selectById(u.getTestId()));
|
|
|
+ try (var sqlSession = sqlSessionFactory.openSession()) {
|
|
|
+ var mapper = sqlSession.getMapper(H2UserMapper.class);
|
|
|
+ for (H2User u : userList) {
|
|
|
+ Assertions.assertNotNull(mapper.selectById(u.getTestId()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testBatchAutoCommitTrueOnException1() {
|
|
|
+ var userList = List.of(new H2User(4000L, "测试"), new H2User(4000L, "测试"));
|
|
|
+ Assertions.assertThrowsExactly(PersistenceException.class, () -> MybatisBatchUtils.execute(sqlSessionFactory, userList, true, H2UserMapper.class.getName() + ".insert"));
|
|
|
+ try (var sqlSession = sqlSessionFactory.openSession()) {
|
|
|
+ var mapper = sqlSession.getMapper(H2UserMapper.class);
|
|
|
+ Assertions.assertNotNull(mapper.selectById(4000L));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testBatchAutoCommitTrueOnException2() {
|
|
|
+ var userList = List.of(new H2User(4010L, "测试"), new H2User(4011L, "测试"));
|
|
|
+ Assertions.assertThrowsExactly(RuntimeException.class, () -> MybatisBatchUtils.execute(sqlSessionFactory, userList, true, H2UserMapper.class.getName() + ".insert", parameter -> {
|
|
|
+ if (parameter.getTestId() == 4011L) {
|
|
|
+ throw new RuntimeException("出异常了");
|
|
|
+ }
|
|
|
+ return parameter;
|
|
|
+ }));
|
|
|
+ try (var sqlSession = sqlSessionFactory.openSession()) {
|
|
|
+ var mapper = sqlSession.getMapper(H2UserMapper.class);
|
|
|
+ Assertions.assertNull(mapper.selectById(4010L));
|
|
|
+ Assertions.assertNull(mapper.selectById(4011L));
|
|
|
}
|
|
|
- System.out.println("------------------批量测试结束-----------------------------");
|
|
|
}
|
|
|
|
|
|
}
|