您的位置:首页 > 其它

hql语句传递一个数组进行查询

2010-01-05 15:19 597 查看
以前都是传递一个int或String类型的变量进行查询,这次是传递一个String的数组,查询符合要求的数据。这里就要用到拼凑字符串。数据库是Mysql。

DAO的实现层里查询代码如下:

...
//这里只贴主要部分,其他略
/*
拼凑字符串,sql语句原型:select * from tableName where name in ('张三','李四'),这里是用问号赋值的方式,括号里不能直接写?,而要进行拼凑处理才行。
*/
String a="";
for(int i=0;i<receiverName.length;i++){
a=a+"?";
if(i<receiverName.length-1){
a=a+",";
}
}
System.out.println(a);
System.out.println(receiverName[0]);
//根据多个用户查询与之对应的id
List ids = publicDao.findByValues("select id from User where pid in (select id from Persion where name in ("+a+"))", receiverName); //这里的receiverName是传过来的String数组
//主要是上面部分
Integer[] receiverIds = new Integer[ids.size()];
for(int i = 0;i<receiverIds.length;i++){
receiverIds[i] = (Integer) ids.get(i);	//把ids里的值赋给receiverIds
}
...


希望以后采用这种in形式带数组查询时,能注意下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