chenjunda
长期主义

diary 2025-10-28

2025-10-28 思考感悟

实习日记

1,使用fastExcel工具来进行excel文件的扫描,基本思路就是先设置一下监听器

1
DynamicReadExcelListener readListener = new DynamicReadExcelListener();

然后就可以调用里面的FastExcelFactory中的read方法传入file的输入流,并加入监听器就可以读取excel文档了

1
2
3
4
5
try {
FastExcelFactory.read(file.getInputStream()).registerReadListener(readListener).sheet(0).doRead();
} catch (IOException e) {
throw new YyptServiceException("文件解析失败");
}

其中有一些方法可以直接获得表头和数据,并且使用List<Map<Integer,String>>来封装的

1
2
3
4
5
6
7
8
9
10
// 校验表头
List<Map<Integer, String>> headList = readListener.getHeadList();
if (org.apache.commons.collections4.CollectionUtils.isEmpty(headList)) {
throw new YyptServiceException("第1个工作簿表头为空");
}
// 数据
List<Map<Integer, String>> dataList = readListener.getDataList();
if (CollectionUtils.isEmpty(dataList)) {
throw new YyptServiceException("第1个工作簿数据为空");
}

如果想要遍历表头和数据,就是基本的Map遍历,其中比较对我来说难的点就是使用MP中的lambda语法批量根据name查询id然后使用stream流保存到集合里

,2,如果单纯的查询一个name的id使用MP中的lambda语法就是

1
2
3
4
Long deptId=deptMapper.selectOne(new LambdaQueryWrapper<Dept>()
.eq(Dept::getDeptName,deptName)
.select(Dept::getId)
).getId();

只需要new一个wrapper并制定泛型,使用.eq(类中字段,传入实际name).select(类中方法).getId这种写法就可以一步查询

1
2
3
4
Long deptId=deptMapper.selectOne(new LambdaQueryWrapper<Dept>()
.eq(Dept::getDeptName,deptName)
.select(Dept::getId)
).getId();

但是MP没有办法直接查询并封装集合,就需要结合stream流来实现(我认为selectone或者selectList括号后的函数就不是MP的函数了,而是java里的封装函数),这里的stream流使用就是,先开启一个流,然后使用.map(进行类型转换的函数).collect(Collectors.toList)来封装成集合,map中传入的也是一个方法,是查询的方法

1
2
3
4
5
6
List<Long> roleIds = roleMapper.selectList(new LambdaQueryWrapper<Role>()
.eq(Role::getName, roleName)
.select(Role::getId)
).stream()
.map(Role::getId)
.collect(Collectors.toList());

3,还有一个难点就是我使用fastExcel获得的dataList是没有类型的,我需要讲里面的数据都进行类型转换,重新封装成一个新的list,其中role其中的list类型的成员,我还要遍历里面的成员在嵌套封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//该怎么获取data集合封装成req集合???
List<UserSaveReq> reqList = new ArrayList<>();
for (int i = 0; i < dataList.size(); i++) {
Map<Integer, String> data = dataList.get(i);
UserSaveReq req = new UserSaveReq();
req.setUsername(username);
req.setName(name);
req.setPhone(phone);
req.setEmail(email);
req.setJob(new IdBase(jobId));
req.setDept(new IdBase(deptId));
req.setStore(new IdBase(storeId));
List<IdBase> idBases = new ArrayList<>();
roleIds.forEach(roleId -> {
IdBase idBase = new IdBase();
idBase.setId(roleId);
idBases.add(idBase);
});
req.setRoles(idBases);
reqList.add(req);
}

Author: chenjunda

Link: http://example.com/2025/10/28/diary-2025-10-28/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
diary2025-10-30
NextPost >
560.和为K的子数组
CATALOG
  1. 1. 实习日记