" />

最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn)

 更新時(shí)間:2024年05月15日 08:29:22   作者:深夜無(wú)眠T  
本文主要介紹了java數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

本篇文章將記錄幾種使用java向mysql數(shù)據(jù)庫(kù)中批量插入數(shù)據(jù)的方法,比如插入1000條,10000條,10萬(wàn)條甚至100萬(wàn)條數(shù)據(jù)。操作數(shù)據(jù)庫(kù)的方式采用Mybatis框架。

輸入的數(shù)據(jù):

現(xiàn)數(shù)據(jù)庫(kù)有一個(gè)student表,表中字段如下:

編寫student實(shí)體類,及其controller和dao層,因?yàn)橹皇遣迦霐?shù)據(jù)所以不需要加service層。

方法一

最簡(jiǎn)單的方法就是利用循環(huán),不斷執(zhí)行單條數(shù)據(jù)的插入指令。

因此在dao中寫一個(gè)insertStudent方法,并在xml文件中編寫sql語(yǔ)句

void insertStudent(Student student);
    <insert id="insertStudent" parameterType="com.example.bootdemo.entity.Student">
        insert into student (s_name,s_birth,s_sex) values (#{s_name},#{s_birth},#{s_sex})
    </insert>

隨后在controller中編寫循環(huán)條用該方法,比如循環(huán)插入1000條數(shù)據(jù),代碼如下:

    @ResponseBody
    @RequestMapping("/insertstudent")
    public Integer insertStudent(){
        System.out.println("開始插入");
        long start = System.currentTimeMillis();
        /**
         * 依靠循環(huán)插入
         */
        for (int i = 0; i < 1000; i++) {
                Student student = new Student();
                student.setS_birth("20230206");
                student.setS_name("zjd");
                student.setS_sex("男");
                studentDao.insertStudent(student);
        }

        long end = System.currentTimeMillis();
        System.out.println("耗時(shí):"+(end-start));
        return 1;
    }

這種方式雖然可以實(shí)現(xiàn),但是效率比較慢,因?yàn)槊看螆?zhí)行插入都要執(zhí)行一次sql,速度很慢。

方法二

在所有要插入的數(shù)據(jù)放在列表中,并在sql中利用foreach進(jìn)行批量插入。這樣執(zhí)行一次sql就可以插入很多數(shù)據(jù)。

xml編寫中編寫sql

    <insert id="batchInsertStudent" parameterType="java.util.List">
        insert into student (s_name,s_birth,s_sex) values
        <foreach collection="students"  item="student" index="index" separator=",">
            (
             #{student.s_name},
             #{student.s_birth},
             #{student.s_sex}
            )
        </foreach>
    </insert>

將數(shù)據(jù)方法List中執(zhí)行sql語(yǔ)句。

    @ResponseBody
    @RequestMapping("/insertstudent")
    public Integer insertStudent(){
        System.out.println("開始插入");
        long start = System.currentTimeMillis();
        /**
         * 批量插入,大量數(shù)據(jù)時(shí)不推薦使用
         */
        List<Student> students = new ArrayList<>(count);
        for(int i=0;i<count;i++){
            Student student = new Student();
            student.setS_name("zjd"+i);
            student.setS_birth("20230206");
            student.setS_name("zjd");
            student.setS_sex("男");
            students.add(student);
        }
        studentDao.batchInsertStudent(students);

        long end = System.currentTimeMillis();
        System.out.println("耗時(shí):"+(end-start));
        return 1;
    }

這兩種方法在數(shù)據(jù)量很大時(shí)都不推薦使用,第一種會(huì)很慢,第二種可能會(huì)因?yàn)閿?shù)據(jù)過多,sql執(zhí)行失敗,直接報(bào)錯(cuò)。

方法三

既然第二種在插入大量數(shù)據(jù)時(shí)會(huì)報(bào)錯(cuò),那么面對(duì)大量數(shù)據(jù),我們可以將其分批插入,比如我可以每次直插入3000條數(shù)據(jù),執(zhí)行多次就可以實(shí)現(xiàn)大量數(shù)據(jù)的插入。

代碼如下:

    @ResponseBody
    @RequestMapping("/insertstudent")
    public Integer insertStudent() throws InterruptedException {
        System.out.println("開始插入");
        long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for(int i=0;i<6;i++){
            List<Student> students = new ArrayList<>(count);
            int tempCount = 0;
            for(int n=0;n<count;n++){
                if(tempCount==2999){
                    studentDao.batchInsertStudent(students);
                    tempCount=0;
                    students.clear();
                }
                Student student = new Student();
                student.setS_name("zjd"+i);
                student.setS_birth("20230206");
                student.setS_name("zjd");
                student.setS_sex("男");
                students.add(student);
                tempCount++;
            }
            studentDao.batchInsertStudent(students);
            long end = System.currentTimeMillis();
            System.out.println("耗時(shí):"+(end-start));
            countDownLatch.countDown();
        }
        countDownLatch.await();
        return 1;
    }

