" />

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

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

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

前言

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

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

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

編寫student實體類,及其controller和dao層,因為只是插入數(shù)據(jù)所以不需要加service層。

方法一

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

因此在dao中寫一個insertStudent方法,并在xml文件中編寫sql語句

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("耗時:"+(end-start));
        return 1;
    }

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

方法二

在所有要插入的數(shù)據(jù)放在列表中,并在sql中利用foreach進行批量插入。這樣執(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語句。

    @ResponseBody
    @RequestMapping("/insertstudent")
    public Integer insertStudent(){
        System.out.println("開始插入");
        long start = System.currentTimeMillis();
        /**
         * 批量插入,大量數(shù)據(jù)時不推薦使用
         */
        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("耗時:"+(end-start));
        return 1;
    }

這兩種方法在數(shù)據(jù)量很大時都不推薦使用,第一種會很慢,第二種可能會因為數(shù)據(jù)過多,sql執(zhí)行失敗,直接報錯。

方法三

既然第二種在插入大量數(shù)據(jù)時會報錯,那么面對大量數(shù)據(jù),我們可以將其分批插入,比如我可以每次直插入3000條數(shù)據(jù),執(zhí)行多次就可以實現(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("耗時:"+(end-start));
            countDownLatch.countDown();
        }
        countDownLatch.await();
        return 1;
    }

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

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

相關(guān)文章

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

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

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

    SpringBoot WebSocket連接報no mapping for GE

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

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

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

    JAVA Future類的使用詳解

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

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

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