问题
I have a simple query which is
select distinct roleid,firstname,lastname where role_id='210';
I am using the same query for fetching the different lists such as projectnames ,projectmanagers name,developers...etc..based on the roleid that I am passing.
My problem is I need to run the above query in a single shot by passing all the roleid and retrieve them in a single hit and assign those list to different list such as managers list ,developers list ,testers list ..etc and send it to the UI putting them in a linked hasmap.
Example
LinkedHashMap<String, List<SelectItem>> results = new LinkedHashMap<String, List<SelectItem>>();
List<SelectItem> getProjectManager = getProjectManager(xxx);
List<SelectItem> getResourceOwnerSE = getResourceOwner(yyy);
List<SelectItem> getReqLeadPri = getReqLeadPri(zzz);
results.put("getProjectManager", getProjectManager);
results.put("getResourceOwner", getResourceOwnerSE);
results.put("getReqLeadPri", getReqLeadPri);
return results;
All the above methods getProjectManager(xxx),getResourceOwner(yyy),getReqLeadPri(zzz) runs with same query as mentioned above but passing different roleid xxx,yyy,zzz.
I don't know how to fetch a different list from a single query passing different parameters and assign to the list and return the results.
I am trying to achieve this because the UI is very slow when I try to get the lists individually from UI <-> DB each time when calling the same query passing different parameters each time for fetching different list. Hence, I am trying to get the results in a single shot.
回答1:
If you want to fir only one sql statement you can use ResultSetExtractor
public class SelectItemResultSetExtractor implements ResultSetExtractor<LinkedHashMap<String, List<SelectItem>>>{
public LinkedHashMap<String, List<SelectItem>> extractData(ResultSet rs) throws SQLException,
DataAccessException {
LinkedHashMap<String, List<SelectItem>> result = new ...
//put the 3 categories with empty arraylists
while(rs.next()){
SelectItem item= new SelectItem();
item.setRoleid(rs.getInt(1))
item.setFirstName(rs.getInt(2));
item.setLastName(rs.getString(3));
//if item.getRoleid() is ProjManager
// then put in the list of the ProjManager
result.get("ProjManager").add(item);
//if item.getRoleid() is ResourceOwnerSE
// then put in the list of the ResourceOwnerSE
...
}
return result;
}
}
来源:https://stackoverflow.com/questions/20950571/spring-jdbc-template-how-to-retrieve-multiple-result-with-multiple-parameters-w