hibernate分页查询

package cn.itcast.b;

import javax.swing.Scrollable;

import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class App_page {
	private static SessionFactory sf;
	static {
		sf=new Configuration().configure().buildSessionFactory();
	}
	@Test
	public void Page(){
		Session session=sf.openSession();
		session.beginTransaction();
		
		Query q=session.createQuery("from Cat");
		ScrollableResults scroll=q.scroll();//得到滚动的结果集
		scroll.last();//滚动到最后一行
		int totalCount=scroll.getRowNumber()+1;//得到滚动的记录数,即总数据数
		
		//设置分页参数
		q.setFirstResult(0);
		q.setMaxResults(3);
		
		System.out.println(q.list());
		System.out.println("总记录数"+totalCount);
		
		session.getTransaction();
		session.close();
	}
}

你可能感兴趣的:(hibernate)