首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在java中正确检索所有行?

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

Stack Overflow用户
提问于 2019-07-08 12:08:02
回答 2查看 371关注 0票数 0

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

这是我所做的:

代码语言:javascript
复制
    @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;
EN

回答 2

Stack Overflow用户

发布于 2019-07-08 12:25:49

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

UPDATE -我认为是数据库没有返回正确的值,但在您的评论之后,我得到了问题的确切位置。请尝试修改代码,如下所示。为此,您可能需要定义3个新的数组列表和一个父列表。然后迭代从数据库中检索到的列表以形成最终列表。

代码语言:javascript
复制
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;
票数 0
EN

Stack Overflow用户

发布于 2019-07-08 14:39:11

代码语言:javascript
复制
Modifiy your model class like below

public class AutoComplete {

ArrayList<String> Annual ;
ArrayList<String> Biweekly ;
ArrayList<String> Hourly;

 public ArrayList<String>getHourly() {
        return Hourly;
    }

    public void setHourly(ArrayList<String> hourly) {
        Hourly = hourly;
    }

    public ArrayList<String> getBiweekly() {
        return Biweekly;
    }

    public void setBiweekly(ArrayList<String> biweekly) {
        Biweekly = biweekly;
    }

    public ArrayList<String> getAnnual() {
        return Annual;
    }

    public void setAnnual(ArrayList<String> annual) {
        Annual = annual;
    }

}

Now in your autocompleteRepository.findAllByJobClassCdIsContaining(String jobClassCd) Method Store all the rows in arrays and then set array in to model class return on single model object called AutoComplete .

public AutoComplete  findAllByJobClassCdIsContaining(String jobClassCd){

/// Declare arrays
ArrayList<String> Annual = new ArrayList<>();
ArrayList<String> Biweekly = new ArrayList<>();
ArrayList<String> Hourly= new ArrayList<>();

//Retrieve from DB and set into 3 arrays 

   // code 
     While(row.next()){ 
  ///  get from db and set to arrays.
     }


// set arrays to object
AutoComplete  acc=new AutoComplete  ();
acc.setHourly(Hourly);
acc.setsetBiweekly(Biweekly );
acc.setAnnual(Annual);

}

All other method in service and domain layer will be modified accordingly.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56928311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档