多线程处理Excel导入数据入库

本次示例是使用springboot版本:2.2.6.RELEASE 数据库使用:Mysql

springboot整合mybatisPlus,模拟导入2.4万条数据。

目录

一、目录结构如下图

二、依赖

三、实体类

四、Dao

五、service

五、controller

六、线程类

七、测试

八、测试结果



一、目录结构如下图

 

二、依赖

        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<!--<dependency>
			<groupId>com.oracle.ojdbc</groupId>
			<artifactId>ojdbc8</artifactId>

		</dependency>-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>

		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>false</optional>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.14</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.14</version>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId&g
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
你可以使用Java中的多线程来同时读取Excel文件的多个部分,并将它们合并到一个数据结构中。以下是一个简单的Java多线程读取Excel文件的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; public class ExcelReader { private static final int THREAD_COUNT = 4; public List<Data> readExcel(File file) throws IOException, InterruptedException { List<Data> dataList = new ArrayList<>(); try (FileInputStream fis = new FileInputStream(file); HSSFWorkbook workbook = new HSSFWorkbook(fis)) { Sheet sheet = workbook.getSheetAt(0); int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum(); if (rowCount <= THREAD_COUNT) { // 无需多线程 for (int i = 1; i <= rowCount; i++) { Row row = sheet.getRow(i); Cell cell1 = row.getCell(0); Cell cell2 = row.getCell(1); Cell cell3 = row.getCell(2); dataList.add(new Data(cell1.getStringCellValue(), cell2.getStringCellValue(), cell3.getStringCellValue())); } } else { // 多线程读取 ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT); int batchSize = rowCount / THREAD_COUNT; int remainCount = rowCount % THREAD_COUNT; int startIndex = 1; int endIndex = batchSize; for (int i = 0; i < THREAD_COUNT; i++) { if (i == THREAD_COUNT - 1) { endIndex += remainCount; } final int start = startIndex; final int end = endIndex; executor.execute(() -> { List<Data> subList = new ArrayList<>(); for (int j = start; j <= end; j++) { Row row = sheet.getRow(j); Cell cell1 = row.getCell(0); Cell cell2 = row.getCell(1); Cell cell3 = row.getCell(2); subList.add(new Data(cell1.getStringCellValue(), cell2.getStringCellValue(), cell3.getStringCellValue())); } synchronized (dataList) { dataList.addAll(subList); } }); startIndex = endIndex + 1; endIndex += batchSize; } executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(100); } } } return dataList; } private static class Data { private String field1; private String field2; private String field3; public Data(String field1, String field2, String field3) { this.field1 = field1; this.field2 = field2; this.field3 = field3; } } } ``` 在这个示例中,我们使用了Apache POI库来读取Excel文件。我们将文件读取分为两种情况: - 如果行数小于或等于线程数,则不需要多线程。 - 如果行数大于线程数,则将Excel文件的行数平均分配到多个线程中。 在第二种情况下,我们使用Java ExecutorService API来创建一个线程池,并将行数分成线程数份。然后,我们使用Java lambda表达式来定义每个线程的任务,该任务将读取Excel文件的每个部分,并将其添加到一个数据列表中。最后,我们使用synchronized块来确保多个线程不会同时修改数据列表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值