fix: 修复员工导入Service层的事务管理和批量插入性能问题
问题1: importEmployeeAsync方法缺少@Transactional注解 - 在第239行添加@Transactional注解,确保异步操作的事务一致性 问题2: saveBatch方法性能问题 - 原实现: 循环内逐条调用insert(),不是真正的批量插入 - 修复方案: 1. 在CcdiEmployeeMapper接口中新增insertBatch方法 2. 在CcdiEmployeeMapper.xml中实现真正的批量插入SQL 3. saveBatch方法改为调用insertBatch,分批次批量插入 性能提升: - 之前: 1000条数据需要1000次数据库往返 - 之后: 1000条数据只需2次数据库往返(分批次500条) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -43,4 +43,12 @@ public interface CcdiEmployeeMapper extends BaseMapper<CcdiEmployee> {
|
|||||||
* @return 影响行数
|
* @return 影响行数
|
||||||
*/
|
*/
|
||||||
int insertOrUpdateBatch(@Param("list") List<CcdiEmployee> list);
|
int insertOrUpdateBatch(@Param("list") List<CcdiEmployee> list);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量插入员工信息
|
||||||
|
*
|
||||||
|
* @param list 员工信息列表
|
||||||
|
* @return 影响行数
|
||||||
|
*/
|
||||||
|
int insertBatch(@Param("list") List<CcdiEmployee> list);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -236,6 +236,7 @@ public class CcdiEmployeeServiceImpl implements ICcdiEmployeeService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Async("importExecutor")
|
@Async("importExecutor")
|
||||||
|
@Transactional
|
||||||
public CompletableFuture<ImportResultVO> importEmployeeAsync(List<CcdiEmployeeExcel> excelList, Boolean isUpdateSupport) {
|
public CompletableFuture<ImportResultVO> importEmployeeAsync(List<CcdiEmployeeExcel> excelList, Boolean isUpdateSupport) {
|
||||||
String taskId = UUID.randomUUID().toString();
|
String taskId = UUID.randomUUID().toString();
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
@@ -531,12 +532,11 @@ public class CcdiEmployeeServiceImpl implements ICcdiEmployeeService {
|
|||||||
* 批量保存
|
* 批量保存
|
||||||
*/
|
*/
|
||||||
private void saveBatch(List<CcdiEmployee> list, int batchSize) {
|
private void saveBatch(List<CcdiEmployee> list, int batchSize) {
|
||||||
|
// 使用真正的批量插入,分批次执行以提高性能
|
||||||
for (int i = 0; i < list.size(); i += batchSize) {
|
for (int i = 0; i < list.size(); i += batchSize) {
|
||||||
int end = Math.min(i + batchSize, list.size());
|
int end = Math.min(i + batchSize, list.size());
|
||||||
List<CcdiEmployee> subList = list.subList(i, end);
|
List<CcdiEmployee> subList = list.subList(i, end);
|
||||||
for (CcdiEmployee employee : subList) {
|
employeeMapper.insertBatch(subList);
|
||||||
employeeMapper.insert(employee);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,4 +66,17 @@
|
|||||||
update_time = NOW()
|
update_time = NOW()
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
|
<!-- 批量插入员工信息 -->
|
||||||
|
<insert id="insertBatch" parameterType="java.util.List">
|
||||||
|
INSERT INTO ccdi_employee
|
||||||
|
(employee_id, name, dept_id, id_card, phone, hire_date, status,
|
||||||
|
create_time, create_by, update_by, update_time)
|
||||||
|
VALUES
|
||||||
|
<foreach collection="list" item="item" separator=",">
|
||||||
|
(#{item.employeeId}, #{item.name}, #{item.deptId}, #{item.idCard},
|
||||||
|
#{item.phone}, #{item.hireDate}, #{item.status}, NOW(),
|
||||||
|
#{item.createBy}, #{item.updateBy}, NOW())
|
||||||
|
</foreach>
|
||||||
|
</insert>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
Reference in New Issue
Block a user