- 这是一个
游标
的概念,当获取大量数据的时候则使用Cursor
存储,不用List
存储 - 需要配合
@Transactional
使用才生效 - 业务场景:搜索数据库大量数据出来,然后遍历
Cursor
将数据写入并生成指定文件
@Transactional
public String export(InteractiveReplyStatistics interactiveReplyStatistics, User user, List<Integer> userIdsByRole) {
ExcelWriter writer = null;
String filename = IdUtil.fastSimpleUUID() + ".xlsx";
String filepath = localStoreDirConfig.getLocalExportFileStoreDirPath() + "/" + filename;
try {
int count = interactiveReplyStatisticsMapper.countByAllGroupByBatchNo(interactiveReplyStatistics, userIdsByRole);
if (count > Symbols.API_MAX_EXPORT) {
return null;
}
Cursor<InteractiveReplyStatistics> cursor = interactiveReplyStatisticsMapper.steamByAllGroupByBatchNo(interactiveReplyStatistics, userIdsByRole);
writer = ExcelUtil.getBigWriter(filepath);
ArrayList<String> temp = new ArrayList(){{
add("日期");
add("批次号");
add("提交人");
add("短信标题");
add("回复应答码");
add("回复条数");
}};
writer.writeHeadRow(temp);
Iterator<InteractiveReplyStatistics> iterator = cursor.iterator();
while (iterator.hasNext()) {
InteractiveReplyStatistics o = iterator.next();
if (o == null) {
break;
}
temp.clear();
temp.add(DateUtil.formatDate(o.getStatTime()));
temp.add(o.getAncestorBatchNo());
temp.add(o.getUserName());
temp.add(o.getTaskName());
temp.add(o.getAnswerCode());
temp.add(o.getAnswerCount()+"");
writer.writeRow(temp);
}
writer.flush();
} catch (Exception e) {
logger.error(e);
} finally {
if (writer != null) {
writer.close();
}
}
return filename;
}