//参考表查出所有数据 List allProjectDemeritInfos = allProjectDemeritInfoService.queryAllProjects().stream().collect(Collectors.toList());

if (!CollectionUtils.isEmpty(allProjectDemeritInfos)) {

//存放批量新增具体规则
List<SpecificRegular> 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());

if (!CollectionUtils.isEmpty(specificRegularList)) {

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

        long millis1 = System.currentTimeMillis();
        long millis2 = System.currentTimeMillis();
        long time = millis2 - millis1; //经过的毫秒数
        log.info('新增规则所需时间:' + time + 'ms');

        springToGameService.reloadAddOneRegularWithSpecificRegular(regular, specificRegularList);

        logResult('插入数据', ResultCode.SUCCESS);
    } catch (Exception e) {
        logResult('插入数据', ResultCode.FAIL);
    }
} else {
    logResult('新增数据的集合为空!', ResultCode.FAIL);
}

} else { logResult('参考表数据为空!', ResultCode.FAIL); }

// 操作失败 logResult('批量新增规则失败!', ResultCode.FAIL);

// ...

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

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

Java 代码优化技巧:提高效率和可读性

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

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