美文网首页
数据组装

数据组装

作者: _River_ | 来源:发表于2020-12-15 21:38 被阅读0次

    第一步:

    通过 deviceIdList 获取 List<Map<String, String>> MapList

    // 1:创建Map数组
    List<Map<String, String>> deviceMapList = findDeviceNoByDeviceIdList(deviceIdList)
    
    // 2:Mapper查询
    List<Map<String,String>> findDeviceNoByDeviceIdList(@Param("deviceIdList") List<Long> deviceIdList);
    
    
    ##3:具体查询SQL
    
    <select id="findDeviceNoByDeviceIdList" resultType="java.util.Map">
        SELECT
            device.device_id AS "deviceId",
            device.device_no AS "deviceNo"
            FROM device
            WHERE 
            device.device_id IN
            <foreach 
            collection="deviceIdList" item="item" open="(" separator="," close=")">
            #{item}
            </foreach>
    </select>
    
    
    
    // 4: 转换成Map  Id作为Key  对象作为Value
    Map<String, String> otherDeviceMapList= deviceMapList.stream().collect
    (Collectors.toMap(data -> data.get("deviceId"),data -> data.get("deviceNo")));
    
    
    
    //5: 进行数据组装
    deviceList.stream().filter(vo ->  otherDeviceMapList.containsKey(vo.getDeviceId().toString())).
    forEach(vo -> {
    vo.setDeviceNo(otherDeviceMapList.get(vo.getDeviceId().toString()));
    });
    
    

    相关文章

      网友评论

          本文标题:数据组装

          本文链接:https://www.haomeiwen.com/subject/mmlggktx.html