前言

Github:https://github.com/HealerJean

博客:http://blog.healerjean.com

1、批量获取数据分段处理

@Slf4j
public class BigDataQueryUtils {

    /**
     * 1、获取分段结果
     */
    @Test
    public void test(){
        int totalCount = 10;
        int intervalLength = 5;
        List<ImmutablePair<Integer, Integer>> result = intervalPairOfTotalCount(totalCount, intervalLength);
        log.info("totalCouont:{}, intervalLength:{}, result:{}", totalCount, intervalLength, result);
    }

    /**
     * 获取分段区间,索引从0开始
     * @param totalCount 总数量
     * @param pageSize 区间长度
     */
    public static List<ImmutablePair<Integer, Integer>> intervalPairOfTotalCount(Integer totalCount, Integer pageSize){
        List<ImmutablePair<Integer, Integer>> result = new ArrayList<>();

        Integer pageCount;
        if (totalCount % pageSize == 0) {
            pageCount = totalCount / pageSize;
        } else {
            pageCount = totalCount / pageSize + 1;
        }

        Integer pageNow = 1;
        while (pageNow <= pageCount ){
            int startIndex = (pageNow - 1) * pageSize;
            ImmutablePair<Integer, Integer> pair = ImmutablePair.of(startIndex, pageSize);

            // 结束索引,看需要是否要取分段endIndex
            // int endIndex = pageNow .compareto(pageCount) !=0  ? startIndex + pageSize : totalCount;
            // ImmutablePair<Integer, Integer> pair = ImmutablePair.of(startIndex, endIndex);

            pageNow++;
            result.add(pair);
        }
        return result;
    }

}

2、分页太大分段处理


@Override
public List<ClaimOrderInfoDTO> queryClaimOrderList(ClaimQueryReq claimQueryReq) {
  List<ClaimOrderInfoDTO> result = new ArrayList<>();
  PageQueryRequest<ClaimQueryReq> pageQueryRequest = new PageQueryRequest<>();
  pageQueryRequest.setQuery(claimQueryReq);
  pageQueryRequest.setPageSize(NumberConstant.ONE_HUNDRED);
  pageQueryRequest.setPageIndex(NumberConstant.ONE);
  boolean finish  = false;
  while (Boolean.FALSE.equals(finish)){
    PageQueryResponse<ClaimOrderInfoDTO>  response = queryClaimOrder(pageQueryRequest);
    if (response != null && response.getSuccess() != null && response.getSuccess() &&  response.getTotalPage() != null && response.getTotalPage() > pageQueryRequest.getPageIndex() &&!CollectionUtils.isEmpty(response.getValues())){
      result.addAll(response.getValues());
      pageQueryRequest.setPageSize(NumberConstant.ONE_HUNDRED);
      pageQueryRequest.setPageIndex(pageQueryRequest.getPageIndex() + NumberConstant.ONE);
      continue;
    }
    finish = true;
  }
  return result;
}

ContactAuthor