這樣速度也會(huì)比單條循環(huán)插入要快很多。

到此這篇關(guān)于java數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java 批量插入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring Cloud @RefreshScope 原理及使用

    Spring Cloud @RefreshScope 原理及使用

    這篇文章主要介紹了Spring Cloud @RefreshScope 原理及使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • intellij idea tomcat熱部署配置教程

    intellij idea tomcat熱部署配置教程

    這篇文章主要介紹了intellij idea tomcat熱部署配置教程圖解,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-07-07
  • feign微服務(wù)之間傳遞請(qǐng)求頭數(shù)據(jù)方式

    feign微服務(wù)之間傳遞請(qǐng)求頭數(shù)據(jù)方式

    這篇文章主要介紹了feign微服務(wù)之間傳遞請(qǐng)求頭數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2026-03-03
  • 詳解JavaScript中的函數(shù)聲明和函數(shù)表達(dá)式

    詳解JavaScript中的函數(shù)聲明和函數(shù)表達(dá)式

    這篇文章主要介紹了詳解JavaScript中的函數(shù)聲明和函數(shù)表達(dá)式,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-08-08
  • 詳解Java虛擬機(jī)管理的內(nèi)存運(yùn)行時(shí)數(shù)據(jù)區(qū)域

    詳解Java虛擬機(jī)管理的內(nèi)存運(yùn)行時(shí)數(shù)據(jù)區(qū)域

    這篇文章主要介紹了詳解Java虛擬機(jī)管理的內(nèi)存運(yùn)行時(shí)數(shù)據(jù)區(qū)域的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • SpringBoot整合Lettuce redis過程解析

    SpringBoot整合Lettuce redis過程解析

    這篇文章主要介紹了SpringBoot整合Lettuce redis過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • SpringBoot中Controller的傳參方式詳細(xì)講解

    SpringBoot中Controller的傳參方式詳細(xì)講解

    這篇文章主要介紹了SpringBoot在Controller層接收參數(shù)的常用方法,Controller接收參數(shù)的常用方式總體可以分為三類,第一類是Get請(qǐng)求通過拼接url進(jìn)行傳遞,第二類是Post請(qǐng)求通過請(qǐng)求體進(jìn)行傳遞,第三類是通過請(qǐng)求頭部進(jìn)行參數(shù)傳遞,下面我們來(lái)詳細(xì)看看
    2023-01-01
  • Guava范圍類Range方法實(shí)例深入解析

    Guava范圍類Range方法實(shí)例深入解析

    這篇文章主要為大家介紹了Guava范圍類Range方法實(shí)例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • 基于Springboot疫苗接種行程管理系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)

    基于Springboot疫苗接種行程管理系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)

    本文主要介紹了基于Springboot實(shí)現(xiàn)的疫苗接種行程管理系統(tǒng)的示例代碼,系統(tǒng)主要實(shí)現(xiàn)個(gè)人疫苗接種管理、行程管理、病史管理、風(fēng)險(xiǎn)地區(qū)管理、核酸檢測(cè)報(bào)告結(jié)果上報(bào)、疫情新聞管理等功能,需要的可以參考一下
    2022-03-03
  • Java中用Socket實(shí)現(xiàn)HTTP文件上傳實(shí)例

    Java中用Socket實(shí)現(xiàn)HTTP文件上傳實(shí)例

    本篇文章主要介紹了Java中用Socket實(shí)現(xiàn)HTTP文件上傳實(shí)例,詳細(xì)的介紹了通過讀取Socket的輸入流來(lái)實(shí)現(xiàn)一個(gè)文件上傳的功能,有興趣的同學(xué)可以一起了解一下
    2017-04-04

最新評(píng)論

河间市| 静安区| 金门县| 北票市| 黎平县| 综艺| 平和县| 秦皇岛市| 永川市| 喀喇| 新乐市| 定南县| 兴宁市| 南和县| 方山县| 泽州县| 育儿| 贵南县| 库伦旗| 三都| 东台市| 石林| 林芝县| 安仁县| 昆山市| 兴和县| 米泉市| 房产| 内黄县| 湖南省| 磐安县| 陇川县| 溧阳市| 九龙坡区| 福安市| 河北省| 景德镇市| 兴隆县| 潮安县| 霍山县| 巫溪县|