手写分页 获取数据并处理
1. 准备测试数据
1 // 生成 连续的整数 2 private static List<Integer> makeSequence(int begin, int end) { 3 List<Integer> ret = new ArrayList<>(end - begin + 1); 4 for (int i=begin; i<=end; i++) { 5 ret.add(i); 6 } 7 return ret; 8 }
2. 分页获取数据 并 处理
1 // 分页 处理 2 private static void pageQuery(List<Integer> shopIds){ 3 PopBillBaseDTO popBillBaseDTO = PopBillBaseDTO.builder().build(); 4 int shopOncePageSize = 20; 5 int size = shopIds.size(); 6 if(shopOncePageSize >= size){ 7 // 配置的分批处理数 >= 总店铺数:一次全量处理,不必传参。 8 System.out.println("配置的分批处理数 "+shopOncePageSize+" >= 总店铺数 "+size+" :一次全量处理,不必传参。"); 9 10 }else{ 11 // 配置的分批处理数 < 总店铺数:依配置 分批处理。 12 System.out.println("配置的分批处理数 "+shopOncePageSize+" < 总店铺数 "+size+" :依配置 分批处理。"); 13 int handleSize = 0; 14 int pageNum = size / shopOncePageSize; // 页码 15 int remainder = size % shopOncePageSize; // 余数 16 if(remainder == 0){ 17 // 余数为0:整除 18 System.out.println("整除!!"); 19 for(int index = 0; index < size; index += shopOncePageSize){ 20 handleSize += shopOncePageSize; 21 popBillBaseDTO.setShopIds(shopIds.subList(index, handleSize)); 22 System.out.println("货主 区间:"+ JSON.toJSONString(popBillBaseDTO)); 23 } 24 }else{ 25 System.out.println("除留余数:"+remainder+" !!"); 26 for(int index = 0; index < size-remainder; index += shopOncePageSize){ 27 handleSize += shopOncePageSize; 28 popBillBaseDTO.setShopIds(shopIds.subList(index, handleSize)); 29 System.out.println("货主 区间:"+ JSON.toJSONString(popBillBaseDTO)); 30 } 31 // 余下最后一组,独立处理 32 popBillBaseDTO.setShopIds(shopIds.subList(pageNum * shopOncePageSize, size)); 33 System.out.println("货主 区间:"+ JSON.toJSONString(popBillBaseDTO)); 34 } 35 36 System.out.println("配置的分批处理数 < 总店铺数:依配置 分批处理 结束!!"); 37 } 38 System.out.println("处理完结!!!"); 39 }
3.执行测试
1 public static void main(String[] args) { 2 // 获取 连续的整数(测试数据) 3 List<Integer> shopIds = makeSequence(1, 10); 4 pageQuery(shopIds); 5 }