文章目录
- EasyExcel
- 引入依赖
- 表结构
- 项目结构
- DozerUtils工具类
- 实体类Student
- Controller
- 监听类
- Service
- ServiceImpl
- mapper
- 启动项目
- 测试
- 测试数据
- PostMan测试
EasyExcel
EasyExcel是一个基于Java的、快速、简洁、解决大文件内存溢出的Excel处理工具。
他能让你在不用考虑性能、内存的等因素的情况下,快速完成Excel的读、写等功能。
引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.3</version>
</dependency>
引入dozermapper依赖用于后续对象间类型转换
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-spring-boot-starter</artifactId>
<version>6.5.0</version>
</dependency>
表结构
学生表
CREATE TABLE `student` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '学生表ID',
`sname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '学生姓名',
`sno` bigint NOT NULL COMMENT '学号',
`sex` char(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '性别',
`age` int NOT NULL COMMENT '年龄',
`is_deleted` tinyint UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否删除',
PRIMARY KEY (`id`)
);
项目结构
DozerUtils工具类
public class DozerUtils {
public static <T,S> List<T> mapList(final Mapper mapper, List<S> sourceList, Class<T> targetObjectClass){
List<T> targetList=new ArrayList<T>();
for(S s:sourceList){
targetList.add(mapper.map(s,targetObjectClass));
}
return targetList;
}
}
实体类Student
@ExcelProperty(value =“该字段在Excel中的表头名称”)
@ExcelIgnore:解析时忽略该字段
@Data
public class Student {
/**主键ID**/
@ExcelIgnore
private Long id;
/**学生姓名**/
@ExcelProperty(value = "姓名")
private String sname;
/**学号**/
@ExcelProperty(value = "学号")
private Long sno;
/**性别**/
@ExcelProperty(value = "性别")
private String sex;
/**年龄**/
@ExcelProperty(value = "年龄")
private Integer age;
/**是否删除**/
@ExcelIgnore
private Integer isDeleted;
}
Controller
@RestController
@RequestMapping("/easyExcel")
public class EasyExcelController {
@Resource
private IStudentService studentService;
@PostMapping("/studentInput")
public void studentInput(MultipartFile file){
studentService.studentInput(file);
}
}
监听类
本次设置为1000条数据为一批导入,可根据实际情况调整为3000以内
本次导入未设置导入逻辑,如需设置逻辑,在监听类saveDate方法内调用按逻辑增加的service方法
@Data
public class StudentListener extends AnalysisEventListener<Student> {
/**
*单次缓存量为1000
*/
private final int BATCH_SIZE = 1000;
/**
* 临时存储List
*/
List<Student> cacheData = new ArrayList<Student>();
private IStudentService studentService;
private Mapper dozerMapper;
public StudentListener(IStudentService studentService, Mapper dozerMapper){
this.studentService = studentService;
this.dozerMapper = dozerMapper;
}
@Override
public void invoke(Student data, AnalysisContext analysisContext) {
cacheData.add(data);
if (cacheData.size() >= BATCH_SIZE){
saveData();
//每批存储完成后清空list
cacheData.clear();
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
if (cacheData.size() > 0){
saveData();
}
}
/**
* 加入数据库
*/
private void saveData(){
List<Student> students = DozerUtils.mapList(dozerMapper,cacheData,Student.class);
studentService.saveBatch(students);
}
}
Service
public interface IStudentService extends IService<Student> {
void studentInput(MultipartFile file);
}
ServiceImpl
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
@Resource
private IStudentService studentService;
@Resource
private Mapper dozerMapper;
@Override
@Transactional(rollbackFor = Throwable.class)
public void studentInput(MultipartFile file) {
try {
InputStream inputStream = file.getInputStream();
EasyExcel.read(inputStream, Student.class,
new StudentListener(studentService, dozerMapper)).sheet().doRead();
}catch (Exception e){
e.printStackTrace();
}
}
}
mapper
public interface StudentMapper extends BaseMapper<Student> {
}
启动项目
测试
测试数据
注意列名文字与实体类对应字段注解中的文字相同
PostMan测试
form-data中设置格式为file
选择需要上传的测试文件
完成后查看数据库
导入成功