Java List分批处理,分批插入。

时间:2025-02-15 16:58:08

    @Test
    public void rand(){
        //生成数据
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < 55; i++) {
            Map<String,Object> map = new HashMap<>();
            ("se_id", i);
            (map);
        }
        int stopIndext = 0;
        int startIndext = 0;
        int num = 10;
        int size = ();
        //循环次数
        int fori = size%num==0 ? size/num : size/num+1;
        //长度不足无需分批
        if(num < size) {
            for (int i = 0; i < fori; i++) {
                //处理切分长度
                stopIndext = (i==fori-1) ? stopIndext + size%num : stopIndext + num;
                List<Map<String, Object>> lis = (startIndext, stopIndext);
                //可以在此处写insert方法
                ((lis));
                //清除切分后的数据
                (0,stopIndext).clear();
                stopIndext = startIndext;
            }
        }else {
            //可以在此处写insert方法
            ((list));
        }
    }

如果支持引用第三方包的话,可以使用hutool工具包

import ;

// 切分list
List<List<Object>> split = (list, 500);