代码还可以怎么优化给出具体代码 参考表查出所有数据 ListAllProjectDemeritInfo allProjectDemeritInfos=allProjectDemeritInfoServicequeryAllProjects; if !CollectionUtilsisEmptyallProjectDemeritInfos
- 使用并发流来提高查询速度和代码简洁度:
List
- 使用Lambda表达式来简化代码:
List
- 批量插入数据时可以使用MyBatis的批量插入功能来提高效率:
specificRegularService.saveBatch(specificRegularList); //改为specificRegularMapper.insertBatch(specificRegularList);
- 使用try-with-resources来自动关闭资源:
try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) { SpecificRegularMapper specificRegularMapper = sqlSession.getMapper(SpecificRegularMapper.class); specificRegularMapper.insertBatch(specificRegularList); sqlSession.commit(); }
- 将重复的日志代码提取出来,封装成方法,提高代码复用性:
private void logResult(String message, ResultCode resultCode) { log.info(message + ",结果为:" + resultCode.getMessage()); }
//使用方法: logResult("插入数据", ResultCode.SUCCESS)
原文地址: http://www.cveoy.top/t/topic/fV6t 著作权归作者所有。请勿转载和采集!