最近做项目碰到一个Hibernate的多字段查询时,如何接收的问题,如接收多个查询函数的结果。

JAVA:
Java代码  

  1. //**************************    
  2.         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)";    
  3.             
  4.         if(goodCode!=null&&!"".equals(goodCode)){    
  5.             hql +=" and a.productId=:goodCode";    
  6.         }    
  7.         if(goodName!=null&&!"".equals(goodName)){    
  8.             hql +=" and a.productName like :goodName";    
  9.         }    
  10.         hql+=" order by a.status asc,a.productId desc";    
  11.         query = sessionFactory.getCurrentSession().createQuery(hql);    
  12.         if(goodCode!=null&&!"".equals(goodCode)){    
  13.             query.setString("goodCode", goodCode);    
  14.         }    
  15.         if(goodName!=null&&!"".equals(goodName)){    
  16.             query.setParameter("goodName""%"+goodName+"%");    
  17.         }    
  18.             
  19.             
  20.         int spag = (pag - 1) * pagnum;       
  21.         query.setFirstResult(spag);    
  22.         query.setMaxResults(pagnum);    
  23.         List results = query.list();    
  24.         List<Map> lm = new ArrayList();    
  25.         for (Iterator iterator = results.iterator(); iterator.hasNext(); ) {     
  26.             Object[] rows = (Object[]) iterator.next();     
  27.             Map map = new HashMap();    
  28.             map.put("productId", (Integer) rows[0]);    
  29.             map.put("productName", (String) rows[1]);    
  30.             map.put("productPrice", (Double) rows[2]);    
  31.             map.put("validDate", CommonUtil.fmtDate((Date) rows[3]));    
  32.             map.put("productNum", (Long) rows[4]);    
  33.             lm.add(map);    
  34.                 
  35.         }     



在页面上用struts2标签:


Jsp代码  



  1. <s:iterator value="listMap" status="L">    
  2.                <TR bgcolor=<s:if test="#L.odd">"#FFFFFF"</s:if><s:else>"#F6F8F8"</s:else>>    
  3.                <td><s:property value="productId"/></td>    
  4.                <td><s:property value="productName"/></td>    
  5.                <td><s:property value="productPrice"/></td>    
  6.                <td><s:property value="validDate"/></td>    
  7.                <td><s:property value="productNum"/></td>    
  8.                </TR>    
  9.               </s:iterator>  

本文转载自http://www.itjianghu.net/120107/40915570046296307.htm