Java 如何使用RequestParam通过Hibernate传递多个值?
我正在我的url中传递两个参数:名称和缩写。当使用此URL接受单个值时,应用程序工作正常Java 如何使用RequestParam通过Hibernate传递多个值?,java,spring,hibernate,spring-mvc,Java,Spring,Hibernate,Spring Mvc,我正在我的url中传递两个参数:名称和缩写。当使用此URL接受单个值时,应用程序工作正常 http://localhost:8080/userSearch/locate?name=John&abbreviation=F 现在我需要缩写来处理多个值F,M 正在尝试使用这些URL之一 http://localhost:8080/userSearch/locate?name=John&abbreviation=F&abbreviation=M http://localhost
http://localhost:8080/userSearch/locate?name=John&abbreviation=F
现在我需要缩写来处理多个值F,M
正在尝试使用这些URL之一
http://localhost:8080/userSearch/locate?name=John&abbreviation=F&abbreviation=M
http://localhost:8080/userSearch/locate?name=John&abbreviation=F,M
当传递多个值时,我得到一个空字符串[]。我查看了查询,我认为HQL参数中的缩写设置不正确。我看到F和M被传递给方法,我尝试使用
String resultString = Arrays.toString(status);
和/或
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < status.length; i++) {
strBuilder.append( abbreviation[i] );
}
String resultString = strBuilder.toString();
StringBuilder strBuilder=new StringBuilder();
for(int i=0;i
下面的代码:
控制器
public List<Users> findUser(
@RequestParam(value = "name") String name,
@RequestParam(value = "abbreviation") String[] abbreviation
throws Exception{
List<Users> users=new ArrayList<Users>();
users= userService.getUser(name, abbreviation);
return users;
}
公共列表查找器(
@RequestParam(value=“name”)字符串名称,
@RequestParam(value=“缩写”)字符串[]缩写
抛出异常{
列表用户=新建ArrayList();
users=userService.getUser(名称,缩写);
返回用户;
}
服务
public List<Users> getUser(String name, String[] abbreviation)
throws Exception {
return UserImpl.locateUser(name, abbreviation);
}
public List getUser(字符串名称,字符串[]缩写)
抛出异常{
返回UserImpl.locateUser(名称、缩写);
}
Impl
public List<User> locateUser(String name, String[] abbreviation ) throws Exception {
String resultString = Arrays.toString(status);
//or
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < status.length; i++) {
strBuilder.append( status[i] );
}
String statusResults = strBuilder.toString();
StringBuffer sql = new StringBuffer();
sql.append("select user, con "
+ " from User user, Contacts con "
+ " where user.userId = con.userId");
sql.append( " and user.name =:name " );
//works with single value
sql.append( " and user.abbreviation =:abbreviation " );
//does not work with multiple value
sql.append( " and user.abbreviation =:resultString " );
Query qry = factory.getCurrentSession().createQuery(sql.toString());
qry.setParameter("name", name);
//works with single value
query.setParameter("abbreviation", abbreviation);
//doesn't work with multiple value (Returns an empty string [])
query.setParameter("resultString", resultString);
List<Object[]> userList = query.list();
public List locateUser(字符串名称,字符串[]缩写)引发异常{
字符串resultString=Arrays.toString(状态);
//或
StringBuilder strBuilder=新StringBuilder();
for(int i=0;i
如何处理来自URL的这两个值
更新
public List<Users> getUser(String name, String[] abbreviation)
throws Exception {
return UserImpl.locateUser(name, abbreviation);
}
MS.成功了。谢谢。使用ParameterList后,我得到了一个ORA-01797:此运算符后面必须跟有任何或全部。因此我将:改为in.尝试将
@RequestParam(value=“缩写”)字符串[]缩写改为@RequestParam(value=“缩写”)字符串缩写
。然后使用String[]args=缩写拆分字符串。如果使用
http://localhost:8080/userSearch/locate?name=John&abbreviation=F&abbreviation=M
并使用setParameterList
方法添加参数
query.setParameterList(“缩写”,缩写);
一切都应该对你有用
另外,请注意列表参数user.缩写in:缩写
的正确用法,如@Roman在下面的评论中所述您是否尝试使用usename=John&缩写=F&缩写=M?很好,很高兴答案是helpfull@Sunny你可以考虑接受/损坏一个帮助你解决问题的答案。将问题解决后交还给贡献者,或者HQL部分不应该从user.缩写=:缩写
更改为user.缩写中的缩写:缩写
或类似的内容吗?没错,这部分也是必要的,很好。你也使用了正确的语法