分页

本文介绍了一个通用的分页查询工具类QueryResultUtil的实现方法,并展示了如何在DAO层中使用该工具类来处理分页查询。此工具类能够方便地设置分页参数并返回查询结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.List;

public class QueryResultUtil<T> {

	private int pageNo = 1;
	private int pageSize = 8;
	
	private int totalItems;
	private List<T> list;

	public QueryResultUtil() {

	}

	public QueryResultUtil(int totalItems, List<T> adverMap) {
		this.totalItems = totalItems;
		this.list = adverMap;
	}
	
	public QueryResultUtil(int pageNo, int pageSize, int totalCount, List<T> list) {
		if (totalCount <= 0)
			this.totalItems = 0;
		else {
			this.totalItems = totalCount;
		}
		if (pageSize <= 0)
			this.pageSize = 20;
		else {
			this.pageSize = pageSize;
		}
		if (pageNo <= 0)
			this.pageNo = 1;
		else {
			this.pageNo = pageNo;
		}
		if ((this.pageNo - 1) * this.pageSize >= totalCount) {
			this.pageNo = (totalCount / pageSize);
		}
		this.list = list;
	}

	public int getPageNo() {
		return this.pageNo;
	}

	public int getPageSize() {
		return this.pageSize;
	}

	public int getTotalPage() {
		int totalPage = this.totalItems / this.pageSize;
		if (this.totalItems % this.pageSize != 0) {
			totalPage++;
		}
		return totalPage;
	}

	public boolean isFirstPage() {
		return this.pageNo <= 1;
	}

	public boolean isLastPage() {
		return this.pageNo >= getTotalPage();
	}

	public int getNextPage() {
		if (isLastPage()) {
			return this.pageNo;
		}
		return this.pageNo + 1;
	}

	public int getPrePage() {
		if (isFirstPage()) {
			return this.pageNo;
		}
		return this.pageNo - 1;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getFirstResult() {
		 return (this.pageNo - 1) * this.pageSize;
	}

	public int getTotalItems() {
		return totalItems;
	}

	public void setTotalItems(int totalItems) {
		this.totalItems = totalItems;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

}

Dao层调用:
return hibernateTemplate.execute(new HibernateCallback<QueryResultUtil<Map<String, Object>>>() {

			@SuppressWarnings("unchecked")
			@Override
			public QueryResultUtil<Map<String, Object>> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createSQLQuery(countSql);
				Number number = (Number) query.uniqueResult();
				int totalCout = number.intValue();
				QueryResultUtil<Map<String, Object>> queryResult = new QueryResultUtil<Map<String, Object>>(startIndex, pageSize, totalCout, null);
				query = session.createSQLQuery(sql).setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
						.setFirstResult(queryResult.getFirstResult()).setMaxResults(queryResult.getPageSize());
				queryResult.setList(query.list());
				return queryResult;
			}
			
		});



<div class="pageContainer">
	<c:if test="${currentpage != 1}">
		<a href="/cate/${name}/page/${currentpage-1}">上一页</a>
		<a href="/cate/${name}/page/1">1</a>
		<c:if test="${currentpage-1 != 1}"><span>...</span>
			<a href="/cate/${name}/page/${currentpage-1}">${currentpage-1}</a>
		</c:if>
	</c:if>
	<a href="javascript:;" style="color:#6CF;">${currentpage}</a>
	<c:if test="${currentpage != pagenum}">
		<c:if test="${currentpage+1 != pagenum}">
			<a href="/cate/${name}/page/${currentpage+1}">${currentpage+1}</a>
			<span>...</span>
		</c:if>
		<a href="/cate/${name}/page/${pagenum}">${pagenum}</a>
		<a href="/cate/${name}/page/${currentpage+1}">下一页</a>
	</c:if>
</div>

效果请看: http://dianying.ky620.com/cate/kehuan/page/1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值