HQL (七) 分页查询

分页查询(重要)
* setFirstResult(),数据的起始位置,从0开始
* setMaxResults, 每页显示多少条数据
参见:PageQueryTest.java

package com.bjsxt.hibernate;

import java.util.Iterator;
import java.util.List;


import org.hibernate.Session;

import junit.framework.TestCase;

public class PageQueryTest extends TestCase {

	public void testQuery1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();

			List students = session.createQuery("from Student")
									.setFirstResult(1)
									.setMaxResults(2)
									.list();
			
			for (Iterator iter =students.iterator(); iter.hasNext();) {
				Student student = (Student)iter.next();
				System.out.println(student.getName());
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
	
}
		

你可能感兴趣的:(java,Hibernate,JUnit)