如何显示列表<String>?

How can I display a List<String>?

我有一个列表,我想通过 system.out.println()

打印这个列表

我在存储库中的查询是这样的:"

@Query("select groupe, AVG(tauxCharge) from EquilibrageFrequentiel E where 
tauxCharge is not null and tauxCharge != '0'"
        + " and base=:base group by groupe")
public List<String> tauxDeChargeParLigne(@Param("base")Long base);"       

在我的控制器中,我尝试按组显示列表中的每个元素

List<String> tauxDeChargeAssemblage1 = EquilibrageFrequentielService.tauxDeChargeAssemlage(base1);
    for (String tauxDeChargeAssemblage : tauxDeChargeAssemblage1)
    {
        System.out.println(tauxDeChargeAssemblage);
    }

我收到这些错误:"java.lang.Double cannot be cast to java.lang.String" 我该怎么做才能显示我的列表?提前谢谢你。

您需要使用投影。如此方便的方法是创建一个描述查询数据的 DTO returns(String(?),Integer/Long 的元组):

@Query("select new org.example.MyDTO(groupe, AVG(tauxCharge))"
    + " from EquilibrageFrequentiel E"
    + " where tauxCharge is not null and tauxCharge != '0'"
    + " and base=:base group by groupe")
public List<MyDTO> tauxDeChargeParLigne(@Param("base")Long base);       

DTO 是这样的:

package org.example;

@AllArgsConstructor
@Getter
public class MyDTO {
    private String groupe; // or whatever is ther data type of your groupe
    private Long average;
}

或者您可能只想要一个 groupe 的列表(假设它是一个字符串),在这种情况下,只需删除 AVG 部分,例如:

@Query("select groupe
    + " from EquilibrageFrequentiel"
    + " E where tauxCharge is not null and tauxCharge != '0'"
    + " and base=:base group by groupe")
public List<String> tauxDeChargeParLigne(@Param("base")Long base);"