手寫分頁處理

手寫分頁 獲取數據並處理

 

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     }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章