第一种:推荐:

DAO层的Hql语句如果用From.....执行出来的是一个基于Map结构的List,而用select.........执行出来的结果不是Map型的,为了方便使用、维护修改,将其转为基于Map的List。

DAO层代码如下: 

 
  
  1. public List getSlCountList(Integer year, Integer month, String office) {  
  2.     Session session = getSession();  
  3.     String hql="SELECT office,count(*) as myCount from ReportMonth " 
  4.         + "Where (year = ?) AND (month = ?) And (office like ?) And(slsn <>'') " 
  5.         + "Group by office";  
  6.     List list = null;  
  7.     List newList=new ArrayList();  
  8.     Object[] obj;  
  9.     try {  
  10.         Query query = session.createQuery(hql);  
  11.         query.setParameter(0, year);  
  12.         query.setParameter(1, month);  
  13.         query.setParameter(2"%" + office + "%");  
  14.         list = query.list();  
  15.         for(int i=0;i<list.size();i++){  
  16.             Map map = new HashMap();  
  17.             obj = (Object[])list.get(i);  
  18.             map.put("office", (String)obj[0]);  
  19.             map.put("myCount", (Integer)obj[1]);  
  20.             newList.add(map);  
  21.         }  
  22.           
  23.      } catch (HibernateException e1) {  
  24.          newList=null;  
  25.         e1.printStackTrace();  
  26.     }  
  27.     finally{              
  28.         session.close();  
  29.     }  
  30.     return newList;  
  31. }  

JSP部分代码如下:  

 
  
  1. <ww:iterator value="#request['slCountList']" id="slCountList">  
  2.   <tr class="tr0">  
  3.       <td height="26" width="15%">&nbsp;<ww:property value="#slCountList.office"/>  
  4. .................. 

 

第二种:DAO代码简洁,但是在JSP中用obj[0]obj[1]表示,感觉不好,晦涩。

 DAO:

 
  
  1. public List getSlCountList(Integer year, Integer month, String office) {  
  2.     Session session = getSession();  
  3.     String hql="SELECT office,count(*) as myCount from ReportMonth " 
  4.         + "Where (year = ?) AND (month = ?) And (office like ?) And(slsn <>'') " 
  5.         + "Group by office";  
  6.     List list = null;  
  7.     try {  
  8.         Query query = session.createQuery(hql);  
  9.         query.setParameter(0, year);  
  10.         query.setParameter(1, month);  
  11.         query.setParameter(2"%" + office + "%");  
  12.         list = query.list();        
  13.      } catch (HibernateException e1) {  
  14.          list=null;  
  15.         e1.printStackTrace();  
  16.     }  
  17.     finally{              
  18.         session.close();  
  19.     }  
  20.     return list;  
  21. }  

JSP:

 
  
  1.     <ww:iterator value="#request['slCountList']" id="slCountList">  
  2.         <tr class="tr0">  
  3.             <td height="26" width="15%">&nbsp;<ww:property value="#slCountList[0]"/><B><font color="red">(<ww:property value="#slCountList[1]"/>座)</font></B></td>  
  4.             <td width="90%">  
  5. .......................