第一种:推荐:
DAO层的Hql语句如果用From.....执行出来的是一个基于Map结构的List,而用select.........执行出来的结果不是Map型的,为了方便使用、维护修改,将其转为基于Map的List。
DAO层代码如下:
- public List getSlCountList(Integer year, Integer month, String office) {
- Session session = getSession();
- String hql="SELECT office,count(*) as myCount from ReportMonth "
- + "Where (year = ?) AND (month = ?) And (office like ?) And(slsn <>'') "
- + "Group by office";
- List list = null;
- List newList=new ArrayList();
- Object[] obj;
- try {
- Query query = session.createQuery(hql);
- query.setParameter(0, year);
- query.setParameter(1, month);
- query.setParameter(2, "%" + office + "%");
- list = query.list();
- for(int i=0;i<list.size();i++){
- Map map = new HashMap();
- obj = (Object[])list.get(i);
- map.put("office", (String)obj[0]);
- map.put("myCount", (Integer)obj[1]);
- newList.add(map);
- }
- } catch (HibernateException e1) {
- newList=null;
- e1.printStackTrace();
- }
- finally{
- session.close();
- }
- return newList;
- }
JSP部分代码如下:
- <ww:iterator value="#request['slCountList']" id="slCountList">
- <tr class="tr0">
- <td height="26" width="15%"> <ww:property value="#slCountList.office"/>
- ..................
第二种:DAO代码简洁,但是在JSP中用obj[0]obj[1]表示,感觉不好,晦涩。
DAO:
- public List getSlCountList(Integer year, Integer month, String office) {
- Session session = getSession();
- String hql="SELECT office,count(*) as myCount from ReportMonth "
- + "Where (year = ?) AND (month = ?) And (office like ?) And(slsn <>'') "
- + "Group by office";
- List list = null;
- try {
- Query query = session.createQuery(hql);
- query.setParameter(0, year);
- query.setParameter(1, month);
- query.setParameter(2, "%" + office + "%");
- list = query.list();
- } catch (HibernateException e1) {
- list=null;
- e1.printStackTrace();
- }
- finally{
- session.close();
- }
- return list;
- }
JSP:
- <ww:iterator value="#request['slCountList']" id="slCountList">
- <tr class="tr0">
- <td height="26" width="15%"> <ww:property value="#slCountList[0]"/><B><font color="red">(<ww:property value="#slCountList[1]"/>座)</font></B></td>
- <td width="90%">
- .......................
转载于:https://blog.51cto.com/xuchenguang/398952