java 常见函数 和 mapper
目录
- 85.
- 86.初始化list集合时赋值
- 87.要素过多--->细看
- 88.mapper.selectJoinList()
- 89.mapper.selectJoinPage()
- 90.Collectors.groupingBy(MsegmentInfoResponse::getDeptName)
- 91.实体中添加字段,不映射到数据库表 @TableField(exist = false)
- 92.mybatis-plus按条件查询数据库(eq、in)(构造链式结构)
- 93.新建含初始值的list集合
- 94.double判空
- 95.String、Integer对象比较得用equals()
- 96.String判空
- 97.排序
- 98. 遍历赋值
- 99. list判空
- 100.抛出异常
85.
/**
* 添加信息
*
* @param addBmsSMaintainSchemeParam
* @return
*/
@Override
public void add(AddBmsSMaintainSchemeParam addBmsSMaintainSchemeParam) {
BmsSMaintainScheme bmsSMaintainScheme = addBmsSMaintainSchemeParam.getBmsSMaintainScheme();
int id = idManagementService.getId("s_maintain_scheme");
bmsSMaintainScheme.setId(id);
bmsSMaintainScheme.setDeleteState(0);
bmsSMaintainSchemeMapper.insert(bmsSMaintainScheme);
//关联关系表
List<BmsRMaintainScheme> bmsRMaintainSchemeList = addBmsSMaintainSchemeParam.getBmsSStructureList();
int bmsRMaintainSchemeId = idManagementService.getId("r_maintain_scheme", bmsRMaintainSchemeList.size());
for (BmsRMaintainScheme bmsRStructureStyle : bmsRMaintainSchemeList) {
bmsRStructureStyle.setId(bmsRMaintainSchemeId++ - bmsRMaintainSchemeList.size() + 1);
bmsRStructureStyle.setSchemeId(id);
bmsRStructureStyle.setDeleteState(0);
}
bmsRMaintainSchemeService.saveBatch(bmsRMaintainSchemeList);
}
86.初始化list集合时赋值
List<String> lst = Arrays.asList("明奎","Dk","The 8","胜宽");
87.要素过多—>细看
/**
* 添加信息
*
* @param request
* @return
*/
@Override
public BaseResponse add(AddAssetDataRequest request) {
// 添加资产表信息
AmsDAssetData amsDAssetData = new AmsDAssetData();
int assetId = idManagementService.getId("ams_d_asset_data");
BeanUtils.copyProperties(request, amsDAssetData);
amsDAssetData.setId(assetId);
amsDAssetData.setDeleteState(0);
//取当前时间
amsDAssetData.setCreateTime(new Date());
amsDAssetData.setModifyTime(new Date());
amsDAssetData.setModifyUserId(request.getCreateUserId());
amsDAssetData.setFacilityStatus(1);
AmsDAssetData one = amsDAssetDataMapper.selectOne(new QueryWrapper<AmsDAssetData>().lambda().eq(AmsDAssetData::getFacilityCode, request.getFacilityCode())
.eq(AmsDAssetData::getDeleteState, 0));
if (one != null) {
return new BaseResponse(ResponseEnum.ERROR.getCode(),"该设备已存在!");
}
List<String> collect = securityDeviceMapper.selectList(new QueryWrapper<JcDSecurityDevice>()
.lambda().eq(JcDSecurityDevice::getDeleteState, 0))
.stream().map(JcDSecurityDevice::getImei).collect(Collectors.toList());
if (StringUtils.isNotBlank(request.getAddress())) {
amsDAssetData.setInstallAddr(request.getAddress());
JcDSecurityDevice selectOne = securityDeviceMapper.selectOne(new QueryWrapper<JcDSecurityDevice>()
.lambda().eq(JcDSecurityDevice::getImei, request.getFacilityCode()));
if (collect.contains(request.getFacilityCode())) {
// 更新安装地址
selectOne.setInstallAddr(request.getAddress());
securityDeviceMapper.updateById(selectOne);
}
}
int insert = amsDAssetDataMapper.insert(amsDAssetData);
if (insert > 0) {
// 添加资产附件信息
String attachFiles = request.getAttachFiles();
if (StringUtils.isNotBlank(attachFiles)) {
List<String> strings = Arrays.asList(attachFiles.split(","));
addAttachFiles(strings, assetId, request.getShapeType().toString(), request.getAssetName(), request.getCreateUserId());
}
return new BaseResponse(ResponseEnum.SUCCESS);
}
return new BaseResponse(ResponseEnum.ERROR.getCode(), "采集失败!");
}
88.mapper.selectJoinList()
@Override
public List<BmsDMaintainHistoryUnit> getHisList(Integer msegmentId, String type, Integer year, Integer lastYear, List<Integer> unitIds) {
MPJLambdaWrapper<BmsDMaintainHistoryUnit> objectMPJLambdaWrapper = new MPJLambdaWrapper<>();
// QueryWrapper<BmsDMaintainHistoryUnit> queryWrapper = new QueryWrapper<>();
objectMPJLambdaWrapper.selectAll(BmsDMaintainHistoryUnit.class).eq(BmsDMaintainHistoryUnit::getMsegmentId, msegmentId)
.le(BmsDMaintainHistoryUnit::getYear, year)
.eq(BmsDMaintainHistoryUnit::getYear, lastYear).eq(BmsDMaintainHistoryUnit::getDeleteState, 0);
if (StringUtils.isNotBlank(type)) {
objectMPJLambdaWrapper.like(BmsDMaintainHistoryUnit::getSchemeName, type);
}
if (CollectionUtils.isNotEmpty(unitIds)) {
objectMPJLambdaWrapper.in(BmsDMaintainHistoryUnit::getUnitId, unitIds);
}
List<BmsDMaintainHistoryUnit> list = bmsDMaintainHistoryUnitMapper.selectJoinList(BmsDMaintainHistoryUnit.class, objectMPJLambdaWrapper);
return list;
}
89.mapper.selectJoinPage()
@Override
public IPage<RmsDMsegmentResponse> selectPageList(GetMsegmentPageRequest rmsDMsegmentParam) {
if (CollectionUtils.isEmpty(rmsDMsegmentParam.getDeptIds())) {
rmsDMsegmentParam.setDeptIds(UserUtils.getDeptIds());
}
MPJLambdaWrapper mpjLambdaWrapper = selectLambdaWrapper(rmsDMsegmentParam);
return rmsDMsegmentMapper.selectJoinPage(new Page<>(rmsDMsegmentParam.getPageNum(), rmsDMsegmentParam.getPageSize()), RmsDMsegmentResponse.class, mpjLambdaWrapper);
}
90.Collectors.groupingBy(MsegmentInfoResponse::getDeptName)
@Override
public Map<String, List<MsegmentInfoResponse>> selectRouteByDept(GetMsegmentPageRequest rmsDMsegmentParam) {
MPJLambdaWrapper mpjLambdaWrapper = selectLambdaWrapper(rmsDMsegmentParam);
List<MsegmentInfoResponse> msegmentInfoResponseList = rmsDMsegmentMapper.selectJoinList(MsegmentInfoResponse.class, mpjLambdaWrapper);
Map<String, List<MsegmentInfoResponse>> listMap = msegmentInfoResponseList.stream().collect(Collectors.groupingBy(MsegmentInfoResponse::getDeptName));
return listMap;
}
91.实体中添加字段,不映射到数据库表 @TableField(exist = false)
@Data
@TableName("ria_d_project_scheme")
@ApiModel(value = "RiaDProjectScheme对象", description = "项目库维修方案表")
public class RiaDProjectScheme implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "主键")
@TableId(value = "id", type = IdType.INPUT)
private Integer id;
//关联查询出deptName存放在实体
@ApiModelProperty(value = "单位名称")
@TableField(exist = false)
private String deptName;
@ApiModelProperty(value = "指定国检车道pqi")
private Double nationalPqi;
}
92.mybatis-plus按条件查询数据库(eq、in)(构造链式结构)
List<RiaSUnitAnalysis> lstUnit = unitAnalysisMapper.selectList(new QueryWrapper<RiaSUnitAnalysis>().lambda()
.eq(RiaSUnitAnalysis::getYear, year)
.in(RiaSUnitAnalysis::getUnitId, unitIds));
93.新建含初始值的list集合
calDiffParam.lstPQI = Arrays.asList(100.0);
94.double判空
Double prior = 0.0;
boolean flag = prior.equals(Double.NaN); //prior = NaN 时,判断结果为真
prior == null || prior.equals(null) <------起不到判断作用
95.String、Integer对象比较得用equals()
String a = "test1";
if(!a.equals("test1"));
96.String判空
注:StringUtils 和 StringUtil 判空
方法\工具类 | StringUtils | StringUtil |
---|---|---|
.isEmpty() | 成立 | 成立 |
.isNotEmpty() | 成立 | 不成立 |
.isBlank() | 成立 | 不成立 |
String scheme = "";
boolean flag = StringUtils.isBlank(this.scheme);
flag = StringUtils.isNotEmpty(this.scheme);
flag = StringUtils.isEmpty(this.scheme);
97.排序
//从小到大:
lstKM.sort((a, b) -> {
Double d1 = a.getPQI();
Double d2 = b.getPQI();
return d1.compareTo(d2);
});
//从大到小:
lstKM.sort((a, b) -> {
Double d1 = a.getPQI();
Double d2 = b.getPQI();
return d2.compareTo(d1);
});
98. 遍历赋值
response.getLstBeforeRepair().forEach(param -> param.setMsegmentName(lstMsegment.get(0).getMsegmentName()));
99. list判空
if (m_lstGeneralParam == null || m_lstGeneralParam.size() <= 0) {
return;
}
100.抛出异常
if (CollectionUtils.isEmpty(lstPQI)) {
throw new BusinessException(ResponseEnum.ERROR.getCode(), "PQI不能为空");
}