• 大集合List分为多个子集合


    批量插入时如果一次插入的对象过多会导致超过mysql限定sql长度,通过命令查看

    show VARIABLES like 'max_allowed_packet' ,如果数据太多,就将大集合List分为多个小集合,分多次批量插入吧。

    附上源码

    import com.duotin.util.beans.SubListView;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    /**
     *
     * Created by xueshan on 17/1/22.
     */
    public class CommonBusiLogic {
    
        /**
         *将大集合分成多个子集合
         * @param list
         * @param subListSize 大小
         * @param <T>
         * @return
         */
        public static <T> List<List<T>> getSubListBySubListSize(List<T> list, Integer subListSize){
            List<List<T>> allList = new ArrayList<>();
            if(CollectionUtils.isEmpty(list)){
                return Collections.emptyList();
            }
            if(list.size()<=subListSize){
                allList.add(list);
                return allList;
            }
            for(SubListView subListView:getSubListViewList(list.size(), subListSize)){
                allList.add(list.subList(subListView.getStartIndex(),subListView.getEndIndex()));
            }
            return allList;
        }
        private static List<SubListView> getSubListViewList(Integer listSize, Integer subListSize){
            List<SubListView> subListViewList = new ArrayList<>();
            Integer multipleSize=listSize/subListSize;
            for(int i=0;i<listSize/subListSize;i++){
                SubListView subListView = new SubListView();
                subListView.setStartIndex(i);
                subListView.setEndIndex(i+subListSize);
                subListView.setSubListSize(subListSize);
                subListViewList.add(subListView);
            }
            if(listSize%subListSize>0){
                SubListView endSubListView = new SubListView();
                endSubListView.setStartIndex(multipleSize*subListSize);
                endSubListView.setEndIndex(multipleSize*subListSize+listSize%subListSize);
                endSubListView.setSubListSize(subListSize);
                subListViewList.add(endSubListView);
            }
            return subListViewList;
    
        }
    }
    雪人正在努力……
  • 相关阅读:
    POJ 3411 Paid Roads(DFS)
    POJ 1699 Best Sequence(DFS)
    Codeforces Round #191 (Div. 2)
    Windows && Linux 搭建python开发环境
    zabbix 源码编译安装
    智能运维基础设施
    Redis
    ubuntu16.04 安装 mysql
    Python必须知道的基础语法
    ubuntu && CentOS && RedHat 离线安装docker
  • 原文地址:https://www.cnblogs.com/nunuAction/p/6428954.html
Copyright © 2020-2023  润新知