gpt4 book ai didi

mysql - Spring JDBC 模板 - 如何通过单个查询检索具有多个参数的多个结果

转载 作者:行者123 更新时间:2023-11-30 00:51:14 25 4
gpt4 key购买 nike

我有一个简单的查询

select distinct roleid,firstname,lastname where role_id='210';


我使用相同的查询来获取不同的列表,例如项目名称、项目经理名称、开发人员...等。基于我传递的角色ID。我的问题是我需要通过传递所有角色 ID 来一次性运行上述查询,并在一次点击中检索它们,并将这些列表分配给不同的列表,例如经理列表、开发人员列表、测试人员列表等,然后将其发送到UI 将它们放入链接的 hasmap 中。
示例

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;

上述所有方法 getProjectManager(xxx)、getResourceOwner(yyy)、getReqLeadPri(zzz) 都使用与上述相同的查询运行,但传递不同的角色 ID xxx、yyy、zzz。

我不知道如何从传递不同参数的单个查询中获取不同的列表并分配给列表并返回结果。

我试图实现这一点,因为当我每次尝试从 UI <-> DB 单独获取列表时,当每次调用相同的查询传递不同的参数以获取不同的列表时,UI 非常慢。因此,我试图一次性获得结果。

最佳答案

如果你只想执行一条sql语句,你可以使用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;
}

}

关于mysql - Spring JDBC 模板 - 如何通过单个查询检索具有多个参数的多个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20950571/

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