excel表数据多线程导入数据库

Student table

一个学生表
在这里插入图片描述
excel表
在这里插入图片描述

Student实体

@Data
public class Student {
   
    private int id;
    private int studentId;
    private int teacherId;
    private String studentName;
    private int studentAge;
    private int studentSex;
}

StudentMapper

void studentAdd(int id,int studentId,int teacherId,String studentName,int studentAge,int studentSex);

 <insert id="studentAdd" >
        insert into student values(#{arg0},#{arg2},#{arg1},#{arg3},#{arg4},#{arg5})
</insert>

Service

public void saveExcelStudent(List<Student> studentList) throws InterruptedException {
   
        //一个线程处理100条数据
        int count = 100;
        //数据集合大小
        int listSize = studentList.size();
        //开启的线程数
        int threadSize = (listSize / count) +1;
        //存放每个线程的执行数据
        List<Student> newList = null;
        Integer mun = 0;
        ExecutorService executor = Executors.newFixedThreadPool(threadSize);
        CountDownLatch begin = new CountDownLatch(1);
        CountDownLatch end = new CountDownLatch(threadSize);
        //循环创建线程
        for(int i = 0;i < threadSize;i++){
   
            //startIndex是一个线程的第一个元素在studentList的索引,endIndex是一个线程最后一个元素在studentList的索引
            //newList 将一个线程在studentList对应的student放入newList
            if((i + 1) == threadSize){
   
                int startIndex = (i * count);
                int endIndex = studentList.size(
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值