Code Shared & Review(20201214-20201220)
发布于: 2020 年 12 月 21 日
一、后端
1.stream 应用
1.1 字符串转 List
// 无法处理异常如字符创包含非数字类型(英文、汉字、空格等其他字符)
List<Integer> idList = Arrays.stream(idListStr.split(seperator))
.map(Integer::parseInt)
.collect(Collectors.toList());
gradeIdList.stream().map(String::valueOf).collect(Collectors.joining(",")))
复制代码
1.2 List 转字符串
gradeSortedMap.values().stream().collect(Collectors.joining(","));
String.join(",", gradeSortedMap.values());
attachmentListTmp.stream()
.filter(e -> AttachmentTypeEnum.AV.getCode().equals(e.getTypeCode()))
.map(Attachment::getOriginalFileName)
.collect(Collectors.joining(","));
复制代码
1.3 List<Object> -> map<T,Object>
Map<String, Udc> gradeUdc = GlobalDataCache.udcCache
.stream()
.filter(e -> UdcMeta.grade.getUdcCode().equals(e.getUdcCode()))
.collect(Collectors.toMap(Udc::getValDesc, Function.identity()))
复制代码
1.4 List<Object> -> map<T,List<Object>>
Map<String, List<Chunk>> mapTmp = chunkList.stream()
.collect(Collectors.groupingBy(Chunk::getIdentifier));
复制代码
1.5 List 中对象的属性是否包含指定值
courseList.stream().noneMatch(e -> e.getId() == dra.getCourseId()) == true
复制代码
2.清除 warning
2.1 类注释
2.2 import
2.3 多余的变量定义
ExamBase examBase = examBaseService.findById(id);
return ResultUtil.success(examBase);
==>
return ResultUtil.success(examBaseService.findById(id));
复制代码
3.注入方式 autowired -> 构造器注入
@Autowired
private ExamBaseService examBaseService;
// 构造器注入
private final ExamBaseService examBaseService;
public ExamBaseController(ExamBaseService examBaseService) {
this.examBaseService = examBaseService;
}
复制代码
4.关于日志
controller层入参使用log.info
log.debug("[ExamBase-PAGE] query={}", query);
=>
log.info("[ExamBase-PAGE] query={}", query);
复制代码
if (ToolUtil.isEmpty(id)) {
return ResultUtil.fail("请求数据不完整或格式错误!");
}
log.debug("[ExamBase-findById] id={}", id);
==>
log.info("[ExamBase-findById] id={}", id);
if (ToolUtil.isEmpty(id) || id < 1) {
return ResultUtil.fail(BizExceptionEnum.PARAM_IMCOMPLETE);
}
复制代码
5.swagger 及 JSON 转换
@ApiOperation("新增考试基础表信息")
@BussinessLog(value = "新增考试基础表")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ResultUtil save(@ApiParam(name="examBaseConfig",value = "JSON类型[ExamBase,List<ExamCourseFullMark>],要求:examName必填")
String examBaseConfig) {
if (ToolUtil.isOneEmpty(examBaseConfig)) {
return ResultUtil.fail("请求数据不完整或格式错误!");
}
CourseFullMarkBean courseFullMarkBean = JSONObject.parseObject(examBaseConfig, CourseFullMarkBean.class);
log.debug("[ExamBase-save] examBase={}", courseFullMarkBean); // 无法输出内容,
ResultUtil result = examBaseService.save(courseFullMarkBean);
return result;
}
复制代码
@BussinessLog(value = "新增考试基础表")
@ApiOperation("新增考试基础表信息")
@ApiParam(name="examBaseConfig",value = "JSON类型[ExamBase,List<ExamCourseFullMark>],要求:examName必填")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ResultUtil save(String examBaseConfig) {
log.info("[ExamBase-save] examBaseConfig={}", examBaseConfig);
if (ToolUtil.isOneEmpty(examBaseConfig)) {
return ResultUtil.fail(BizExceptionEnum.PARAM_IMCOMPLETE);
}
try {
CourseFullMarkBean courseFullMarkBean = JSONObject.parseObject(examBaseConfig, CourseFullMarkBean.class);
log.debug("[ExamBase-save] courseFullMarkBean={}", courseFullMarkBean);
return examBaseService.save(courseFullMarkBean);
}catch(JSONException exception) {
return ResultUtil.fail(BizExceptionEnum.JSON_FORMAT_EXCEPTION);
}
}
复制代码
6.e.printStackTrace()
1.只调用 e.printStackTrace(),是不会把日志文件输出到 logback 管理的日志文件。只会写入 tomcat 中的 catalina.out
2.应该使用 log.error("tips",e) 。输出异常信息 + 异常类型,不包括异常堆栈
7.controlle
controller 层只检测入参和处理出参,不负责业务逻辑。下面的代码中,异常逻辑处理需要放在 initBaseImportStu 中。
try {
return examBaseService.initBaseImportStu(examBaseId, file);
} catch (Exception e) {
e.printStackTrace();
examBaseService.updateStatus(examBaseId, Constant.EXAM_INIT_STATUS_START);
return ResultUtil.fail("导入学生信息失败");
}
复制代码
8.SQL delete 返回结果只又两种 >0 or ==0
int result = examBaseService.deleteById(examBaseId);
if (result==-1){
return ResultUtil.fail("基础考试已经被使用在报告配置中,无法删除");
}
复制代码
9.C/U/D 必须加事务控制
@Override
public int batchExamCourseFullMark(Long examBaseId, List<ExamCourseFullMark> marks) {
return examCourseFullMarkMapper.batchInsertFullMark(examBaseId,marks);
}
@Transactional(rollbackFor = Exception.class)
@Override
public int deleteByExamBaseId(Long id){
return this.examCourseFullMarkMapper.deleteById(id);
}
@Override
public ExamCourseFullMark findFullMark(Long examBaseId, Long courseId) {
return examCourseFullMarkMapper.findFullMark(examBaseId, courseId);
}
复制代码
12.代码分析
@Transactional(rollbackFor = Exception.class)
@Override
public ResultUtil save(CourseFullMarkBean courseFullMarkBean) {
//验证科目
List<ExamCourseFullMark> marks = courseFullMarkBean.getFullMarks();
ExamBase examBase = courseFullMarkBean.getExamBase();
if(marks == null || marks.size() == 0){
return ResultUtil.fail("没有选择科目信息");
}
StringBuilder courseNames = new StringBuilder();
for (ExamCourseFullMark mark: marks
) {
ExamCourse examCourse = examCourseService.findById(mark.getCourseId());
if (examCourse!=null){
courseNames.append(mark.getCourseId()).append(",");
}else{
return ResultUtil.fail("科目信息错误[科目ID:"+mark.getCourseId()+"]");
}
}
//赋值exam科目属性
examBase.setExamCourse(courseNames.substring(0,courseNames.length()-1));
examBaseMapper.insertExamBase(examBase);
//添加fullMarks
examCourseFullMarkService.batchExamCourseFullMark(examBase.getId(),marks);
return ResultUtil.success("考试创建成功");
}
复制代码
service 层不要使用 resultUtil.
异常可以使用 throw new ServiceException(BizException.****).
参数校验放在数据库操作之前
for 循环中,不要使用数据库连接这种耗时操作,可以先把数据一次性查出来。
StringBuilder.substring() => List<Integer>. 使用“,”连接。参考 1.2
变量命名必须符合实际意义。names 中不能添加 id。
事务无法对 mapper 层的方法生效
service 层,的 C/U/D 必须加事务
WHERE IN 语句
WHERE("T.code in " + SqlUtil.toSqlStringSet(codes));
复制代码
7.关于 Update 属性为空的判断
// 基本类型 varchar ,int ,日期等
if(StringUtils.isNotEmpty(obj.getField()))
==>
if(null != obj.getField() )
// 集合类型
if(ToolUtil.isNotEmpty(obj.getField())
// 该语句会导致空字符串无法更新的bug
if(StringUtils.notEmpty(obj.getSendTime())){
SET("sendTime = #{obj.sendTime}");
}
// 正确写法
if(null != obj.getChannel() ){
SET("channel = #{obj.channel}");
}
复制代码
二、前端
1.Variable named rule
2.Element 行选择的理解及命名方式
handleSelectionChange(selectRow) {
this.selectRowsId = []
selectRow.forEach(element => {
this.selectRowsId.push(element.id)
})
},
复制代码
3.不要让客户清理缓存
静态资源,比如图片等。
4.民族选择器如何设计
5.Hard Code
<el-image
:preview-src-list="getImgPreview('http://xzzz.zzedu.net.cn/ppst/upload/file/'+scope.row.imgSignUrl)"
fit="scale-down"
:src="'http://xzzz.zzedu.net.cn/ppst/upload/file/'+scope.row.imgSignUrl"
style="width:80px;height:50px;vertical-align: top"
/>
selGrade_onChanged(value) {
this.$http.request_get('business/exam/findCourseDataSource', { gradeCode: value, examId: this.examId }).then(response => {
if (response.data.success) {
this.selCourseDataSourceList = response.data.data
if (this.selCourseDataSourceList.length !== 0) {
this.courseId = this.selCourseDataSourceList[0].id
}
} else {
this.courseId = ''
this.selCourseDataSourceList = []
}
})
},
btnRelationExam_Click() {
console.log(this.selectRowsId)
this.$http.request_get('business/exam/findCourseDataSource', { gradeCode: value, examId: this.examId }).then(response => {
if (response.data.success) {
this.selCourseDataSourceList = response.data.data
if (this.selCourseDataSourceList.length !== 0) {
this.courseId = this.selCourseDataSourceList[0].id
}
} else {
this.courseId = ''
this.selCourseDataSourceList = []
}
})
this.$refs.eltable.clearSelection()
this.selectRowsId = []
},
复制代码
3.English
btnRelationChanel_Click() {
this.dialogVisible = false
this.$refs.eltable.clearSelection()
this.selectRowsId = []
},
复制代码
划线
评论
复制
发布于: 2020 年 12 月 21 日阅读数: 1195
刘璐
关注
还未添加个人签名 2018.03.29 加入
还未添加个人简介
评论