读书人

hibernate根据条件查询的有关问题

发布时间: 2012-12-19 14:13:14 作者: rapoo

hibernate根据条件查询的问题
本帖最后由 mutou269874721 于 2010-10-15 17:43:54 编辑 项目用的是SSH框架。。现在做到一个根据两个条件查询数据库的问题。不知道数据库语句该怎么写了。
请大家指教一下。。


public List<Restaurant> getListSerach(String add, String rType,
int pageSize, int pageNo) {
Session session=super.getSession();
String hql="from Restaurant res where res.foodAddress like %?% and res.foodType like %?% order by newid()";
Query query=session.createQuery(hql);
query.setString(0, add);
query.setString(1, rType);
List<Restaurant> list=query.list();
return list;
}


下面的是分页的统计总数代码:

public int getTotalSerach(String add, String rType) {
Session session=super.getSession();
String hql="select count(res.id) from Restaurant res where res.foodAddress like %"+'?'+"% and res.foodType like %"+'?'+"% ";
Query query=session.createQuery(hql);
query.setString(0, rType);
Long record=(Long) query.uniqueResult();
return record.intValue();
}



再把自己action的代码贴出来


public ActionForward getListSerach(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
int pageSize=8;
int pageNo=Integer.parseInt(request.getParameter("reqPage"));
String add=new String(request.getParameter("add").getBytes("ISO-8859-1"),"UTF-8");
String rType=new String(request.getParameter("rType").getBytes("ISO-8859-1"),"UTF-8");
List<Restaurant> list=resBiz.getListSerach(add, rType, pageSize, pageNo);
int record=resBiz.getTotalSerach(add, rType);
request.setAttribute("add", add);
request.setAttribute("rType", rType);
request.setAttribute("list", list);
request.setAttribute("currentPage", pageNo);
request.setAttribute("pageSize", pageSize);
request.setAttribute("maxPage", record % pageSize == 0 ?
record/ pageSize : (record / pageSize + 1));
return mapping.findForward("viewResList");
}


上面两个的hql语句都是错的。。请大家指教一下


用模糊查询的时候,这个用法应该是怎样的呢??

[最优解释]
like ? and res.foodType like ?
query.setString(0, "%"+add+"%");


[其他解释]
那就是因为你条件不满足导致没有结果的啊
[其他解释]
like :a and res.foodType like :b
query.setParameter("a", "%"+add+"%");

[其他解释]
引用:
like :a and res.foodType like :b
query.setParameter("a", "%"+add+"%");


你好,很感谢你的帮助,语句能正常执行了
但是不能取得结果
下面的是我打印的hql语句和取值list与record:
Hibernate: select restaurant0_.id as id3_, restaurant0_.foodTitle as foodTitle3_, restaurant0_.foodIntroduce as foodIntr3_3_, restaurant0_.foodAddress as foodAddr4_3_, restaurant0_.foodGroup as foodGroup3_, restaurant0_.foodType as foodType3_ from yule.dbo.restaurant restaurant0_ where (restaurant0_.foodAddress like ?) and (restaurant0_.foodType like ?) order by newid()


Hibernate: select count(restaurant0_.id) as col_0_0_ from yule.dbo.restaurant restaurant0_ where (restaurant0_.foodAddress like ?) and (restaurant0_.foodType like ?)
list:0
record:0

都是0,这是为什么呢??
我用了你的语句,代码是这样的:


public List<Restaurant> getListSerach(String add, String rType,
int pageSize, int pageNo) {
Session session=super.getSession();
String hql="from Restaurant res where res.foodAddress like ? and res.foodType like ? order by newid()";
Query query=session.createQuery(hql);
query.setString(0, "%"+add+"%");
query.setString(1, "%"+rType+"%");
List<Restaurant> list=query.list();
return list;
}


public int getTotalSerach(String add, String rType) {
Session session=super.getSession();
String hql="select count(res.id) from Restaurant res where res.foodAddress like ? and res.foodType like ? ";
Query query=session.createQuery(hql);
query.setString(0, "%"+add+"%");
query.setString(1, "%"+rType+"%");
Long record=(Long) query.uniqueResult();
return record.intValue();
}

[其他解释]
请大家帮帮忙啊

[其他解释]
and 换成or 试试!
[其他解释]
该回复于2010-10-16 10:48:41被版主删除
[其他解释]
引用:
and 换成or 试试!


条件是要两个条件同时满足啊,不能换的
[其他解释]
没有。。条件是满足的,我在sqlserver上测试过
[其他解释]
感谢大家的帮忙。。

现在已经解决了,是自己没有设置编码格式,竟然出现乱码了都不知道。。
[其他解释]
没错,这也是一种可能,应该要尽早发现的
[其他解释]
学习了,呵呵

读书人网 >J2EE开发

热点推荐