1. 使用并发流来提高查询速度和代码简洁度:

List allProjectDemeritInfos = allProjectDemeritInfoService.queryAllProjects().stream().collect(Collectors.toList());

  1. 使用Lambda表达式来简化代码:

List specificRegularList = allProjectDemeritInfos.stream().map(allProjectDemeritInfo -> { SpecificRegular specificRegular = new SpecificRegular(); specificRegular.setJudgeState(true);//默认评判 specificRegular.setAllPrDemeritInfoId(allProjectDemeritInfo.getAllPrDemeritInfoId()); specificRegular.setProjectDescribe(allProjectDemeritInfo.getProjectDescribe()); specificRegular.setSpecificJudgeParam(allProjectDemeritInfo.getSpecificJudgeParam()); specificRegular.setVoiceBroadcastText(allProjectDemeritInfo.getVoiceBroadcastText()); specificRegular.setDemeritState(allProjectDemeritInfo.getDemeritState()); specificRegular.setProjectId(allProjectDemeritInfo.getProjectId()); specificRegular.setAudioFileId(allProjectDemeritInfo.getAudioFileId()); specificRegular.setRegularId(regular.getRegularId()); specificRegular.setEnableState(1);//表示启用 return specificRegular; }).collect(Collectors.toList());

  1. 批量插入数据时可以使用MyBatis的批量插入功能来提高效率:

specificRegularService.saveBatch(specificRegularList); //改为specificRegularMapper.insertBatch(specificRegularList);

  1. 使用try-with-resources来自动关闭资源:

try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) { SpecificRegularMapper specificRegularMapper = sqlSession.getMapper(SpecificRegularMapper.class); specificRegularMapper.insertBatch(specificRegularList); sqlSession.commit(); }

  1. 将重复的日志代码提取出来,封装成方法,提高代码复用性:

private void logResult(String message, ResultCode resultCode) { log.info(message + ",结果为:" + resultCode.getMessage()); }

//使用方法: logResult("插入数据", ResultCode.SUCCESS)

代码还可以怎么优化给出具体代码 参考表查出所有数据 ListAllProjectDemeritInfo allProjectDemeritInfos=allProjectDemeritInfoServicequeryAllProjects; if !CollectionUtilsisEmptyallProjectDemeritInfos

原文地址: http://www.cveoy.top/t/topic/fV6t 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录