若依框架的导入详解

Controller层

/**
     * 导入数据列表
     */
    @Log(title = "数据管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<MisData> util = new ExcelUtil<MisData>(MisData.class);
        List<MisData> dataList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = misDataService.importData(dataList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入数据模板
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<MisData> util = new ExcelUtil<MisData>(MisData.class);
        return util.importTemplateExcel("导入数据模板");
    }

Service层

/**
     * 导入数据
     *
     * @param dataList 数据列表
     * @param updateSupport 是否更新支持,如果已存在,则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importData(List<MisData> dataList, boolean updateSupport, String operName) {
        if(dataList==null||dataList.size()==0){
            throw new CustomException("导入数据不能为空!");
        }


        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MisData data : dataList)
        {
            try
            {
                // 验证是否存在这个数据编号
                MisData m = misDataMapper.selectNoByNo(data.getNo());
                if (StringUtils.isNull(m))
                {
                    data.setCreateBy(operName);
                    this.insertMisData(data);
                    successNum++;
                    successMsg.append( successNum + "、数据编号 " + data.getNo() + " 导入成功");
                }
                else if (updateSupport)
                {
                    data.setUpdateBy(operName);
                    this.updateMisData(data);
                    successNum++;
                    successMsg.append( successNum + "、数据编号 " + data.getNo() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append( failureNum + "、数据编号 " + data.getNo() + " 已存在");
                }
            }
            catch (Exception e)
            {
                if(data==null){ throw new CustomException("数据编号为空!"); }
                failureNum++;
                String msg =  failureNum + "、数据编号 " + data.getNo() + " 导入失败:";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
        }
        return successMsg.toString();
    }

剩下的Mapper层加上方法就行
最后导入成功!!!!!

你可能感兴趣的:(java,maven)