亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何對值進行分組和連接?

如何對值進行分組和連接?

RISEBY 2023-06-28 15:27:31
我有這個方法: public List<IncomeChannelCategoryMap> allIncomeChannels(final List<String> list) {        final CriteriaQuery<IncomeChannelCategoryMap> criteriaQuery = builder.createQuery(IncomeChannelCategoryMap.class);        final Root<IncomeChannelMapEntity> root = criteriaQuery.from(IncomeChannelMapEntity.class);        final List<Selection<?>> selections = new ArrayList<>();        selections.add(root.get(IncomeChannelMapEntity_.incomeChannel).get(IncomeChannelEntity_.code));        selections.add(root.get(IncomeChannelMapEntity_.logicalUnitCode));        selections.add(root.get(IncomeChannelMapEntity_.logicalUnitIdent));        selections.add(root.get(IncomeChannelMapEntity_.keyword));        criteriaQuery.multiselect(selections);        Predicate codePredicate = root.get(IncomeChannelMapEntity_.incomeChannel).get(IncomeChannelEntity_.code).in(list);        criteriaQuery.where(codePredicate);        return entityManager.createQuery(criteriaQuery).getResultList();    }響應是這樣的:[{    "incomeChannelCode": "DIRECT_SALES",    "logicalUnitCode": "R_CATEGORY",    "logicalUnitIdent": "7"  },  {    "incomeChannelCode": "DIRECT_SALES",    "logicalUnitCode": "R_CATEGORY",    "logicalUnitIdent": "8"  }]我想要實現的是:  {    "incomeChannelCode": "DIRECT_SALES",    "logicalUnitCode": "R_CATEGORY",    "logicalUnitIdent": "7,8"  }有什么建議我怎樣才能實現這個目標?我嘗試過這個,我在一些例子中發現了這一點:builder.function("group_concat", String.class, root.get(IncomeChannelMapEntity_.logicalUnitIdent));但這是行不通的。還有其他建議嗎?@Data@Builder@NoArgsConstructor@AllArgsConstructorpublic class IncomeChannelCategoryMap implements Serializable {    private static final long serialVersionUID = 1L;    private String incomeChannelCode;    private String logicalUnitCode;    private String logicalUnitIdent;    private String keyword;}
查看完整描述

2 回答

?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

嘗試這個:


ArrayList<IncomeChannelCategoryMap> list = entityManager.createQuery(criteriaQuery).getResultList();


List<IncomeChannelCategoryMap> finalList = new ArrayList<>(list.stream().collect(

                 Collectors.toMap(IncomeChannelCategoryMap::getIncomeChannelCode, Function.identity(), (IncomeChannelCategoryMap i1, IncomeChannelCategoryMap i2) -> {

                     i1.setLogicalUnitIdent(i1.getLogicalUnitIdent()+","+i2.getLogicalUnitIdent());

                     return i1;

                 })).values());


 return finalList;

注意:請相應地添加您的 getter 方法,我只是假設您有這些方法名稱。


查看完整回答
反對 回復 2023-06-28
?
慕后森

TA貢獻1802條經驗 獲得超5個贊

您應該使用本機查詢方法,正如我在上一個問題中建議的那樣:


public List<IncomeChannelCategoryMap> allIncomeChannels(final List<String> list) {

    List<Object[]> resultList = entityManager.createNativeQuery(

            "select income_channel_code, logicalunit_code, string_agg(logicalunitident,',') idents, keyword from r_income_channel_map where income_channel_code in (:codes) group by logicalunit_code, income_channel_code, keyword")

            .setParameter("codes", list).getResultList();

    return resultList.stream().map(IncomeChannelCategoryMap::new).collect(Collectors.toList());

}

您需要將此構造函數添加到您的IncomeChannelCategoryMap類中:


IncomeChannelCategoryMap(Object[] objects) {

    this.incomeChannelCode = (String) objects[0];

    this.logicalUnitCode = (String) objects[1];

    this.logicalUnitIdent = (String) objects[2];

    this.keyword = (String) objects[3];

}


查看完整回答
反對 回復 2023-06-28
  • 2 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號