1.前台代码: Student:index.vue: 前端调用方法(参考如下)

代码位置
// 用户导入参数
upload: {
// 是否显示弹出层(用户导入)
open: false,
// 弹出层标题(用户导入)
title: “”,
// 是否禁用上传
isUploading: false,
// 是否更新已经存在的用户数据
updateSupport: 0,
// 设置上传的请求头部
headers: { Authorization: "Bearer " + getToken() },
// 上传的地址
url: process.env.VUE_APP_BASE_API + “/system/student/importStudent”,
},

导入开发文档
//别忘了 开发文档没有会报错
import { getToken } from “@/utils/auth”;

下面的放这里就可以了

/** 导入按钮操作 /
handleImport() {
this.upload.title = “用户导入”;
this.upload.open = true;
},
/
* 下载模板操作 */
importTemplate() {
importTemplate().then(response => {
this.download(response.msg);
});
},
// 文件上传中处理
handleFileUploadProgress(event, file, fileList) {
this.upload.isUploading = true;
},
// 文件上传成功处理
handleFileSuccess(response, file, fileList) {
this.upload.open = false;
this.upload.isUploading = false;
this. r e f s . u p l o a d . c l e a r F i l e s ( ) ; t h i s . refs.upload.clearFiles(); this. refs.upload.clearFiles();this.alert(response.msg, “导入结果”, { dangerouslyUseHTMLString: true });
this.getList();
},
// 提交上传文件
submitFileForm() {
this.$refs.upload.submit();
}

2.后端代码

先写controller层:

/**
 * 导入
 */
@PreAuthorize("@ss.hasPermi('gwyy:student:import')")
@PostMapping("/importStudent")
@ResponseBody
public AjaxResult importStudent(MultipartFile file, boolean updateSupport) throws Exception
{
    ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
    List<SysStudent> studentList = util.importExcel(file.getInputStream());
    LoginUser loginUser = getLoginUser();//重新修改
    String operName = loginUser.getUsername();
    String message = sysStudentService.importStudent(studentList, updateSupport, operName);//此处要修改,根据service层修改
    return AjaxResult.success(message);
}

IStudentService层:

/**
 * 导入学生数据  写一个接口给controller提供服务
 *
 * @param studentList 学生数据列表
 * @param updateSupport 是否更新支持,如果已存在,则进行更新数据
 * @param operName 是否更新支持,如果已存在,则进行更新数据
 * @return 结果
 */
public String importStudent(List<SysStudent> studentList, Boolean updateSupport, String operName) ;

StudentServiceImpl层:

先在里面创建log常量
private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

针对上面的接口写内容
@Override
public String importStudent(List studentList, Boolean updateSupport, String operName) {
if (StringUtils.isNull(studentList) || studentList.size() == 0) {
// throw new CustomException(“导入用户数据不能为空!”);易报错先注释掉
}
int successNum = 0;
int failureNum = 0;
StringBuilder successMsg = new StringBuilder();
StringBuilder failureMsg = new StringBuilder();
for (SysStudent student : studentList) {
try {
// 验证是否存在这个用户,开始大写一直报错,写错了,用的是mapper代理
SysStudent u = sysStudentMapper.selectSysStudentByName(student.getStudentName());
System.out.print(student.getStudentSex() + “”);
if (StringUtils.isNull(u)) {
student.setStudentName(student.getStudentName());
this.insertSysStudent(student);
successNum++;
successMsg.append("
" + successNum + “学校信息” + student.getStudentName() + " 导入成功");
} else if (updateSupport) {
student.setUpdateBy(operName);
this.updateSysStudent(student);
successNum++;
successMsg.append("
" + successNum + “学校信息 " + student.getStudentName() + " 更新成功”);
} else {
failureNum++;
failureMsg.append("
" + failureNum + “学校信息” + student.getStudentName() + " 已存在");
}
} catch (Exception e) {
failureNum++;
String msg = “
” + failureNum + “学校信息” + student.getStudentName() + " 导入失败:";
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层:
/**
* 通过学生姓名查询学生,航哥加入
*
* @param studentName 用户名
* @return 用户对象信息
*/
public SysStudent selectSysStudentByName(String studentName);

}

Mapperxml

<select id="selectSysStudentByName" parameterType="String" resultMap="SysStudentResult">
    <include refid="selectSysStudentVo"/>
    where student_name = #{studentName}
</select>
Logo

快速构建 Web 应用程序

更多推荐