Hibernate分页查询方法实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MadCode2222222222222/article/details/78928498


计算最大页数的方法:

/**
	 * 计算最大页数
	 *   @param rows 每页记录数
	 *            
	 * 
	 * */
	public int findMaxPage(int rows) {
		int maxpage = 0;// 最大页数
		int maxrow = 0;// 总记录数

		String hql = "select count(stuId) from Student";

		Query query = getSession().createQuery(hql);
		maxrow = Integer.parseInt(query.list().get(0).toString());

		maxpage = maxrow == 0 ? 1 : (maxrow % rows == 0 ? maxrow / rows
				: maxrow / rows + 1);
		return maxpage;
	}


分页获取数据的方法:

/**
	 * 分页
	 * 
	 * @param page
	 *            当前页数
	 * @param rows
	 *            每页记录数
	 * */
	public List<Student> findPageAll(int page, int rows) {
		String hql = "from Student  order by stuId";
		Query query = getSession().createQuery(hql);
		query.setFirstResult((page - 1) * rows);// (当前页数-1)*每页记录数
		query.setMaxResults(rows);// 每页记录数
		return query.list();
	}




猜你喜欢

转载自blog.csdn.net/MadCode2222222222222/article/details/78928498