请帮我注释一下分页查询的代码---先谢了!
public List<Object> queryList(final String hql,final Integer showNumber,final Integer beginNumber) {
List list = null;
try {
list = this.getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws
HibernateException,
SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(beginNumber);
query.setMaxResults(showNumber);
List list = query.list();
return list;
}
});
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
------解决方案--------------------public List<Object> queryList(final String hql,final Integer showNumber,final Integer beginNumber) {
List list = null;
try {
list = this.getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
//执行sql
Query query = session.createQuery(hql);
//本次查询的起始条数
query.setFirstResult(beginNumber);
//最后条数
query.setMaxResults(showNumber);
List list = query.list();
return list;
}
});
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
------解决方案--------------------
Java code
public List<Object> queryList(final String hql,final Integer showNumber,final Integer beginNumber) {
List list = null;
try {
list = this.getHibernateTemplate().executeFind( //以下实例化HibernateCallback对象,匿名内部类,类实现或重写了doInHibernate方法
new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql); //根据hql语句,生成查询对象
query.setFirstResult(beginNumber); //设置查询对象从哪行数据起查询
query.setMaxResults(showNumber); //设置查询对象返回结果的最大个数。
List list = query.list(); //执行查询
return list;
}
});
} catch (Exception e) {
e.printStackTrace();
}
return list;
}