当前位置: 首页 > news >正文

EasyExcel listener无法通过Autowired注入xxMapper

easyexcel listener无法通过Autowired注入xxMapper

文章目录

  • easyexcel listener无法通过Autowired注入xxMapper
    • bug记录:
    • 解决方案:
    • easyexcel 使用例子
      • controller
      • ServiceImpl
      • listener

bug记录:

productMapper注入一直为null,而procureDetailMapper却正常注入?

@Slf4j
@Component
public class ProductInfoExcelListener<T> extends AnalysisEventListener<T> {/*** 缓存的数据*/private List<ProductEntity> cachedDataList = ListUtils.newArrayList();/*** 存临时表的数据*/
//    private List<ProcureDetailEntity> tempDataList = ListUtils.newArrayList();private final StringBuilder errMsg = new StringBuilder();private boolean hasException = false;private boolean hasNext = true;private IProcureDetailMapper procureDetailMapper;@Autowiredprivate IProductMapper productMapper;public ProductInfoExcelListener(IProcureDetailMapper procureDetailMapper) {this.procureDetailMapper = procureDetailMapper;}// 每解析一行数据就会调用一次该方法 todo@Overridepublic void invoke(T t, AnalysisContext analysisContext) {// 获取行号int index = analysisContext.readRowHolder().getRowIndex() + 1;ProductExcelVo data = (ProductExcelVo) t;String productId = data.getProductId();String productNum = data.getProductNum();String categoryName = data.getCategoryName();String specName = data.getSpecName();if (!StringUtils.hasText(productId)) {hasException = true;setErrMsg(index, ":商品条码不能为空");return;}if (!StringUtils.hasText(productNum)) {hasException = true;setErrMsg(index, ":商品数量不能为空");return;}if (StringUtils.hasText(productNum)) {int productNumInteger = Integer.parseInt(productNum);if (productNumInteger <= 0) {hasException = true;setErrMsg(index, ":商品数量不能小于0");return;}}if (!StringUtils.hasText(categoryName)) {hasException = true;setErrMsg(index, ":分类名称不能为空");return;}//根据商品条码查询对应的分类信息ProductEntity productBaseDByProductId = productMapper.findProductBaseDByProductId(productId);if (productBaseDByProductId == null) {hasException = true;setErrMsg(index, ":该商品不存在");return;}if (!productBaseDByProductId.getCategoryName().equals(categoryName)) {hasException = true;setErrMsg(index, ":该商品的分类名称有误");return;}cachedDataList.add(productBaseDByProductId);log.info("invoke" + index);}@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {// excel解析完毕以后需要执行的代码
//        saveData()log.info("doAfterAllAnalysed ");}public List getCachedDataList() {return cachedDataList;}private void setErrMsg(int index, String msg) {this.hasNext = false;this.hasException = true;errMsg.append("第").append(index).append(msg);log.error("row {} org data verify failure:{};", index, msg);}public StringBuilder getErrMsg() {return errMsg;}public boolean isHasException() {return hasException;}
}
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ProcureDetailServiceImpl implements IProcureDetailService {private final IProcureDetailMapper procureDetailMapper;@Overridepublic ProductInfo uploadAndParseExcel(MultipartFile file) throws IOException, CustomException {ProductInfoExcelListener<ProductExcelVo> listener = new ProductInfoExcelListener<>(procureDetailMapper);EasyExcel.read(file.getInputStream(),ProductExcelVo.class,listener).sheet().doRead();System.out.println("*******************");System.out.println(listener.getCachedDataList().size() == 0);System.out.println("1111111111111111111");List<ProductEntity> cachedDataList = listener.getCachedDataList();log.info("isHasException:{}", listener.getErrMsg());if(listener.isHasException()){throw new CustomException(CommonConstant.ErrorConstants.EXCEL_UNKNOWN_ERROR.getCode(), listener.getErrMsg().toString());}for (ProductEntity p: cachedDataList){System.out.println(p.toString());}return null;}
}

springboot在listener和filter中注入mapper会为null?

解决方案:

  1. https://codeleading.com/article/13375080759/
  2. 构造器注入

easyexcel 使用例子

<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.1.0</version></dependency>

controller

 @Slf4j
