我想遵循其他示例,例如如何在 JDBCTemplates 中使用 SELECT IN 子句?在我的查询中使用 in 子句,但我从 queryForInt 收到此错误:
The method queryForInt(String, Object[]) in the type JdbcTemplate is not applicable for the arguments (String, Map)
这是我的代码:
List groupList = getGroupsForUser(username);
List<String> groupPara = new ArrayList<String>();
for (Iterator groups = groupList.iterator(); groups.hasNext();) {
Group g = (Group) groups.next();
groupPara.add(g.getGroupName());
}
Map namedParameters = Collections.singletonMap("listOfValues", groupPara);
int groupPermissions = getJdbcTemplate().queryForInt( "SELECT MAX(roleid) FROM groupaccess "
+ "WHERE inode = '"+inode.getId()+"' AND groupname in (:listOfValues)", namedParameters);
return groupPermissions;
我通过这样做让它工作,虽然不是很优雅:
List groupList = getGroupsForUser(username);
String groupPara = ("(");
Object[] params = new Object[groupList.size()+1];
params[0]=inode.getId();
int index = 1;
for (Iterator groups = groupList.iterator(); groups.hasNext();) {
Group g = (Group) groups.next();
params[index]=g.getGroupName();
index++;
groupPara += " ? ,";
}
groupPara = groupPara.substring(0, groupPara.length() - 1);
groupPara+=")";
int groupPermissions = getJdbcTemplate().queryForInt( "SELECT MAX(roleid) FROM groupaccess "
+ "WHERE inode = ? AND groupname in "+groupPara, params);
return groupPermissions;