批量插入大数据量数据优化方案之分批写入

第一种就是自己分批写入:建立新list大小为500,然后循环你的数据

List<UserVO> list=mapper.getList(); //从数据库查询出来的list,也就是需要进行批量插入的总list
//一次插入的条数,也就是分批的list大小
int maxCount= 500;
int listSize=list.size();
int maxSize=listSize - 1;
List<UserVO> newList = new ArrayList<UserVO>();//新建一个载体list
for (int i = 0; i < listSize; i++) {
    //分批次处理
    newList.add(list.get(i));//循环将数据填入载体list
    if (maxCount== newList.size() || i == maxSize) {  //载体list达到要求,进行批量操作
        //调用批量插入
        mapper.insertList(newList);
        newList.clear();//每次批量操作后,清空载体list,等待下次的数据填入
    }
}

 第二种导入apache的包,使用他的 Lists.partition()

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>
List<Integer> list=new ArrayList<>();
for (int i=0;i<500;i++){
    list.add(i);
}
List<List<Integer>> newList = Lists.partition(list, 150);
for (List<Integer> list1:newList){
    System.out.println(list1.size());
}

第三种使用guava的包

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

List<Integer> list=new ArrayList<>();
for (int i=0;i<500;i++){
    list.add(i);
}
List<List<Integer>> newList = Lists.partition(list, 150);
for (List<Integer> list1:newList){
    System.out.println(list1.size());
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
怎么实现? A:Java通过Mybatis实现批量插入数据到Oracle中可以分批提交,具体实现步骤如下: 1. 配置Mybatis的批处理属性 在Mybatis配置文件中,可通过设置batch属性开启批处理功能,同时还需设置fetchSize属性。fetchSize属性指定每次获取的记录数,可以根据实际情况调整大小。 2. 编写批量插入SQL语句 在Mapper.xml文件中,编写批量插入的SQL语句,例如: ``` <insert id="batchInsert" parameterType="java.util.List"> insert into table_name (column1, column2, column3) values <foreach collection="list" item="item" index="index" separator=","> (#{item.column1}, #{item.column2}, #{item.column3}) </foreach> </insert> ``` 其中,#{item.column1}等表示插入的字段值,collection表示要插入的数据集合。 3. 调用批量插入方法 在Java代码中,创建一个List集合存储需要批量插入数据,然后将集合作为参数传递给Mapper接口中的批量插入方法,例如: ``` List<DataEntity> dataList = new ArrayList<>(); // 往dataList中添加数据 dataMapper.batchInsert(dataList); ``` 4. 分批提交数据 如果数据过大,一次性提交可能会导致内存不足或数据库连接超时等问题。为了避免这些问题,可以将数据分批提交。具体实现方式如下: ``` List<DataEntity> dataList = new ArrayList<>(); // 往dataList中添加数据 int batchSize = 1000; int total = dataList.size(); int batchNum = total % batchSize == 0 ? total / batchSize : total / batchSize + 1; for (int i = 0; i < batchNum; i++) { int fromIndex = i * batchSize; int toIndex = (i + 1) * batchSize < total ? (i + 1) * batchSize : total; List<DataEntity> batchList = dataList.subList(fromIndex, toIndex); dataMapper.batchInsert(batchList); } ``` 以上就是Java通过Mybatis实现批量插入数据到Oracle中分批提交的具体实现步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庞胖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值