Ruoyi 导入

Mapper

public Student selectStudentByLoginName(String name);//数据库导入

Mapper.xml

 <select id="selectStudentByLoginName" parameterType="String" resultMap="StudentResult">
        <include refid="selectStudentVo"/>
        where name = #{name}
    </select>

ServiceImpl

 /**
     * 导入用户数据
     *
     * @param studentList 用户数据列表
     * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importStudent(List<Student> studentList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(studentList) || studentList.size() == 0)
        {
            throw new BusinessException("导入用户数据不能为空!");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Student student : studentList)
        {
            try
            {
                // 验证是否存在这个用户
                Student u = studentMapper.selectStudentByLoginName(student.getName() );
                if (StringUtils.isNull(u))
                {
                    student.setName(student.getName());
                    this.insertStudent(student);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "学生信息" + student.getName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    student.setUpdateBy(operName);
                    this.updateStudent(student);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "学校信息 " + student.getName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "学校信息" + student.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "学校信息" + student.getName() + " 导入失败:";
                failureMsg.append(msg + e.getMessage());
                
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
        }
        return successMsg.toString();
    }

Controller


    /**
     * 学校信息导入
     */
    @RequiresPermissions("system:sudent:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        List<Student> studentList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = studentService.importStudent(studentList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @RequiresPermissions("system:student:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        return util.importTemplateExcel("学生信息数据");
    }

Html

<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:student:export">
                <i class="fa fa-download"></i> 导出
            </a>
 nostudentSuccess:prefix + "/nostudentSuccess",
            importTemplateUrl: prefix + "/importTemplate",
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file"/>
            <div class="mt10 pt5">
                <input type="checkbox" id="updateSupport" name="updateSupport" title="如果登录账户已经存在,更新这条数据。"> 是否更新已经存在的用户数据
                &nbsp;	<a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 是否下载这个模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                提示:仅允许导入“xls”或“xlsx”格式文件!
            </font>
        </div>
    </form>
</script>
Logo

快速构建 Web 应用程序

更多推荐