SpringBoot 使用线程池批量插入数据

SpringBoot 使用线程池批量插入数据

  • 服务主体逻辑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 初始化线程
ExecutorService executorService= Executors.newFixedThreadPool(16);

List<DutyRecordThread> list= Lists.newLinkedList();

for (Date groupDate : groupDateList) {
List<DutyRecord> dutyRecordList = new ArrayList<>();
teacherIds.forEach(teacherId -> {
DutyRecord dutyRecord = DutyRecord.builder()
.id(UUID.randomUUID().toString().replace("-", ""))
.schoolId(dutyGroup.getSchoolId())
.dutyId(duty.getId())
.dutyName(duty.getName())
.dutyRemark(duty.getRemark())
.dutyGroupId(dutyGroup.getId())
.dutyGroupName(dutyGroup.getName())
.dutyGroupRemark(dutyGroup.getRemark())
.teacherId(teacherId)
.day(groupDate)
.startTime(duty.getStartTime())
.endTime(duty.getEndTime())
.createBy(dutyGroupDTO.getCreateBy())
.build();
dutyRecordList.add(dutyRecord);
});
list.add(new DutyRecordThread(dutyRecordList));
}

// 执行
try{
executorService.invokeAll(list);
} catch (Exception e) {
throw new ApiException(e.getMessage(), 500);

}
  • 线程类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public class DutyRecordThread implements Callable<Boolean> {
    private List<DutyRecord> data;

    public DutyRecordThread(List<DutyRecord> data) {
    this.data = data;
    }

    @Override
    public Boolean call() {
    System.out.println("线程" + Thread.currentThread().getName() + "正在执行-----------------------");
    insertBatchData(data);
    return true;
    }
    }
  • 执行的插入逻辑

    1
    2
    3
    4
    5
    6
    7
    8
    9
    //TODO:批量插入数据
    @Async
    @Override
    public int insertBatchData(List<DutyRecord> dutyRecordList) {
    //这是一个批量插入的方法

    return dutyRecordDao.insertBatch(dutyRecordList);

    }
Donate comment here