遇到一个问题,我试图从我的数据库中检索数据,而我得到的唯一数据是我的第一行对象。我想让我的所有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;发布于 2019-07-08 12:25:49
您确定criteria findAllByJobClassCdIsContaining(jobClassCd)返回所有3个条目吗?有些地方我觉得这是您的自定义方法,而不是JPA Repository提供的原始方法。尝试使用findAllByJobClassCdContaining(jobClassCd)或简单的findAll()来查看它是否返回1个以上的条目。
UPDATE -我认为是数据库没有返回正确的值,但在您的评论之后,我得到了问题的确切位置。请尝试修改代码,如下所示。为此,您可能需要定义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;发布于 2019-07-08 14:39:11
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.https://stackoverflow.com/questions/56928311
复制相似问题