当前位置:   article > 正文

java 常见方法,mapper,初始化list集合时赋值,批量新增-saveBatch,new Date(),各种判空,String、Integer对象比较equals(),排序,抛异常,分页查询_java savebatch

java savebatch

80.ConvertUtil

 List<GsFinalReport> gsFinalReports = ConvertUtil.convertList(result, GsFinalReport.class);
  • 1

81.ArrayUtils.toString()

将数字转换为字符串;

List<String> lst = Arrays.asList("a","b","c");
String str = ArrayUtils.toString(lst);
System.out.println(a); 		//[a,b,c]
  • 1
  • 2
  • 3

82.mapper.selectList(),返回某一字段的集合

通过实体类mapper赛选出结果,在结果集中取某一字段的结果集。

        List<String> collect = securityDeviceMapper.selectList(
        	new QueryWrapper<JcDSecurityDevice>()
                .lambda().eq(JcDSecurityDevice::getDeleteState, 0))
                .stream().map(JcDSecurityDevice::getImei).collect(Collectors.toList());
  • 1
  • 2
  • 3
  • 4

83.mapper.selectOne(new QueryWrapper().lambda() .eq(AmsDAssetData::getDeleteState, 0))

从数据库查询一个符合某些条件的实体;

		//amsDAssetDataMapper : 实体对应的mapper,用于操作数据库
        AmsDAssetData one = amsDAssetDataMapper.selectOne(
        //AmsDAssetData :实体类名称
        	new QueryWrapper<AmsDAssetData>()
        		.lambda()
        		.eq(AmsDAssetData::getFacilityCode, request.getFacilityCode())
        		.eq(AmsDAssetData::getDeleteState, 0)
            );
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

84.new Date()

        amsDAssetData.setCreateTime(new Date());
  • 1

85.批量新增-saveBatch

  /**
     * 添加信息
     *
     * @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);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

86.初始化list集合时赋值

List<String> lst = Arrays.asList("明奎","Dk","The 8","胜宽");
  • 1

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(), "采集失败!");
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

88.MPJLambdaWrapper,mapper.selectJoinList(),CollectionUtils.isNotEmpty(lst),.like(),.in()

@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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

92.mybatis-plus按条件查询数据库(eq、in)(构造链式结构)

List<RiaSUnitAnalysis> lstUnit = unitAnalysisMapper.selectList(new QueryWrapper<RiaSUnitAnalysis>().lambda()
.eq(RiaSUnitAnalysis::getYear, year)
.in(RiaSUnitAnalysis::getUnitId, unitIds));
  • 1
  • 2
  • 3

93.新建含初始值的list集合

calDiffParam.lstPQI = Arrays.asList(100.0);
  • 1

94.double判空

Double prior = 0.0;
boolean flag = prior.equals(Double.NaN); //prior = NaN 时,判断结果为真

prior == null || prior.equals(null) <------起不到判断作用
  • 1
  • 2
  • 3
  • 4

95.String、Integer对象比较得用equals()

String a = "test1";
if(!a.equals("test1"));
  • 1
  • 2

96.String判空

注:StringUtils 和 StringUtil 判空

方法\工具类StringUtilsStringUtil
.isEmpty()成立成立
.isNotEmpty()成立不成立
.isBlank()成立不成立
String scheme = "";
boolean flag = StringUtils.isBlank(this.scheme);
flag = StringUtils.isNotEmpty(this.scheme);
flag = StringUtils.isEmpty(this.scheme);
  • 1
  • 2
  • 3
  • 4

97.list判空

 if (m_lstGeneralParam == null || m_lstGeneralParam.size() <= 0) {
            return;
        }
  • 1
  • 2
  • 3

97.实体对象判空 (==null或!=null)

 AmsDAssetData one = amsDAssetDataMapper.selectOne(new QueryWrapper<AmsDAssetData>().lambda().
                .eq(AmsDAssetData::getDeleteState, 0));
        if (one != null) {
            return new BaseResponse(ResponseEnum.ERROR.getCode(),"该设备已存在!");
        }
  • 1
  • 2
  • 3
  • 4
  • 5

98. 遍历赋值

response.getLstBeforeRepair().forEach(param -> param.setMsegmentName(lstMsegment.get(0).getMsegmentName()));
  • 1

99. 排序

//从小到大:
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);
        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

100.抛出异常

if (CollectionUtils.isEmpty(lstPQI)) {
    throw new BusinessException(ResponseEnum.ERROR.getCode(), "PQI不能为空");
}
  • 1
  • 2
  • 3

101.在实现层抛出异常

if (one != null) {
            return new BaseResponse(ResponseEnum.ERROR.getCode(),"该设备已存在!");
        }
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/77380?site
推荐阅读
相关标签
  

闽ICP备14008679号