增加原生批量操作测试用例.

This commit is contained in:
nieqiurong 2024-05-05 21:16:50 +08:00
parent fb0b7edb07
commit 0ae1a9e86b
2 changed files with 33 additions and 7 deletions

View File

@ -16,7 +16,6 @@
package com.baomidou.mybatisplus.core.metadata; package com.baomidou.mybatisplus.core.metadata;
import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlInjectionUtils;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;

View File

@ -107,12 +107,19 @@ class MybatisTest {
@Test @Test
void testBatchAutoCommitFalseOnException1() { void testBatchAutoCommitFalseOnException1() {
List<H2User> userList = List.of(new H2User(1000L, "测试"), new H2User(1000L, "测试")); Assertions.assertThrowsExactly(PersistenceException.class, () -> MybatisBatchUtils.execute(sqlSessionFactory,
Assertions.assertThrowsExactly(PersistenceException.class, () -> MybatisBatchUtils.execute(sqlSessionFactory, userList, H2UserMapper.class.getName() + ".insert")); List.of(new H2User(1000L, "测试"), new H2User(1000L, "测试")), H2UserMapper.class.getName() + ".insert"));
try (var sqlSession = sqlSessionFactory.openSession()) { try (var sqlSession = sqlSessionFactory.openSession()) {
var mapper = sqlSession.getMapper(H2UserMapper.class); var mapper = sqlSession.getMapper(H2UserMapper.class);
Assertions.assertNull(mapper.selectById(1000L)); Assertions.assertNull(mapper.selectById(1000L));
} }
Assertions.assertThrowsExactly(PersistenceException.class, () -> MybatisBatchUtils.execute(sqlSessionFactory,
List.of(new H2User(1001L, "测试"), new H2User(1001L, "测试")),
H2UserMapper.class.getName() + ".insert", 1));
try (var sqlSession = sqlSessionFactory.openSession()) {
var mapper = sqlSession.getMapper(H2UserMapper.class);
Assertions.assertNotNull(mapper.selectById(1001L));
}
} }
@Test @Test
@ -145,18 +152,25 @@ class MybatisTest {
@Test @Test
void testBatchAutoCommitTrueOnException1() { void testBatchAutoCommitTrueOnException1() {
var userList = List.of(new H2User(4000L, "测试"), new H2User(4000L, "测试")); Assertions.assertThrowsExactly(PersistenceException.class, () -> MybatisBatchUtils.execute(sqlSessionFactory,
Assertions.assertThrowsExactly(PersistenceException.class, () -> MybatisBatchUtils.execute(sqlSessionFactory, userList, true, H2UserMapper.class.getName() + ".insert")); List.of(new H2User(4000L, "测试"), new H2User(4000L, "测试")), true, H2UserMapper.class.getName() + ".insert"));
try (var sqlSession = sqlSessionFactory.openSession()) { try (var sqlSession = sqlSessionFactory.openSession()) {
var mapper = sqlSession.getMapper(H2UserMapper.class); var mapper = sqlSession.getMapper(H2UserMapper.class);
Assertions.assertNotNull(mapper.selectById(4000L)); Assertions.assertNotNull(mapper.selectById(4000L));
} }
Assertions.assertThrowsExactly(PersistenceException.class,
() -> MybatisBatchUtils.execute(sqlSessionFactory, List.of(new H2User(4020L, "测试"), new H2User(4020L, "测试"), 1),
true, H2UserMapper.class.getName() + ".insert"));
try (var sqlSession = sqlSessionFactory.openSession()) {
var mapper = sqlSession.getMapper(H2UserMapper.class);
Assertions.assertNotNull(mapper.selectById(4020L));
}
} }
@Test @Test
void testBatchAutoCommitTrueOnException2() { void testBatchAutoCommitTrueOnException2() {
var userList = List.of(new H2User(4010L, "测试"), new H2User(4011L, "测试")); Assertions.assertThrowsExactly(RuntimeException.class, () -> MybatisBatchUtils.execute(sqlSessionFactory,
Assertions.assertThrowsExactly(RuntimeException.class, () -> MybatisBatchUtils.execute(sqlSessionFactory, userList, true, H2UserMapper.class.getName() + ".insert", parameter -> { List.of(new H2User(4010L, "测试"), new H2User(4011L, "测试")), true, H2UserMapper.class.getName() + ".insert", parameter -> {
if (parameter.getTestId() == 4011L) { if (parameter.getTestId() == 4011L) {
throw new RuntimeException("出异常了"); throw new RuntimeException("出异常了");
} }
@ -167,6 +181,19 @@ class MybatisTest {
Assertions.assertNull(mapper.selectById(4010L)); Assertions.assertNull(mapper.selectById(4010L));
Assertions.assertNull(mapper.selectById(4011L)); Assertions.assertNull(mapper.selectById(4011L));
} }
Assertions.assertThrowsExactly(RuntimeException.class, () -> MybatisBatchUtils.execute(sqlSessionFactory,
List.of(new H2User(4015L, "测试"), new H2User(4016L, "测试")), true,
H2UserMapper.class.getName() + ".insert", parameter -> {
if (parameter.getTestId() == 4016L) {
throw new RuntimeException("出异常了");
}
return parameter;
},1));
try (var sqlSession = sqlSessionFactory.openSession()) {
var mapper = sqlSession.getMapper(H2UserMapper.class);
Assertions.assertNotNull(mapper.selectById(4015L));
Assertions.assertNull(mapper.selectById(4016L));
}
} }
} }