情景:处理历史流量数据,由于数据量比较大,可以按照时间或者分页进行分批处理
设计逻辑:定时任务处理前一天数据,分页查询分批处理数据
Boolean haveData = true;
Integer pageNum = 1;
String endDayStr = DateUtil.format(start, Constants.DAY_FORMAT);
String startDayStr = DateUtil.format(DateUtil.offsetDay(start, dayOffset), Constants.DAY_FORMAT);
while (haveData){
Page<dto> detailInfo = null;
detailInfo = service.getInfoByPage(pageNum,
Constants.pageSize,startDayStr, endDayStr);
assert detailInfo != null;
List<dto> dtoList = detailInfo .getRecords();
if(CollUtil.isNotEmpty(dtoList )){
//具体处理逻辑
pageNum++;
}else {
haveData = false;
}
}
}
//这里直接用mybatis plus的分页查询进行处理的,可以自己写个mapper或者直接用LambdaQueryWrapper做条件查询都可以
@Override
public Page<dto> getInfoByPage(Integer pageNum, Integer pageSize, String startDayStr, String endDayStr) {
Page<dto> page = new Page<>(pageNum, pageSize);
baseMapper.getInfoByPage(page,startDayStr,endDayStr);
return page;
}
pom依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
<version>4.3.0</version>
</dependency>