原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://xuchenguang.blog.51cto.com/730272/398952
第一种:推荐:
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%">
.......................