MybatisPlus 分批次批量删除数据

MybatisPlus 本身没有这个功能

基于分页实现

private static final int BATCH_SIZE = 1000;

        LambdaQueryWrapper<Record> query = new LambdaQueryWrapper<Record>();
 
        Page<Record> page = new Page<>(1, BATCH_SIZE);

        int total = recordMapper.selectCount(query);
        int pages = (int) Math.ceil((double) total / BATCH_SIZE);

        for (int i = 1; i <= pages; i++) {
            page.setCurrent(i);
            List<Record> records = recordMapper.selectPage(page, query).getRecords();
            // 对这一批数据进行删除操作
            if (!records.isEmpty()) {

        // 获取所有ids
                List<Long> ids = records.stream().map(Record::getId).collect(Collectors.toList());
                recordMapper.deleteBatchIds(ids);
                log.info("deleteBatchIds 已删除:{} 条数据",ids.size());
            
            }
        }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.