原!!将集合分段处理

/**
     * 短信集合按照 perCount分段
     * @param list
     * @return
     */

  private static final int perCount = 3000;//单次入库数量

private List<List<OpenMsgDo>> divideIntoLists(List<OpenMsgDo> list) { if (list == null || list.size() == 0) { return null; } List<List<OpenMsgDo>> lists = new ArrayList<>(); int listSize = list.size(); int sections = listSize / perCount; int remain = listSize % perCount; int start = 0; for (int i = 1; i < sections + 1; i++) { List<OpenMsgDo> tempList = new ArrayList<>(); tempList = list.subList(start, i * perCount); lists.add(tempList); start = i * perCount; } if (remain != 0) { List<OpenMsgDo> tempList = new ArrayList<>(); tempList = list.subList(start, start + remain); lists.add(tempList); } return lists; }

 

//批量入库
            for (Map.Entry<String, List<OpenMsgDo>> entry : msgMap.entrySet()) {
                
                String tableName = entry.getKey();
                List<OpenMsgDo> msgList = entry.getValue();
                //TODO 分批量插入
                List<List<OpenMsgDo>> lists = divideIntoLists(msgList);
                if (lists == null) {
                    continue;
                }
                for (int i = 0; i < lists.size(); i++) {
                    int insertRecords = msgService.batchInsertMsg(tableName, lists.get(i));
                    logger.info("【入库的表名=" + tableName + " ,短信集合大小=" + lists.get(i).size()
                            + " ,实际入库的短信条数=" + insertRecords + "】");
                }
                //                int insertRecords = msgService.batchInsertMsg(entry.getKey(), entry.getValue());
            }
            //解析入库成功标志  
            isPraseAndRecordSuccess = true;

 

posted @ 2018-03-09 09:21  乌云de博客  阅读(312)  评论(0编辑  收藏  举报