Java 常用方法笔记

50 阅读1分钟

List 转为 Map

import java.util.stream.Collectors;

Map<String, Integer> issueDefineNameMap = issueDefineDOList.stream().collect(Collectors.toMap(CcmIssueDefineDO::getName, CcmIssueDefineDO::getId, (k1, k2) -> k1));

相同属性 DTO 转为 VO 或 DO

import cn.hutool.core.bean.BeanUtil;

ccmProductInfoVOList = ccmProductInfoDTOList.stream().map(ccmProductInfoDTO -> BeanUtil.toBean(ccmProductInfoDTO, CcmProductInfoVO.class)).collect(Collectors.toList());

创建一个空List

import java.util.Collections;

Collections.emptyList()

判断List 是否为空

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

CollectionUtils.isEmpty(ccmProductInfoDTOList)

字符串Json转化为 对象

import com.alibaba.fastjson.JSON;

TranslateResultModel translateResultModel = JSON.parseObject(stringResult, TranslateResultModel.class);

Service 层 插入数据库 try catch

try {
    result = iCcmProductInfoService.saveBatch(ccmProductInfoDOList);
} catch (Exception e){
    throw new SqlException(ResEnum.DATA_BASE_ACTION_EXCEPTION.getResCode(), ResEnum.DATA_BASE_ACTION_EXCEPTION.getResMessage());
}

Controller 层 插入数据库 try catch

try {
    aBoolean = ccmProductInfoService.saveMultipleProductInfo(ccmProductInfoDTOList);
} catch (SqlException e) {
    return new ResResult<>(e.getCode(), e.getMsg());
}