Mybatis多线程插入List数据

废话不多说,放代码:

一、pom依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

二、yml文件

spring:
  profiles:
    active: dev
  config:
    location: classpath:logback-spring.xml
    #以上可以不用添加
  datasource:
    url: jdbc:mysql://localhost:3306/tb_user?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false
    username: *****
    password: *****
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:/mappers/*.xml
  configuration:
    map-underscore-to-camel-case: true

server:
  port: 9900
  servlet:
    context-path: /

mapper文件放在resource文件目录下的mappers文件夹中。

三、mapper映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.logtest.com.mapper.TeacherMapper">
    <insert id="insertTeacher">
        insert into teacher(age,birthday)
        values
            <foreach collection="list" item="teacher" separator=",">
                ( #{teacher.age},#{teacher.birthday})
            </foreach>
    </insert>
</mapper>

这里注意namespace文件路径为mapper接口的全路径,一个Mapper接口一个xml映射文件。

四、启动类

package com.example.logtest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LogTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(LogTestApplication.class, args);
    }

}

五、Service层

TeacherService接口

package com.example.logtest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LogTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(LogTestApplication.class, args);
    }

}

TeacherServiceImpl实现类

package com.example.logtest.com.service.impl;

import com.example.logtest.com.entity.Teacher;
import com.example.logtest.com.mapper.TeacherMapper;
import com.example.logtest.com.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    TeacherMapper teacherMapper;

    @Override
    public int insertUser(List<Teacher> teacherList) {
        return teacherMapper.insertTeacher(teacherList);
    }
}

六、mapper接口

package com.example.logtest.com.mapper;

import com.example.logtest.com.entity.Teacher;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;


@Mapper
public interface TeacherMapper {
    int insertTeacher(List<Teacher> teacherList);
}

七、实体类Teacher

package com.example.logtest.com.entity;

import lombok.Data;


@Data
public class Teacher {
    private Integer id;
    private Integer age;
    private String birthday;
}

八、Controller层

package com.example.logtest.com.controller;

import com.example.logtest.com.entity.Teacher;
import com.example.logtest.com.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Formatter;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.SimpleFormatter;

@RestController
@RequestMapping("/test")
public class UserController {

    @Autowired
    TeacherService teacherService;

    @GetMapping("/insert")
    public String insertInfo() throws ParseException {
        int i = 0 ;
        int age = 18;
        ExecutorService service = Executors.newFixedThreadPool(5);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String birthday = simpleDateFormat.format(date);
        List<Teacher> teacherList = new ArrayList<>();
        while ( i < 999999){
            Teacher teacher = new Teacher();
            teacher.setAge(age);
            teacher.setBirthday(birthday);
            age++;
            //刷新入库数据
            date.setTime(simpleDateFormat.parse(birthday).getTime()+10000);
            birthday = simpleDateFormat.format(date);
            System.out.println(birthday);
            teacherList.add(teacher);
            if(teacherList.size() == 1000){
                List<Teacher> newList = new ArrayList<>();
                newList.addAll(teacherList);
                teacherList.clear();
                service.execute(new Runnable() {
                    @Override
                    public void run() {
                        teacherService.insertUser(newList);
                    }
                });
            }
            i++;
        }
        if(teacherList.size() > 0 && teacherList.size() < 1000){
            teacherService.insertUser(teacherList);
        }
        return "执行结束";
    }
}

原来学习时就是这里卡了很久,首先要明确线程池的使用原理,当每一挂起任务即执行execute方法时会调用一个线程,这时是不存在线程之间的争抢关系的,也就不存在同一任务中的线程安全问题,而刷新newList也是为了保证线程安全,保证每次更新入库的数据不重复。最后把不满足长度条件的剩余数据一次性统一入库。 

九、数据库结构:

create database tb_user;
use tb_user;

create table mytable(
id int primary key auto_increment,
age int,
birthday varchar(255)
);

按照以上流程部署完后就可以直接运行,对比运行速度还是快很多,多线程加上批量数据入库可以节省很多时间。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值