第一步:
通过 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()));
});
网友评论