溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mybatis批量插入10萬條數(shù)據(jù)的示例分析

發(fā)布時(shí)間:2021-04-16 10:33:49 來源:億速云 閱讀:543 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)mybatis批量插入10萬條數(shù)據(jù)的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

數(shù)據(jù)庫 在使用mybatis插入大量數(shù)據(jù)的時(shí)候,為了提高效率,放棄循環(huán)插入,改為批量插入,mapper如下:

package com.lcy.service.mapper;

import com.lcy.service.pojo.TestVO;
import org.apache.ibatis.annotations.Insert;

import java.util.List;

public interface TestMapper {

    @Insert("")
    Integer testBatchInsert(List list);
}

實(shí)體類:

package com.lcy.service.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class TestVO {

    private String t1;

    private String t2;

    private String t3;

    private String t4;

    private String t5;

}

測試類如下:

import com.lcy.service.TestApplication;
import com.lcy.service.mapper.TestMapper;
import com.lcy.service.pojo.TestVO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest(classes = TestApplication.class)
@RunWith(SpringRunner.class)
public class TestDemo {

    @Autowired
    private TestMapper testMapper;

    @Test
    public void insert() {
        List list = new ArrayList<>();
        for (int i = 0; i < 200000; i++) {
            list.add(new TestVO(i + "," + i, i + "," + i, i + "," + i, i + "," + i, i + "," + i));
        }
        System.out.println(testMapper.testBatchInsert(list));
    }

}

為了復(fù)現(xiàn)bug,我限制了JVM內(nèi)存:

mybatis批量插入10萬條數(shù)據(jù)的示例分析

執(zhí)行測試類報(bào)錯(cuò)如下:

java.lang.OutOfMemoryError: Java heap space

 at java.base/java.util.Arrays.copyOf(Arrays.java:3746)

可以看到,Arrays在申請內(nèi)存的時(shí)候,導(dǎo)致棧內(nèi)存溢出

改進(jìn)方法,分批新增:

import com.lcy.service.TestApplication;
import com.lcy.service.mapper.TestMapper;
import com.lcy.service.pojo.TestVO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@SpringBootTest(classes = TestApplication.class)
@RunWith(SpringRunner.class)
public class TestDemo {

    @Autowired
    private TestMapper testMapper;

    @Test
    public void insert() {
        List list = new ArrayList<>();
        for (int i = 0; i < 200000; i++) {
            list.add(new TestVO(i + "," + i, i + "," + i, i + "," + i, i + "," + i, i + "," + i));
        }
        int index = list.size() / 10000;
        for (int i=0;i< index;i++){
            //stream流表達(dá)式,skip表示跳過前i*10000條記錄,limit表示讀取當(dāng)前流的前10000條記錄
            testMapper.testBatchInsert(list.stream().skip(i*10000).limit(10000).collect(Collectors.toList()));
        }
    }
}

還有一種方法是調(diào)高JVM內(nèi)存,不過不建議使用,不僅吃內(nèi)存,而且數(shù)據(jù)量過大會(huì)導(dǎo)致sql過長報(bào)錯(cuò)

感謝各位的閱讀!關(guān)于“mybatis批量插入10萬條數(shù)據(jù)的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI