java數(shù)據(jù)庫批量插入數(shù)據(jù)的實(shí)現(xiàn)
前言
本篇文章將記錄幾種使用java向mysql數(shù)據(jù)庫中批量插入數(shù)據(jù)的方法,比如插入1000條,10000條,10萬條甚至100萬條數(shù)據(jù)。操作數(shù)據(jù)庫的方式采用Mybatis框架。
輸入的數(shù)據(jù):
現(xiàn)數(shù)據(jù)庫有一個(gè)student表,表中字段如下:

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

方法一
最簡單的方法就是利用循環(huán),不斷執(zhí)行單條數(shù)據(jù)的插入指令。
因此在dao中寫一個(gè)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("耗時(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語句。
@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í)都不推薦使用,第一種會很慢,第二種可能會因?yàn)閿?shù)據(jù)過多,sql執(zhí)行失敗,直接報(bào)錯(cuò)。
方法三
既然第二種在插入大量數(shù)據(jù)時(shí)會報(bào)錯(cuò),那么面對大量數(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án)插入要快很多。
到此這篇關(guān)于java數(shù)據(jù)庫批量插入數(shù)據(jù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java 批量插入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Cloud @RefreshScope 原理及使用
這篇文章主要介紹了Spring Cloud @RefreshScope 原理及使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
feign微服務(wù)之間傳遞請求頭數(shù)據(jù)方式
這篇文章主要介紹了feign微服務(wù)之間傳遞請求頭數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2026-03-03
詳解JavaScript中的函數(shù)聲明和函數(shù)表達(dá)式
這篇文章主要介紹了詳解JavaScript中的函數(shù)聲明和函數(shù)表達(dá)式,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-08-08
詳解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中Controller的傳參方式詳細(xì)講解
這篇文章主要介紹了SpringBoot在Controller層接收參數(shù)的常用方法,Controller接收參數(shù)的常用方式總體可以分為三類,第一類是Get請求通過拼接url進(jìn)行傳遞,第二類是Post請求通過請求體進(jìn)行傳遞,第三類是通過請求頭部進(jìn)行參數(shù)傳遞,下面我們來詳細(xì)看看2023-01-01
基于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ū)管理、核酸檢測報(bào)告結(jié)果上報(bào)、疫情新聞管理等功能,需要的可以參考一下2022-03-03
Java中用Socket實(shí)現(xiàn)HTTP文件上傳實(shí)例
本篇文章主要介紹了Java中用Socket實(shí)現(xiàn)HTTP文件上傳實(shí)例,詳細(xì)的介紹了通過讀取Socket的輸入流來實(shí)現(xiàn)一個(gè)文件上傳的功能,有興趣的同學(xué)可以一起了解一下2017-04-04

