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

时间:2019-07-08 04:08:01

标签: java spring-boot

遇到一个问题,我试图从数据库中检索数据,而我获得的唯一数据是作为第一行的一个对象。我想将我的所有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;

2 个答案:

答案 0 :(得分:1)

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

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

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;

答案 1 :(得分:0)

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.