例子:mybatis ORM框架
geely4sDao.getGeely4sCarOrderByOrderdetailList(List<String> carOrderidList );
问题:List<String> carOrderidList 有 10W条,则会出现异常,导致500;
解决:分批查询,方案如下:
/**
* 分批查询相关的订单
* @param carOrderidList 车险相关订单
* @return 车险订单详情
*/
private List<Map<String, Object>> getOrderInfoList(List<String> carOrderidList) {
int orderSize = carOrderidList.size();
int orderNum = orderSize / 200;
int orderLeft = orderSize % 200;
if (orderLeft >= 1) {
orderNum++;
}
List<Map<String, Object>> resultList = new ArrayList<>();
int orderCount = 1;
List<Map<String, Object>> tempList;
//循环获取
while (true) {
if (orderCount >= orderNum) {
tempList = geely4sDao.getGeely4sCarOrderByOrderdetailList(
carOrderidList.subList(200 * (orderCount -1), orderSize));
resultList.addAll(tempList);
break;
} else {
tempList = geely4sDao.getGeely4sCarOrderByOrderdetailList(
carOrderidList.subList(200 * (orderCount -1), 200 * orderCount));
resultList.addAll(tempList);
}
orderCount++;
}
return resultList;
}
还有种更简单的写法
int pageindex = 1;
int pagesize = 500;
boolean lookup = true;
List<EmployeeInsureRecordHistory> allHistoryList = new ArrayList<>();
List<EmployeeInsureRecordHistory> historyList;
Map<String, Object> params = new HashMap<>();
params.put("pagesize", pagesize);
while (lookup) {
params.put("offset", (pageindex -1) * pagesize);
historyList = employeeInsureRecordHistoryDao.getAllRecordListByPageForExport(params);
if (historyList.size() < 500) {
lookup = false;
}
pageindex++;
allHistoryList.addAll(historyList);
}