java數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn)
前言
本篇文章將記錄幾種使用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(jdk)環(huán)境變量配置(XP、win7、win8)圖文教程詳解
對(duì)于初學(xué)java的同學(xué)來(lái)說(shuō),第一件事不是寫(xiě)hello world,而是搭建好java開(kāi)發(fā)環(huán)境,下載jdk,安裝,配置環(huán)境變量。這些操作在xp、win7、win8不同的操作系統(tǒng)里面配置不太一樣,下面通過(guò)本文給大家介紹如何在上面不同操作系統(tǒng)下配置2017-03-03Spring Cloud Gateway Hystrix fallback獲取異常信息的處理
這篇文章主要介紹了Spring Cloud Gateway Hystrix fallback獲取異常信息的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07mybatisPlus中apply的使用以進(jìn)行聯(lián)表等復(fù)雜sql語(yǔ)句詳解
在MyBatis-Plus中,`apply()`方法可以用于添加任意的SQL片段,包括聯(lián)表查詢(xún),使用`apply()`方法的好處是可以直接添加原生的SQL片段,而不受MyBatis-Plus提供的常規(guī)查詢(xún)條件構(gòu)建方法的限制,但是,使用`apply()`方法需要注意安全性和性能問(wèn)題2025-03-03

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

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

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

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