您的位置:首页 > 其它

分页

2015-06-04 17:54 429 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: