最近做项目碰到一个Hibernate的多字段查询时,如何接收的问题,如接收多个查询函数的结果。
JAVA:
Java代码
- //**************************
- hql="select a.productId,a.productName,a.productPrice,a.validDate,(select nvl(sum(b.orderNum-b.refundNum),0) from Order b where b.orderStatus not in (1,7,8) and b.product.productId=a.productId) from Product a where a.status in (1,3)";
- if(goodCode!=null&&!"".equals(goodCode)){
- hql +=" and a.productId=:goodCode";
- }
- if(goodName!=null&&!"".equals(goodName)){
- hql +=" and a.productName like :goodName";
- }
- hql+=" order by a.status asc,a.productId desc";
- query = sessionFactory.getCurrentSession().createQuery(hql);
- if(goodCode!=null&&!"".equals(goodCode)){
- query.setString("goodCode", goodCode);
- }
- if(goodName!=null&&!"".equals(goodName)){
- query.setParameter("goodName", "%"+goodName+"%");
- }
- int spag = (pag - 1) * pagnum;
- query.setFirstResult(spag);
- query.setMaxResults(pagnum);
- List results = query.list();
- List<Map> lm = new ArrayList();
- for (Iterator iterator = results.iterator(); iterator.hasNext(); ) {
- Object[] rows = (Object[]) iterator.next();
- Map map = new HashMap();
- map.put("productId", (Integer) rows[0]);
- map.put("productName", (String) rows[1]);
- map.put("productPrice", (Double) rows[2]);
- map.put("validDate", CommonUtil.fmtDate((Date) rows[3]));
- map.put("productNum", (Long) rows[4]);
- lm.add(map);
- }
在页面上用struts2标签:
Jsp代码
- <s:iterator value="listMap" status="L">
- <TR bgcolor=<s:if test="#L.odd">"#FFFFFF"</s:if><s:else>"#F6F8F8"</s:else>>
- <td><s:property value="productId"/></td>
- <td><s:property value="productName"/></td>
- <td><s:property value="productPrice"/></td>
- <td><s:property value="validDate"/></td>
- <td><s:property value="productNum"/></td>
- </TR>
- </s:iterator>
转载于:https://blog.51cto.com/3595505/763007