gpt4 book ai didi

java - 如何在java中正确检索所有行?

转载 作者:行者123 更新时间:2023-12-02 03:20:46 25 4
gpt4 key购买 nike

遇到一个问题,我试图从数据库中检索数据,而我得到的唯一数据是我的第一行的一个对象。我希望将所有 3 行显示为列表,而不是只显示一行。下面我的代码有解决办法吗?感谢您的帮助。

这就是我所做的:

    @RequestMapping(value = "jobs", method = RequestMethod.GET)
public @ResponseBody
List<ArrayList<ArrayList<String>>> getSalary(@RequestParam(value = "autocomplete") String autocompleteValue) {

List<AutoComplete> list = autoCompleteService.retrieveSalary(autocompleteValue);
return Arrays.asList(merge(list));

}


ArrayList<ArrayList<String>> merge(List<AutoComplete> list){

ArrayList<ArrayList<String> > finalList = new ArrayList<ArrayList<String>>(3);
ArrayList<String> annual = new ArrayList<>();
ArrayList<String> biweekly = new ArrayList<>();
ArrayList<String> hourly = new ArrayList<>();

for (int i = 0; i < list.size(); i++) {
AutoComplete autoComplete = list.get(i);
if (autoComplete.getAnnual() != null) {
annual.add(autoComplete.getAnnual());
}
if (autoComplete.getBiweekly() != null) {
biweekly.add(autoComplete.getBiweekly());
}
if (autoComplete.getHourly() != null) {
hourly.add(autoComplete.getHourly());
}
}
finalList.add(annual);
finalList.add(biweekly);
finalList.add(hourly);
return finalList;

最佳答案

您确定条件 findAllByJobClassCdIsContaining(jobClassCd) 返回所有 3 个条目吗?有些地方我觉得这是您的自定义方法,而不是 JPA 存储库提供的原始方法。尝试使用 findAllByJobClassCdContaining(jobClassCd) 或简单地使用 findAll() 来查看它是否返回超过 1 个条目。

更新 - 我认为是数据库没有返回正确的值,但在您发表评论后,我知道问题到底出在哪里。请尝试如下修改您的代码。为此,您可能需要定义 3 个新的数组列表和一个父列表。然后迭代从数据库检索的列表以形成最终列表。

ArrayList<ArrayList<String> > finalList = new ArrayList<ArrayList<String>>(3); 
ArrayList<String> annual = new ArrayList<>();
ArrayList<String> biweekly = new ArrayList<>();
ArrayList<String> hourly = new ArrayList<>();

for (int i = 0; i < list.size(); i++) {
AutoComplete autoComplete = list.get(i);
if (autoComplete.getAnnual() != null) {
annual.add(autoComplete.getAnnual());
}
if (autoComplete.getBiweekly() != null) {
biweekly.add(autoComplete.getBiweekly());
}
if (autoComplete.getHourly() != null) {
hourly.add(autoComplete.getHourly());
}
}
finalList.add(annual);
finalList.add(biweekly);
finalList.add(hourly);
return finalList;

关于java - 如何在java中正确检索所有行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56928311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com