https://www.yuque.com/easyexcel/doc
excel数据导入数据库
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ls</groupId>
<artifactId>ls-easy-excel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ls-easy-excel</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
</project>
application.yml
server:
port: 80
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/order?useUnicode=ture&characterEncoding=UTF-8&serverTimezone=GMT%2B8
password: root
username: root
mybatis:
mapperLocations: classpath:mapper/*.xml
typeAliasesPackage: com.ls.pojo
index.html
<!DOCTYPE html>
<html lang="cn">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h1 >文件上传</h1>
<form action="/fileUpload" method="post" enctype="multipart/form-data">
<p>选择文件: <input type="file" name="file"/></p>
<p><input type="submit" value="提交"/></p>
</form>
</body>
</html>
controller.java
package com.ls.controller;
import com.ls.pojo.Orders;
import com.ls.service.OrderService;
import com.ls.utils.EasyExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
@Controller
public class OrderController {
@Autowired
private OrderService orderService;
@RequestMapping("/find")
@ResponseBody
public List<Orders> getOrderList(){
return orderService.getOrderList();
}
@RequestMapping("/index")
public String index(){
return "index";
}
@RequestMapping("/fileUpload")
@ResponseBody
public List<Orders> fileUpload(@RequestParam("file") MultipartFile file){
try {
List<Orders> result = EasyExcelUtils.read(file,Orders.class);
long s = System.currentTimeMillis();
/* for (Orders orders :result) {
orderService.insert(orders);
}*/
//long e = System.currentTimeMillis();
// System.out.println("for循环出入list " + (e-s));
orderService.iOrderList(result);
long e = System.currentTimeMillis();
System.out.println("sql循环出入list " + (e-s));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
EasyExcelUtils.java
package com.ls.utils;
import com.alibaba.excel.EasyExcel;
import com.ls.listener.OrderExcelListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
/**
* easyExcel 工具类
* @author rstyro
*/
@Slf4j
public class EasyExcelUtils {
/**
* 读取excel 并解析
* @param file 文件
* @param clazz 解析成哪个pojo
* @param <T> t
* @return list
* @throws IOException error
*/
public static <T> List<T> read(MultipartFile file, Class<T> clazz) throws IOException {
OrderExcelListener<T> baseExcelDataListener = new OrderExcelListener();
EasyExcel.read(file.getInputStream(), clazz, baseExcelDataListener).sheet().doRead();
return baseExcelDataListener.getResult();
}
}
OrderExcelListener.java
package com.ls.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
/**
* 通用的解析excel 监听器
* @param <T>
*/
@Slf4j
public class OrderExcelListener<T> extends AnalysisEventListener<T> {
private List<T> result = new ArrayList<>();
//每条数据解析都会调用此方法
//实际可以3000条保存一次
@Override
public void invoke(T t, AnalysisContext analysisContext) {
result.add(t);
}
//所有数据解析完成会执行
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
log.info("所有数据解析完成!");
}
//返回最终解析结果
public List<T> getResult() {
return result;
}
}
OrderService.java
package com.ls.service;
import com.ls.pojo.Orders;
import java.util.List;
public interface OrderService {
void insert(Orders orders);
void iOrderList(List<Orders> ordersList);
List<Orders> getOrderList();
}
OrderServiceImpl.java
package com.ls.service.impl;
import com.ls.mapper.OrderMapper;
import com.ls.pojo.Orders;
import com.ls.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderMapper orderMapper;
@Override
public void insert(Orders orders) {
orderMapper.insert(orders);
}
@Override
public void iOrderList(List<Orders> ordersList) {
orderMapper.iOrderList(ordersList);
}
@Override
public List<Orders> getOrderList() {
return orderMapper.selectAll();
}
}
OrderMapper.java
package com.ls.mapper;
import com.ls.pojo.Orders;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
//通用mapper
public interface OrderMapper extends Mapper<Orders> {
//自定义自己的接口
void iOrderList(List<Orders> ordersList);
}
OrderMapper.xml
<?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.ls.mapper.OrderMapper">
<insert id="iOrderList" parameterType="com.ls.pojo.Orders">
INSERT INTO orders(user_id,product_id,count,money,status)
VALUES
<foreach collection="ordersList" index="index" item="order" separator=",">
(
#{order.userId,jdbcType=BIGINT},
#{order.productId,jdbcType=BIGINT},
#{order.count,jdbcType=BIGINT},
#{order.money,jdbcType=DECIMAL},
#{order.status,jdbcType=INTEGER})
</foreach>
</insert>
</mapper>
foreach元素的属性主要有 item,index,collection,open,separator,close。
collection 需要迭代的对象,
item表示集合中每一个元素进行迭代时的别名,
index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,
open表示该语句以什么开始,
separator表示在每次进行迭代之间以什么符号作为分隔 符,
close表示以什么结束
LsEasyExcelApplication.java
package com.ls;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
//此注解为通用mapper
@MapperScan("com.ls.mapper")
public class LsEasyExcelApplication {
public static void main(String[] args) {
SpringApplication.run(LsEasyExcelApplication.class, args);
}
}