hibernate_数据分页, 查询总数

hibernate中, 查询数据进行分页:

	public void testPage(){
		int currentPage = 2;//第几页数据
		int pageSize = 3;//每页多少条数据
		
		Session session = HibernateUtil.getInstance().getSession();
		
		//limit ?, ?
		List l = session.createQuery("FROM Employee")
			.setFirstResult((currentPage-1)*pageSize)
			.setMaxResults(pageSize)
			.list();
		for(Employee e : l){
			System.out.println(e);
		}
		
		session.close();
	}
查询数据总数:

	public void testTotalCount(){
		Session session = HibernateUtil.getInstance().getSession();
		
		//select count(*) from employee
//		List l = session.createQuery("SELECT COUNT(e) FROM Employee e").list();
//		System.out.println(l.get(0));
		Long total = (Long) session.createQuery("SELECT COUNT(e) FROM Employee e").uniqueResult();
		System.out.println(total);
		session.close();
	}







你可能感兴趣的:(hibernate)