1,addScalar 显式指定返回数据的类型
String sql = "select count(*) as cnt from tblRead" + Misc.formatMoon(year, month) ...
SQLQuery query = session.createSQLQuery(sql);
query.addScalar("cnt", Hibernate.INTEGER);
...
count = (Integer) query.uniqueResult();
2, 返回的结果集是多个列的情况
String sql = "select day, count(*) as cnt from tblRead" + Misc.formatMoon(year, month) ...
SQLQuery query = session.createSQLQuery(sql);
query.addScalar("day", Hibernate.INTEGER);
query.addScalar("cnt", Hibernate.INTEGER);
...
Iterator results = query.list().iterator();
while (results.hasNext())
{
Object[] rows = (Object[]) results.next();
int day = (Integer) rows[0];
int cnt = (Integer) rows[1];
...
String sql = "select count(*) as cnt from tblRead" + Misc.formatMoon(year, month) ...
SQLQuery query = session.createSQLQuery(sql);
query.addScalar("cnt", Hibernate.INTEGER);
...
count = (Integer) query.uniqueResult();
2, 返回的结果集是多个列的情况
String sql = "select day, count(*) as cnt from tblRead" + Misc.formatMoon(year, month) ...
SQLQuery query = session.createSQLQuery(sql);
query.addScalar("day", Hibernate.INTEGER);
query.addScalar("cnt", Hibernate.INTEGER);
...
Iterator results = query.list().iterator();
while (results.hasNext())
{
Object[] rows = (Object[]) results.next();
int day = (Integer) rows[0];
int cnt = (Integer) rows[1];
...
}
SQLQuery query;
List<Object> eventIdList = query.list();
for(Iterator iterator = eventIdList.iterator();iterator.hasNext();){
Object[] objects = (Object[])iterator.next();
AttenceAnalyseVo temp= new AttenceAnalyseVo();
temp.setDeptId(objects[0].toString());
temp.setDeptName(objects[1].toString());
temp.setTotalScore(objects[2].toString());
list.add(temp);
}