通过使用Query接口的setMaxResults()方法和setFirstResults()可以实现分页。
示例:
public void testPage(){
Session session = null;
session = HibernateUtils.getCurrentSession();
String hql = "from Guestbook";
Query query = session.createQuery(hql);
query.setMaxResults(10);
query.setFirstResult(5);
List<Guestbook> list = query.list();
HibernateUtils.closeSession(session);
Iterator<Guestbook> it = list.iterator();
System.out.println("-----------------查询结果----------------");
int i = 0;
while(it.hasNext()){
Guestbook g = (Guestbook) it.next();
System.out.println("-----------第"+i+"条结果的信息-------------");
System.out.println(g.toString());
i++;
}
session.close();
}
示例:
public void testPage(){
Session session = null;
session = HibernateUtils.getCurrentSession();
String hql = "from Guestbook";
Query query = session.createQuery(hql);
query.setMaxResults(10);
query.setFirstResult(5);
List<Guestbook> list = query.list();
HibernateUtils.closeSession(session);
Iterator<Guestbook> it = list.iterator();
System.out.println("-----------------查询结果----------------");
int i = 0;
while(it.hasNext()){
Guestbook g = (Guestbook) it.next();
System.out.println("-----------第"+i+"条结果的信息-------------");
System.out.println(g.toString());
i++;
}
session.close();
}
转载于:https://blog.51cto.com/wujuxiang/408973