1. 程式人生 > 其它 >Java中根據兩個欄位查詢資料並形成Map的方式

Java中根據兩個欄位查詢資料並形成Map的方式

如題,分頁列表介面返回的資料中 preWarningIsUpload欄位需要額外查詢另外一張表去獲取資料判斷

    /**
     * 反饋檔案是否上傳table
     *
     * @param pageList 超標資料列表
     * @return table Table<String, String, String> k1:事件ID, k2:事件型別, value:反饋結果表ID
     */
    public Table<String, String, String> isUploadTable(List<AgendaExceedResult> pageList) {
        
if (CollectionUtil.isEmpty(pageList)) { return HashBasedTable.create(); } List<Map<String, String>> params = new ArrayList<>(); pageList.forEach(o -> { Map<String, String> map = new HashMap<>(); //裝置站點型別 map.put("incidentId", o.getIncidentId());
//因子編碼 map.put("incidentType", o.getAlarmType()); params.add(map); }); List<MaintenanceFeedbackResult> dPolFacts = feedbackResultMapper.batchSelectByCondition(params); Table<String, String, String> table = HashBasedTable.create(); dPolFacts.forEach(o
-> table.put(o.getIncidentId(), o.getIncidentType(), o.getId())); return table; }

構造一個List<Map<String, String>>的列表,遍歷resultList中每條資料將incidentId和incidentType加入到Map中

    /**
     * 批量根據事件ID和事件型別查詢資料
     *
     * @param list List<Map<String,String>> list
     * @return List<DPolFact>
     */
    List<MaintenanceFeedbackResult> batchSelectByCondition(@Param("list") List<Map<String, String>> list);
    <select id="batchSelectByCondition" resultType="com.scdq.env.voc.dataobject.MaintenanceFeedbackResult">
        select * from d_maintenance_feedback_result
        <where>
            <if test='list != null and list.size() != 0 '>
                and (incident_id,incident_type) IN
                <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>
                    (#{item.incidentId}, #{item.incidentType})
                </foreach>
            </if>
            and is_delete = 0
        </where>
    </select>

通過上面的sql語句查詢資料,並將資料放入guava的HashBasedTable中,該容器的特點是,通過rowKey,columnKey兩個值確定一個value值,這樣就可以構造出兩個條件的Map

 1     /**
 2      * 填充反饋結果檔案是否已上傳
 3      */
 4     private void fillIsUpload(List<AgendaExceedResult> pageList, List<AgendaExceedPageResVo> result) {
 5         if (CollectionUtil.isEmpty(pageList)) {
 6             return;
 7         }
 8 
 9         Table<String, String, String> uploadTable = isUploadTable(pageList);
10 
11         result.forEach(o -> {
12             String incidentId = o.getIncidentId();
13             String incidentType = o.getIncidentType();
14             String feedbackId = uploadTable.get(incidentId, incidentType);
15             if (ZERO.equals(incidentType)) {
16                 o.setPreWarningIsUpload(StrUtil.isNotBlank(feedbackId) ? Boolean.TRUE : Boolean.FALSE);
17             } else if (ONE.equals(incidentType)) {
18                 o.setFirstLevelIsUpload(StrUtil.isNotBlank(feedbackId) ? Boolean.TRUE : Boolean.FALSE);
19             } else {
20                 o.setSecondLevelIsUpload(StrUtil.isNotBlank(feedbackId) ? Boolean.TRUE : Boolean.FALSE);
21             }
22         });
23     }

注意:

  上面List<Map<String, String>>的集合,最好的建議是使用Set<T>而泛型則是一個物件,該物件需重寫hashCode和equal方法,利用Set的特性對資料進行去重。