@RestController
@RequestMapping("/api-procure/procureController")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ProcureController extends BaseController {private final IProcureDetailService procureDetailService;/*** 上传excel并且校验解析 procure** @param file      excel* @param loginUser 登录用户* @return* @throws Exception*/@PostMapping("/uploadExcel/procure")public Result<ProductInfo> uploadExcelP(@RequestBody MultipartFile file, @LoginUser LoginUserBean loginUser) throws Exception {log.info("upload excel file:{}", file.getName());ProductInfo data = procureDetailService.uploadAndParseExcel(file, loginUser, CommonConstant.Type.PROCURE.getCode());return Result.succeed(data, "上传成功");}
}

ServiceImpl

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ProcureDetailServiceImpl implements IProcureDetailService {private final IProcureDetailMapper procureDetailMapper;@Overridepublic ProductInfo uploadAndParseExcel(MultipartFile file, LoginUserBean loginUser, String type) throws IOException, CustomException {ProductInfoExcelListener<ProductExcelVo> listener = new ProductInfoExcelListener<>(procureDetailMapper, productMapper);EasyExcel.read(file.getInputStream(),ProductExcelVo.class,listener).sheet().doRead();if (listener.getCachedDataList().size() == 0) {throw new CustomException(CommonConstant.ErrorConstants.EXCEL_UNKNOWN_ERROR.getCode(), "没有有效数据");}List<ProcureDetailEntity> cachedDataList = listener.getCachedDataList();log.info("isHasException:{}", listener.getErrMsg());if (listener.isHasException()) {throw new CustomException(CommonConstant.ErrorConstants.EXCEL_UNKNOWN_ERROR.getCode(), listener.getErrMsg().toString());}
//        for (ProductEntity p : cachedDataList) {
//            System.out.println(p.toString());
//        }if (cachedDataList == null || cachedDataList.size() == 0) {throw new CustomException(CommonConstant.ErrorConstants.EXCEL_UNKNOWN_ERROR.getCode(), "未检测到有效数据");}String userId = loginUser.getUserId();Date date = new Date();//申请标号采购申请单ID 订单//“P”+YYYYMMDDHHMMSS+6位随机数  “PO”+YYYYMMDDHHMMSS+6位随机数String pattern = "YYYYMMDDHHMMSS";SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);String format = dateFormat.format(date);int i = (int) ((Math.random() * 9 + 1) * 100000);String procureId;if (type.equals(CommonConstant.Type.PROCURE.getCode())) {procureId = "P" + format + i;} else if (type.equals(CommonConstant.Type.ORDER.getCode())) {procureId = "PO" + format + i;} else {procureId = null;log.info("这一步执行到就完蛋了");}//存入t_procure_detail_tmp表procureDetailMapper.batchInsertProcureDetailTempList(procureId, userId, date, cachedDataList);//返回响应实体列表ProductInfo productInfo = new ProductInfo();List<ProcureDetailEntity> list = cachedDataList.stream().map(e -> {e.setProcureId(procureId);return e;}).collect(Collectors.toList());productInfo.setProductList(list);return productInfo;}
}

listener

/*** @description <采购申请解析返回的excel信息监听器>* <p>* <p>* 解析模板导入的数据,对商品条码、分类名称、规格名称校验其有效性是否存在,对数量校验大于0.其数据保存在采购单商品临时表.* @author: zhouchaoyu* @Date: 2023-11-14*/
@Slf4j
@Component
public class ProductInfoExcelListener<T> extends AnalysisEventListener<T> {/*** 缓存的数据*/private List<ProcureDetailEntity> cachedDataList = ListUtils.newArrayList();/*** 存临时表的数据*/
//    private List<ProcureDetailEntity> tempDataList = ListUtils.newArrayList();private final StringBuilder errMsg = new StringBuilder();private boolean hasException = false;private boolean hasNext = true;private IProcureDetailMapper procureDetailMapper;private IProductMapper productMapper;public ProductInfoExcelListener(IProcureDetailMapper procureDetailMapper, IProductMapper productMapper) {this.procureDetailMapper = procureDetailMapper;this.productMapper = productMapper;}// 每解析一行数据就会调用一次该方法 todo@Overridepublic void invoke(T t, AnalysisContext analysisContext) {// 获取行号int index = analysisContext.readRowHolder().getRowIndex() + 1;ProductExcelVo data = (ProductExcelVo) t;String productId = data.getProductId();String productNum = data.getProductNum();String categoryName = data.getCategoryName();String specName = data.getSpecName();if (!StringUtils.hasText(productId)) {hasException = true;setErrMsg(index, ":商品条码不能为空");return;}if (!StringUtils.hasText(productNum)) {hasException = true;setErrMsg(index, ":商品数量不能为空");return;}if (StringUtils.hasText(productNum)) {int productNumInteger = Integer.parseInt(productNum);if (productNumInteger <= 0) {hasException = true;setErrMsg(index, ":商品数量不能小于0");return;}}if (!StringUtils.hasText(categoryName)) {hasException = true;setErrMsg(index, ":分类名称不能为空");return;}//根据商品条码查询对应的分类信息ProductEntity productBaseDByProductId = productMapper.findProductBaseDByProductId(productId);if (productBaseDByProductId == null) {hasException = true;setErrMsg(index, ":该商品不存在");return;}if (!productBaseDByProductId.getCategoryName().equals(categoryName)) {hasException = true;setErrMsg(index, ":该商品的分类名称有误");return;}String uid = IdGenerator.getIdStr() ;ProcureDetailEntity procureDetailEntity = new ProcureDetailEntity();procureDetailEntity.setId(uid);procureDetailEntity.setProductId(productBaseDByProductId.getProductId());procureDetailEntity.setProductNum(Integer.parseInt(productNum));procureDetailEntity.setCategoryId(productBaseDByProductId.getCategoryId());procureDetailEntity.setSpecId(specName);procureDetailEntity.setRowStatus("1");procureDetailEntity.setProductName(productBaseDByProductId.getProductName());procureDetailEntity.setCategoryName(productBaseDByProductId.getCategoryName());procureDetailEntity.setInventoryCount(productBaseDByProductId.getInventoryCount());BigDecimal costPrice = productBaseDByProductId.getCostPrice();BigDecimal totalPrice = costPrice.multiply(new BigDecimal(productNum)).setScale(2, RoundingMode.HALF_UP);procureDetailEntity.setTotalPrice(totalPrice);cachedDataList.add(procureDetailEntity);log.info("invoke" + index);}@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {// excel解析完毕以后需要执行的代码
//        saveData()log.info("doAfterAllAnalysed ");}public List<ProcureDetailEntity> getCachedDataList() {return cachedDataList;}private void setErrMsg(int index, String msg) {this.hasNext = false;this.hasException = true;errMsg.append("第").append(index).append(msg);log.error("row {} org data verify failure:{};", index, msg);}public StringBuilder getErrMsg() {return errMsg;}public boolean isHasException() {return hasException;}
}
http://www.lryc.cn/news/239817.html

相关文章:

  • Android Spannable 使用​注意事项
  • Apache访问控制
  • 二、类与对象(二)
  • Pytorch从零开始实战10
  • 设计模式-单例模式实战
  • requests库出现AttributeError问题的修复与替代方法
  • opencv-2D直方图
  • 读像火箭科学家一样思考笔记06_初学者之心
  • 中职组网络安全 Server-Hun-1.img Server-Hun-2.img
  • 基于区域划分的GaN HEMT 准物理大信号模型
  • laravel引入element-ui后,blade模板中使用elementui时,事件未生效问题(下载element-ui到本地直接引入项目)
  • 【计算机网络笔记】路由算法之层次路由
  • 【华为OD机试python】分糖果【2023 B卷|100分】
  • ARM 汇编基础
  • 虹科Pico汽车示波器 | 汽车免拆检修 | 2017款东风本田XR-V车转向助力左右不一致
  • 阿里云服务器ECS经济型e实例优惠99元性能怎么样?
  • vue3引入vuex基础
  • C++二维数组中的查找
  • 【计算思维】蓝桥杯STEMA 科技素养考试真题及解析 2
  • Qt+sqlite3使用事务提升插入效率
  • 【深度学习】不用Conda在PP飞桨Al Studio三个步骤安装永久PyTorch环境
  • SpringBoot:kaptcha生成验证码
  • C/C++ 使用API实现数据压缩与解压缩
  • Visual Studio连接unity编辑器_unity基础开发教程
  • 2023亚太杯数学建模B题思路分析 - 玻璃温室中的微气候法规
  • 轻量封装WebGPU渲染系统示例<37>- 多个局部点光源应用于非金属材质形成的效果(源码)
  • 设备状态监测与故障诊断系统的作用
  • 浮点数运算精度丢失,如何解决
  • 使用微信小程序openMapApp接口,报错问题解决openMapApp:fail invaild coord
  • 2023亚太杯数学建模思路 - 案例:粒子群算法