Hibernate HQL示例十一:分页查询
分页查询:
select * from t_student limit 1,2;从第2条开始(包括第2条),共查询2条数据
?测试:
?
package com.bjsxt.hibernate;import java.util.Iterator;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import junit.framework.TestCase;/** * 分页查询测试 * @author Administrator * */public class PageQueryTest extends TestCase {public void testQuery1() {Session session = null;try {session = HibernateUtils.getSession();Query query = session.createQuery("from Student");query.setFirstResult(2);//从第3条开始,第一条应是query.setFirstResult(0)query.setMaxResults(5);//每次查询多少条List students = query.list();for (Iterator iter = students.iterator();iter.hasNext();) {Student student = (Student)iter.next();System.out.println(student.getId() + ", " + student.getName());}}catch(Exception e) {e.printStackTrace();}finally {HibernateUtils.closeSession(session);}}}
?