" />

欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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),文中通過(guò)示例代碼介紹的非常詳細(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表,表中字段如下:

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

方法一

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

因此在dao中寫(xiě)一個(gè)insertStudent方法,并在xml文件中編寫(xiě)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中編寫(xiě)循環(huán)條用該方法,比如循環(huán)插入1000條數(shù)據(jù),代碼如下:

    @ResponseBody
    @RequestMapping("/insertstudent")
    public Integer insertStudent(){
        System.out.println("開(kāi)始插入");
        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編寫(xiě)中編寫(xiě)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("開(kāi)始插入");
        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ù)過(guò)多,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("開(kāi)始插入");
        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)文章

  • Java常用的八種排序算法及代碼實(shí)現(xiàn)+圖解

    Java常用的八種排序算法及代碼實(shí)現(xiàn)+圖解

    這篇文章主要介紹了Java常用的八種排序算法及代碼實(shí)現(xiàn),在Java的時(shí)候,對(duì)于排序的應(yīng)用需要熟練的掌握,這樣才能夠確保Java學(xué)習(xí)時(shí)候能夠有扎實(shí)的基礎(chǔ)能力。那Java有哪些排序算法呢?本文小編就來(lái)詳細(xì)說(shuō)說(shuō)Java經(jīng)典的8種排序算法,需要的朋友可以參考一下
    2021-12-12
  • SpringBoot WebSocket連接報(bào)no mapping for GET問(wèn)題

    SpringBoot WebSocket連接報(bào)no mapping for GE

    文章描述了一個(gè)在調(diào)試WebSocket連接時(shí)遇到的`nomappingforGET`異常問(wèn)題,并提供了問(wèn)題解決的方法,包括檢查WebSocket注解和補(bǔ)充相關(guān)配置,此外,還特別提到了在使用Nginx轉(zhuǎn)發(fā)WebSocket時(shí)所需的配置
    2025-02-02
  • 兩種Eclipse部署動(dòng)態(tài)web項(xiàng)目方法

    兩種Eclipse部署動(dòng)態(tài)web項(xiàng)目方法

    這篇文章主要介紹了兩種Eclipse部署動(dòng)態(tài)web項(xiàng)目方法,需要的朋友可以參考下
    2015-11-11
  • JAVA Future類(lèi)的使用詳解

    JAVA Future類(lèi)的使用詳解

    這篇文章主要介紹了JAVA Future類(lèi)的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-04-04
  • 使用Java生成32位16進(jìn)制密鑰的代碼實(shí)現(xiàn)

    使用Java生成32位16進(jìn)制密鑰的代碼實(shí)現(xiàn)

    在許多加密和安全應(yīng)用中,生成隨機(jī)的密鑰是至關(guān)重要的一步,密鑰通常以16進(jìn)制形式表示,并且具有特定的長(zhǎng)度,在這篇博客中,我們將探討如何使用Java生成一個(gè)32位長(zhǎng)度的16進(jìn)制密鑰,并展示詳細(xì)的代碼示例和運(yùn)行結(jié)果,需要的朋友可以參考下
    2024-08-08
  • 最新評(píng)論