新增一个批量工具类.
This commit is contained in:
parent
7487b076f8
commit
4b82909f90
@ -1,3 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2011-2023, baomidou (jobob@qq.com).
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package com.baomidou.mybatisplus.core.batch;
|
package com.baomidou.mybatisplus.core.batch;
|
||||||
|
|
||||||
import org.apache.ibatis.mapping.MappedStatement;
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
|
@ -1,3 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2011-2023, baomidou (jobob@qq.com).
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package com.baomidou.mybatisplus.core.batch;
|
package com.baomidou.mybatisplus.core.batch;
|
||||||
|
|
||||||
import org.apache.ibatis.executor.BatchResult;
|
import org.apache.ibatis.executor.BatchResult;
|
||||||
|
@ -1,3 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2011-2023, baomidou (jobob@qq.com).
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package com.baomidou.mybatisplus.core.batch;
|
package com.baomidou.mybatisplus.core.batch;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||||
|
@ -1,3 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2011-2023, baomidou (jobob@qq.com).
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package com.baomidou.mybatisplus.core.batch;
|
package com.baomidou.mybatisplus.core.batch;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -0,0 +1,149 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2011-2023, baomidou (jobob@qq.com).
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.baomidou.mybatisplus.core.toolkit;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.batch.BatchMethod;
|
||||||
|
import com.baomidou.mybatisplus.core.batch.BatchSqlSession;
|
||||||
|
import com.baomidou.mybatisplus.core.batch.MybatisBatch;
|
||||||
|
import com.baomidou.mybatisplus.core.batch.ParameterConvert;
|
||||||
|
import org.apache.ibatis.executor.BatchResult;
|
||||||
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.BiPredicate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author nieqiurong
|
||||||
|
* @since 3.5.4
|
||||||
|
*/
|
||||||
|
public class MybatisBatchUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行批量操作
|
||||||
|
*
|
||||||
|
* @param sqlSessionFactory {@link SqlSessionFactory}
|
||||||
|
* @param dataList 数据集列表
|
||||||
|
* @param statement 执行的 mapper 方法 (示例: com.baomidou.mybatisplus.core.mapper.BaseMapper.insert )
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return 批处理结果
|
||||||
|
*/
|
||||||
|
public static <T> List<BatchResult> execute(SqlSessionFactory sqlSessionFactory, List<T> dataList, String statement) {
|
||||||
|
return new MybatisBatch<>(sqlSessionFactory, dataList).execute(statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行批量操作
|
||||||
|
*
|
||||||
|
* @param sqlSessionFactory {@link SqlSessionFactory}
|
||||||
|
* @param dataList 数据集列表
|
||||||
|
* @param statement 执行的 mapper 方法 (示例: com.baomidou.mybatisplus.core.mapper.BaseMapper.insert )
|
||||||
|
* @param parameterConvert 参数转换器
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return 批处理结果
|
||||||
|
*/
|
||||||
|
public static <T> List<BatchResult> execute(SqlSessionFactory sqlSessionFactory, List<T> dataList, String statement, ParameterConvert<T> parameterConvert) {
|
||||||
|
return new MybatisBatch<>(sqlSessionFactory, dataList).execute(statement, parameterConvert);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行批量操作
|
||||||
|
*
|
||||||
|
* @param sqlSessionFactory {@link SqlSessionFactory}
|
||||||
|
* @param dataList 数据集列表
|
||||||
|
* @param autoCommit 是否自动提交(这里生效的前提依赖于事务管理器 {@link org.apache.ibatis.transaction.Transaction})
|
||||||
|
* @param statement 执行的 mapper 方法 (示例: com.baomidou.mybatisplus.core.mapper.BaseMapper.insert )
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return 批处理结果
|
||||||
|
*/
|
||||||
|
public static <T> List<BatchResult> execute(SqlSessionFactory sqlSessionFactory, List<T> dataList, boolean autoCommit, String statement) {
|
||||||
|
return new MybatisBatch<>(sqlSessionFactory, dataList).execute(autoCommit, statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行批量操作
|
||||||
|
*
|
||||||
|
* @param sqlSessionFactory {@link SqlSessionFactory}
|
||||||
|
* @param dataList 数据集列表
|
||||||
|
* @param autoCommit 是否自动提交(这里生效的前提依赖于事务管理器 {@link org.apache.ibatis.transaction.Transaction})
|
||||||
|
* @param statement 执行的 mapper 方法 (示例: com.baomidou.mybatisplus.core.mapper.BaseMapper.insert )
|
||||||
|
* @param parameterConvert 参数转换器
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return 批处理结果
|
||||||
|
*/
|
||||||
|
public static <T> List<BatchResult> execute(SqlSessionFactory sqlSessionFactory, List<T> dataList, boolean autoCommit, String statement, ParameterConvert<T> parameterConvert) {
|
||||||
|
return new MybatisBatch<>(sqlSessionFactory, dataList).execute(autoCommit, statement, parameterConvert);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行批量操作
|
||||||
|
*
|
||||||
|
* @param sqlSessionFactory sqlSessionFactory {@link SqlSessionFactory}
|
||||||
|
* @param dataList 数据集列表
|
||||||
|
* @param batchMethod 批量操作方法
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return 批处理结果
|
||||||
|
*/
|
||||||
|
public static <T> List<BatchResult> execute(SqlSessionFactory sqlSessionFactory, List<T> dataList, BatchMethod<T> batchMethod) {
|
||||||
|
return new MybatisBatch<>(sqlSessionFactory, dataList).execute(batchMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行批量操作
|
||||||
|
*
|
||||||
|
* @param sqlSessionFactory sqlSessionFactory {@link SqlSessionFactory}
|
||||||
|
* @param dataList 数据集列表
|
||||||
|
* @param autoCommit 是否自动提交(这里生效的前提依赖于事务管理器 {@link org.apache.ibatis.transaction.Transaction})
|
||||||
|
* @param batchMethod 批量操作方法
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return 批处理结果
|
||||||
|
*/
|
||||||
|
public static <T> List<BatchResult> execute(SqlSessionFactory sqlSessionFactory, List<T> dataList, boolean autoCommit, BatchMethod<T> batchMethod) {
|
||||||
|
return new MybatisBatch<>(sqlSessionFactory, dataList).execute(autoCommit, batchMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量保存或更新
|
||||||
|
*
|
||||||
|
* @param sqlSessionFactory sqlSessionFactory {@link SqlSessionFactory}
|
||||||
|
* @param dataList 数据集列表
|
||||||
|
* @param insertMethod 插入方法
|
||||||
|
* @param insertPredicate 插入条件 (当条件满足时执行插入方法,否则执行更新方法)
|
||||||
|
* @param updateMethod 更新方法
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return 批处理结果
|
||||||
|
*/
|
||||||
|
public static <T> List<BatchResult> saveOrUpdate(SqlSessionFactory sqlSessionFactory, List<T> dataList, BatchMethod<T> insertMethod, BiPredicate<BatchSqlSession, T> insertPredicate, BatchMethod<T> updateMethod) {
|
||||||
|
return new MybatisBatch<>(sqlSessionFactory, dataList).saveOrUpdate(insertMethod, insertPredicate, updateMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量保存或更新
|
||||||
|
*
|
||||||
|
* @param sqlSessionFactory sqlSessionFactory {@link SqlSessionFactory}
|
||||||
|
* @param dataList 数据集列表
|
||||||
|
* @param autoCommit 是否自动提交(这里生效的前提依赖于事务管理器 {@link org.apache.ibatis.transaction.Transaction})
|
||||||
|
* @param insertMethod 插入方法
|
||||||
|
* @param insertPredicate 插入条件 (当条件满足时执行插入方法,否则执行更新方法)
|
||||||
|
* @param updateMethod 更新方法
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return 批处理结果
|
||||||
|
*/
|
||||||
|
public static <T> List<BatchResult> saveOrUpdate(SqlSessionFactory sqlSessionFactory, List<T> dataList, boolean autoCommit, BatchMethod<T> insertMethod, BiPredicate<BatchSqlSession, T> insertPredicate, BatchMethod<T> updateMethod) {
|
||||||
|
return new MybatisBatch<>(sqlSessionFactory, dataList).saveOrUpdate(autoCommit, insertMethod, insertPredicate, updateMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -16,9 +16,9 @@
|
|||||||
package com.baomidou.mybatisplus.test;
|
package com.baomidou.mybatisplus.test;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
|
import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
|
||||||
import com.baomidou.mybatisplus.core.batch.MybatisBatch;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler;
|
import com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.MybatisBatchUtils;
|
||||||
import com.baomidou.mybatisplus.test.h2.entity.H2User;
|
import com.baomidou.mybatisplus.test.h2.entity.H2User;
|
||||||
import com.baomidou.mybatisplus.test.h2.enums.AgeEnum;
|
import com.baomidou.mybatisplus.test.h2.enums.AgeEnum;
|
||||||
import com.baomidou.mybatisplus.test.h2.mapper.H2UserMapper;
|
import com.baomidou.mybatisplus.test.h2.mapper.H2UserMapper;
|
||||||
@ -97,7 +97,7 @@ class MybatisTest {
|
|||||||
mapper = sqlSession.getMapper(H2UserMapper.class);
|
mapper = sqlSession.getMapper(H2UserMapper.class);
|
||||||
List<H2User> userList = Arrays.asList(new H2User(1000L, "测试"), new H2User(1001L, "测试"));
|
List<H2User> userList = Arrays.asList(new H2User(1000L, "测试"), new H2User(1001L, "测试"));
|
||||||
try {
|
try {
|
||||||
new MybatisBatch<>(sqlSessionFactory, userList).execute(H2UserMapper.class.getName() + ".insert", parameter -> {
|
MybatisBatchUtils.execute(sqlSessionFactory, userList, H2UserMapper.class.getName() + ".insert", parameter -> {
|
||||||
if (parameter.getTestId() == 1001L) {
|
if (parameter.getTestId() == 1001L) {
|
||||||
throw new RuntimeException("报错了");
|
throw new RuntimeException("报错了");
|
||||||
}
|
}
|
||||||
@ -110,7 +110,7 @@ class MybatisTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
userList = Arrays.asList(new H2User(2000L, "测试"), new H2User(2001L, "测试"));
|
userList = Arrays.asList(new H2User(2000L, "测试"), new H2User(2001L, "测试"));
|
||||||
new MybatisBatch<>(sqlSessionFactory, userList).execute(H2UserMapper.class.getName() + ".insert");
|
MybatisBatchUtils.execute(sqlSessionFactory, userList, H2UserMapper.class.getName() + ".insert");
|
||||||
for (H2User u : userList) {
|
for (H2User u : userList) {
|
||||||
Assertions.assertNotNull(mapper.selectById(u.getTestId()));
|
Assertions.assertNotNull(mapper.selectById(u.getTestId()));
|
||||||
}
|
}
|
||||||
@ -118,7 +118,7 @@ class MybatisTest {
|
|||||||
userList = Arrays.asList(new H2User(3000L,"测试"),new H2User(3001L,"测试"));
|
userList = Arrays.asList(new H2User(3000L,"测试"),new H2User(3001L,"测试"));
|
||||||
sqlSession = sqlSessionFactory.openSession();
|
sqlSession = sqlSessionFactory.openSession();
|
||||||
mapper = sqlSession.getMapper(H2UserMapper.class);
|
mapper = sqlSession.getMapper(H2UserMapper.class);
|
||||||
new MybatisBatch<>(sqlSessionFactory, userList).execute(true, H2UserMapper.class.getName() + ".insert");
|
MybatisBatchUtils.execute(sqlSessionFactory, userList, true, H2UserMapper.class.getName() + ".insert");
|
||||||
for (H2User u : userList) {
|
for (H2User u : userList) {
|
||||||
Assertions.assertNotNull(mapper.selectById(u.getTestId()));
|
Assertions.assertNotNull(mapper.selectById(u.getTestId()));
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|||||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.MybatisBatchUtils;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.test.h2.entity.H2User;
|
import com.baomidou.mybatisplus.test.h2.entity.H2User;
|
||||||
@ -75,7 +76,7 @@ class H2UserMapperTest extends BaseTest {
|
|||||||
transactionTemplate.execute((TransactionCallback<List<BatchResult>>) status -> {
|
transactionTemplate.execute((TransactionCallback<List<BatchResult>>) status -> {
|
||||||
MybatisBatch.Method<H2User> mapperMethod = new MybatisBatch.Method<>(H2UserMapper.class);
|
MybatisBatch.Method<H2User> mapperMethod = new MybatisBatch.Method<>(H2UserMapper.class);
|
||||||
// 执行批量插入
|
// 执行批量插入
|
||||||
new MybatisBatch<>(sqlSessionFactory, h2UserList).execute(mapperMethod.insert());
|
MybatisBatchUtils.execute(sqlSessionFactory, h2UserList, mapperMethod.insert());
|
||||||
throw new RuntimeException("出错了");
|
throw new RuntimeException("出错了");
|
||||||
});
|
});
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
@ -86,7 +87,7 @@ class H2UserMapperTest extends BaseTest {
|
|||||||
transactionTemplate.execute(status -> {
|
transactionTemplate.execute(status -> {
|
||||||
MybatisBatch.Method<H2User> mapperMethod = new MybatisBatch.Method<>(H2UserMapper.class);
|
MybatisBatch.Method<H2User> mapperMethod = new MybatisBatch.Method<>(H2UserMapper.class);
|
||||||
// 执行批量插入
|
// 执行批量插入
|
||||||
return new MybatisBatch<>(sqlSessionFactory, h2UserList).execute(mapperMethod.insert());
|
return MybatisBatchUtils.execute(sqlSessionFactory, h2UserList, mapperMethod.insert());
|
||||||
});
|
});
|
||||||
for (H2User h2User : h2UserList) {
|
for (H2User h2User : h2UserList) {
|
||||||
Assertions.assertNotNull(userMapper.selectById(h2User.getTestId()));
|
Assertions.assertNotNull(userMapper.selectById(h2User.getTestId()));
|
||||||
@ -102,7 +103,7 @@ class H2UserMapperTest extends BaseTest {
|
|||||||
}
|
}
|
||||||
MybatisBatch.Method<H2User> mapperMethod = new MybatisBatch.Method<>(H2UserMapper.class);
|
MybatisBatch.Method<H2User> mapperMethod = new MybatisBatch.Method<>(H2UserMapper.class);
|
||||||
// 执行批量插入
|
// 执行批量插入
|
||||||
List<BatchResult> batchResults = new MybatisBatch<>(sqlSessionFactory, h2UserList).execute(mapperMethod.insert());
|
List<BatchResult> batchResults = MybatisBatchUtils.execute(sqlSessionFactory, h2UserList, mapperMethod.insert());
|
||||||
int[] updateCounts = batchResults.get(0).getUpdateCounts();
|
int[] updateCounts = batchResults.get(0).getUpdateCounts();
|
||||||
Assertions.assertEquals(batchSize, updateCounts.length);
|
Assertions.assertEquals(batchSize, updateCounts.length);
|
||||||
for (int updateCount : updateCounts) {
|
for (int updateCount : updateCounts) {
|
||||||
@ -111,7 +112,7 @@ class H2UserMapperTest extends BaseTest {
|
|||||||
|
|
||||||
List<Long> ids = Arrays.asList(120000L, 120001L);
|
List<Long> ids = Arrays.asList(120000L, 120001L);
|
||||||
MybatisBatch.Method<H2User> method = new MybatisBatch.Method<>(H2UserMapper.class);
|
MybatisBatch.Method<H2User> method = new MybatisBatch.Method<>(H2UserMapper.class);
|
||||||
new MybatisBatch<>(sqlSessionFactory, ids).execute(method.insert(H2User::ofId));
|
MybatisBatchUtils.execute(sqlSessionFactory, ids, method.insert(H2User::ofId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -125,7 +126,7 @@ class H2UserMapperTest extends BaseTest {
|
|||||||
}
|
}
|
||||||
MybatisBatch.Method<H2User> method = new MybatisBatch.Method<>(H2UserMapper.class);
|
MybatisBatch.Method<H2User> method = new MybatisBatch.Method<>(H2UserMapper.class);
|
||||||
// 执行批量插入
|
// 执行批量插入
|
||||||
batchResults = new MybatisBatch<>(sqlSessionFactory, h2UserList).execute(method.get("myInsertWithoutParam"));
|
batchResults = MybatisBatchUtils.execute(sqlSessionFactory, h2UserList, method.get("myInsertWithoutParam"));
|
||||||
updateCounts = batchResults.get(0).getUpdateCounts();
|
updateCounts = batchResults.get(0).getUpdateCounts();
|
||||||
Assertions.assertEquals(batchSize, updateCounts.length);
|
Assertions.assertEquals(batchSize, updateCounts.length);
|
||||||
for (int updateCount : updateCounts) {
|
for (int updateCount : updateCounts) {
|
||||||
@ -137,7 +138,7 @@ class H2UserMapperTest extends BaseTest {
|
|||||||
h2UserList.add(new H2User("myInsertWithParam" + i));
|
h2UserList.add(new H2User("myInsertWithParam" + i));
|
||||||
}
|
}
|
||||||
// 执行批量插入
|
// 执行批量插入
|
||||||
batchResults = new MybatisBatch<>(sqlSessionFactory, h2UserList).execute(method.get("myInsertWithParam", parameter -> Map.of("user1", parameter)));
|
batchResults = MybatisBatchUtils.execute(sqlSessionFactory, h2UserList, method.get("myInsertWithParam", parameter -> Map.of("user1", parameter)));
|
||||||
updateCounts = batchResults.get(0).getUpdateCounts();
|
updateCounts = batchResults.get(0).getUpdateCounts();
|
||||||
Assertions.assertEquals(batchSize, updateCounts.length);
|
Assertions.assertEquals(batchSize, updateCounts.length);
|
||||||
for (int updateCount : updateCounts) {
|
for (int updateCount : updateCounts) {
|
||||||
@ -148,7 +149,7 @@ class H2UserMapperTest extends BaseTest {
|
|||||||
for (int i = 0; i < batchSize; i++) {
|
for (int i = 0; i < batchSize; i++) {
|
||||||
h2UserList.add(new H2User("myInsertWithParam" + i));
|
h2UserList.add(new H2User("myInsertWithParam" + i));
|
||||||
}
|
}
|
||||||
batchResults = new MybatisBatch<>(sqlSessionFactory, h2UserList).execute(method.get("myInsertWithParam", parameter -> Map.of("user1", parameter)));
|
batchResults = MybatisBatchUtils.execute(sqlSessionFactory, h2UserList, method.get("myInsertWithParam", parameter -> Map.of("user1", parameter)));
|
||||||
updateCounts = batchResults.get(0).getUpdateCounts();
|
updateCounts = batchResults.get(0).getUpdateCounts();
|
||||||
Assertions.assertEquals(batchSize, updateCounts.length);
|
Assertions.assertEquals(batchSize, updateCounts.length);
|
||||||
for (int updateCount : updateCounts) {
|
for (int updateCount : updateCounts) {
|
||||||
@ -167,7 +168,7 @@ class H2UserMapperTest extends BaseTest {
|
|||||||
List<H2User> userList = new ArrayList<>();
|
List<H2User> userList = new ArrayList<>();
|
||||||
MybatisBatch.Method<H2User> method = new MybatisBatch.Method<>(H2UserMapper.class);
|
MybatisBatch.Method<H2User> method = new MybatisBatch.Method<>(H2UserMapper.class);
|
||||||
// 转换成实体进行逻辑删除
|
// 转换成实体进行逻辑删除
|
||||||
batchResults = new MybatisBatch<>(sqlSessionFactory, ids).execute(method.deleteById(id -> {
|
batchResults = MybatisBatchUtils.execute(sqlSessionFactory, ids, method.deleteById(id -> {
|
||||||
H2User h2User = H2User.ofId(id);
|
H2User h2User = H2User.ofId(id);
|
||||||
userList.add(h2User);
|
userList.add(h2User);
|
||||||
return h2User;
|
return h2User;
|
||||||
@ -181,7 +182,7 @@ class H2UserMapperTest extends BaseTest {
|
|||||||
Assertions.assertNotNull(h2User.getLastUpdatedDt());
|
Assertions.assertNotNull(h2User.getLastUpdatedDt());
|
||||||
}
|
}
|
||||||
// 不能走填充
|
// 不能走填充
|
||||||
batchResults = new MybatisBatch<>(sqlSessionFactory, ids).execute(method.deleteById());
|
batchResults = MybatisBatchUtils.execute(sqlSessionFactory, ids, method.deleteById());
|
||||||
updateCounts = batchResults.get(0).getUpdateCounts();
|
updateCounts = batchResults.get(0).getUpdateCounts();
|
||||||
for (int updateCount : updateCounts) {
|
for (int updateCount : updateCounts) {
|
||||||
Assertions.assertEquals(0, updateCount);
|
Assertions.assertEquals(0, updateCount);
|
||||||
@ -197,7 +198,7 @@ class H2UserMapperTest extends BaseTest {
|
|||||||
}
|
}
|
||||||
MybatisBatch.Method<H2User> mapperMethod = new MybatisBatch.Method<>(H2UserMapper.class);
|
MybatisBatch.Method<H2User> mapperMethod = new MybatisBatch.Method<>(H2UserMapper.class);
|
||||||
// 执行批量更新
|
// 执行批量更新
|
||||||
List<BatchResult> batchResults = new MybatisBatch<>(sqlSessionFactory, h2UserList).execute(mapperMethod.updateById());
|
List<BatchResult> batchResults = MybatisBatchUtils.execute(sqlSessionFactory, h2UserList, mapperMethod.updateById());
|
||||||
int[] updateCounts = batchResults.get(0).getUpdateCounts();
|
int[] updateCounts = batchResults.get(0).getUpdateCounts();
|
||||||
Assertions.assertEquals(batchSize, updateCounts.length);
|
Assertions.assertEquals(batchSize, updateCounts.length);
|
||||||
for (int updateCount : updateCounts) {
|
for (int updateCount : updateCounts) {
|
||||||
@ -207,11 +208,11 @@ class H2UserMapperTest extends BaseTest {
|
|||||||
List<Long> ids = Arrays.asList(120000L, 120001L);
|
List<Long> ids = Arrays.asList(120000L, 120001L);
|
||||||
MybatisBatch.Method<H2User> method = new MybatisBatch.Method<>(H2UserMapper.class);
|
MybatisBatch.Method<H2User> method = new MybatisBatch.Method<>(H2UserMapper.class);
|
||||||
|
|
||||||
new MybatisBatch<>(sqlSessionFactory, ids).execute(method.update(id -> Wrappers.<H2User>lambdaUpdate().set(H2User::getName, "updateTest").eq(H2User::getTestId, id)));
|
MybatisBatchUtils.execute(sqlSessionFactory, ids, method.update(id -> Wrappers.<H2User>lambdaUpdate().set(H2User::getName, "updateTest").eq(H2User::getTestId, id)));
|
||||||
new MybatisBatch<>(sqlSessionFactory, ids).execute(method.update(id -> new H2User().setName("updateTest2"), id -> Wrappers.<H2User>lambdaUpdate().eq(H2User::getTestId, id)));
|
MybatisBatchUtils.execute(sqlSessionFactory, ids, method.update(id -> new H2User().setName("updateTest2"), id -> Wrappers.<H2User>lambdaUpdate().eq(H2User::getTestId, id)));
|
||||||
|
|
||||||
new MybatisBatch<>(sqlSessionFactory, h2UserList).execute(method.update(user -> Wrappers.<H2User>update().set("name", "updateTest3").eq("test_id", user.getTestId())));
|
MybatisBatchUtils.execute(sqlSessionFactory, h2UserList, method.update(user -> Wrappers.<H2User>update().set("name", "updateTest3").eq("test_id", user.getTestId())));
|
||||||
new MybatisBatch<>(sqlSessionFactory, h2UserList).execute(method.update(user -> new H2User("updateTests4"), p -> Wrappers.<H2User>update().eq("test_id", p.getTestId())));
|
MybatisBatchUtils.execute(sqlSessionFactory, h2UserList, method.update(user -> new H2User("updateTests4"), p -> Wrappers.<H2User>update().eq("test_id", p.getTestId())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -222,7 +223,7 @@ class H2UserMapperTest extends BaseTest {
|
|||||||
h2UserList.add(new H2User(Long.valueOf(40000 + i), "test" + i));
|
h2UserList.add(new H2User(Long.valueOf(40000 + i), "test" + i));
|
||||||
}
|
}
|
||||||
MybatisBatch.Method<H2User> mapperMethod = new MybatisBatch.Method<>(H2UserMapper.class);
|
MybatisBatch.Method<H2User> mapperMethod = new MybatisBatch.Method<>(H2UserMapper.class);
|
||||||
List<BatchResult> batchResults = new MybatisBatch<>(sqlSessionFactory, h2UserList).saveOrUpdate(
|
List<BatchResult> batchResults = MybatisBatchUtils.saveOrUpdate(sqlSessionFactory, h2UserList,
|
||||||
mapperMethod.insert(),
|
mapperMethod.insert(),
|
||||||
((sqlSession, h2User) -> userMapper.selectById(h2User.getTestId()) == null),
|
((sqlSession, h2User) -> userMapper.selectById(h2User.getTestId()) == null),
|
||||||
mapperMethod.updateById());
|
mapperMethod.updateById());
|
||||||
@ -242,7 +243,7 @@ class H2UserMapperTest extends BaseTest {
|
|||||||
h2UserList.add(new H2User(Long.valueOf(50000 + i), "test" + i));
|
h2UserList.add(new H2User(Long.valueOf(50000 + i), "test" + i));
|
||||||
}
|
}
|
||||||
MybatisBatch.Method<H2User> mapperMethod = new MybatisBatch.Method<>(H2UserMapper.class);
|
MybatisBatch.Method<H2User> mapperMethod = new MybatisBatch.Method<>(H2UserMapper.class);
|
||||||
List<BatchResult> batchResults = new MybatisBatch<>(sqlSessionFactory, h2UserList).saveOrUpdate(
|
List<BatchResult> batchResults = MybatisBatchUtils.saveOrUpdate(sqlSessionFactory, h2UserList,
|
||||||
mapperMethod.insert(),
|
mapperMethod.insert(),
|
||||||
((sqlSession, h2User) -> sqlSession.selectList(H2UserMapper.class.getName() + ".selectById", h2User.getTestId()).isEmpty()),
|
((sqlSession, h2User) -> sqlSession.selectList(H2UserMapper.class.getName() + ".selectById", h2User.getTestId()).isEmpty()),
|
||||||
mapperMethod.updateById());
|
mapperMethod.updateById());
|
||||||
|
@ -1,3 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2011-2023, baomidou (jobob@qq.com).
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package com.baomidou.mybatisplus.autoconfigure;
|
package com.baomidou.mybatisplus.autoconfigure;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user