为了账号安全,请及时绑定邮箱和手机立即绑定

如何对值进行分组和连接?

如何对值进行分组和连接?

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 关注
  • 117 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信