前言
本篇文章将记载几种使用java向mysql数据库中批量插入数据的方法,好比插入1000条,10000条,10万条乃至100万条数据。操作数据库的方式采用Mybatis框架。
输入的数据:
现数据库有一个student表,表中字段如下:
编写student实体类,及其controller和dao层,因为只是插入数据以是不需要加service层。
方法一
最简单的方法就是使用循环,不断执行单条数据的插入指令。
因此在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中编写循环条用该方法,好比循环插入1000条数据,代码如下:
- @ResponseBody
- @RequestMapping("/insertstudent")
- public Integer insertStudent(){
- System.out.println("开始插入");
- long start = System.currentTimeMillis();
- /**
- * 依靠循环插入
- */
- 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;
- }
复制代码 这种方式虽然可以实现,但是效率比较慢,因为每次执行插入都要执行一次sql,速度很慢。
方法二
在全部要插入的数据放在列表中,并在sql中使用foreach进行批量插入。这样执行一次sql就可以插入很多数据。
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>
复制代码 将数据方法List中执行sql语句。
- @ResponseBody
- @RequestMapping("/insertstudent")
- public Integer insertStudent(){
- System.out.println("开始插入");
- long start = System.currentTimeMillis();
- /**
- * 批量插入,大量数据时不推荐使用
- */
- 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;
- }
复制代码 这两种方法在数据量很大时都不推荐使用,第一种会很慢,第二种可能会因为数据过多,sql执行失败,直接报错。
方法三
既然第二种在插入大量数据时会报错,那么面临大量数据,我们可以将其分批插入,好比我可以每次直插入3000条数据,执行多次就可以实现大量数据的插入。
代码如下:
- @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;
- }
复制代码 这样速度也会比单条循环插入要快很多。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |