Mybatis保存多记录,导致SQL过长,保存失败,按指定次数切分,多次保存。

 

    private static final Integer WORKITEM_MAX_NUMBER = 200;
    private int savePbhProblemworkitem(List<ProblemWorkitemVm> problemworkitem) {
        try {
            int count = 0;
            int limit = countStep(problemworkitem.size(), WORKITEM_MAX_NUMBER);
            List<List<ProblemWorkitemVm>> splitList = Stream.iterate(0, n -> n + 1).limit(limit).parallel().map(a -> problemworkitem.stream().skip(a * WORKITEM_MAX_NUMBER).limit(WORKITEM_MAX_NUMBER).parallel().collect(Collectors.toList())).collect(Collectors.toList());
            for(List<ProblemWorkitemVm> pwList : splitList){
                count += pgMapper.savePbhProblemWorkitem(pwList);
            }
            return count;
        } catch (Exception e) {
            log.error(e.getMessage());
            return 0;
        }
    }

    /**
     * 计算切分次数
     */
    private static Integer countStep(Integer size, Integer maxNumber) {
        return (size + maxNumber - 1) / maxNumber;
    }

 

posted @ 2023-11-23 11:51  丿小师傅灬  阅读(85)  评论(0编辑  收藏  举报