java - 如何遍历 JPA 响应并从中获取值
问题描述
存储库中的代码是
@Query("select divisionName, count(stundeName)FROM StudentDetails GROUP BY divisionName")
public List<Object> getDivisionCount();
服务中的代码是
List <Object> countList= studentDetailsRepository.getDivisionCount();
现在我想遍历 countList 和 categories 然后进入不同的类别,我试过这个
for(int i=0;i< countList.size();i++ )
System.out.println(countList.get(i).getClass().getDeclaredField('count'));
它不起作用并给出错误
解决方案
最好在 JPA 响应上使用投影。一种选择是使用基于界面的投影。在您的情况下,查询可能如下所示:
@Query(value = "SELECT s.divisionName AS divisionName, count(s.stundeName) AS stundeName FROM StudentDetails AS s GROUP BY s.divisionName")
public List<StudentDetails> getDivisionCount();
然后你需要创建一个这样的接口:
public interface StudentDetails{
String getDivisionName();
String getStundeName();
}
推荐阅读
- python - Mac上的Python tkinter:直到文件浏览器打开后按钮才会突出显示
- java - 我将如何创建一个返回的方法
- typescript - 为什么联合类型中的“从不”类型毫无意义?
- android - Git 仅合并 string.xml 文件
- css - 如何根据内容调整div的高度
- html - 标签中的class =“thumbnail”在引导程序4中不起作用
- python - 无法找到列表中的项目
- keras - Keras ImageDataGenerator 验证精度低
- docker - 在 docker-compose.yml 中将环境变量从一个服务传递到另一个服务
- c# - 如何使用 WPF 和 MVVM 在 DevExpress 调度程序控件上获取保存的约会?