分批查询数据(简单的实现)

例子: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);
        }

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值