ssh中的分頁查詢是比較常用的,接下來我用程式碼來介紹如何實現一個分頁查詢
首先建立一個Model用來儲存查詢分頁的資訊
package com.haiziwang.qrlogin.utils;
import java.util.List;
public class prospage<T> {
private int page; // 當前頁數
private int totalCount; // 總記錄數
private int totalPage; // 總頁數
private int ererypagecount; // 每頁顯示的記錄數
private List<T> list; // 每頁顯示資料的集合.
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getErerypagecount() {
return ererypagecount;
}
public void setErerypagecount(int ererypagecount) {
this.ererypagecount = ererypagecount;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
@Override
public String toString() {
return "prospage [page=" + page + ", totalCount=" + totalCount + ", totalPage=" + totalPage
+ ", ererypagecount=" + ererypagecount + ", list=" + list + "]";
}
}
這是Dao類查詢的程式碼:
public prospage<pros> findpagebyid(int page,Integer cid){
prospage<pros> prospage = new prospage<pros>();
//設定當前頁數
prospage.setPage(page);
//顯示每夜記錄數
int limit=8;
prospage.setErerypagecount(limit);
//設定總記錄數
int totalc=0;
totalc=findcountbycid(cid);
prospage.setTotalCount(totalc);
//設定總頁數
int totalpage =0;
if(totalc % limit==0){
totalpage=totalc / limit;
}else{
totalpage=totalc / limit+1;
}
prospage.setTotalPage(totalpage);
//每頁顯示的資料
//每頁從哪開始
int a =(page-1)*limit;
List<pros> list=getpagebycid(cid,a,limit);
prospage.setList(list);
return prospage;
}
//分頁查詢
public List<pros> getpagebycid(Integer cid, int a, int limit) {
String hql = "select p from pros p join p.secondLei cs join cs.categroy c where c.cid = ?";
Query query = getSession().createQuery(hql).setInteger(0, cid);
List<pros> proslist = query.setFirstResult(a)
.setMaxResults(limit)
.list();
System.out.println("含有多少個"+proslist );
if(proslist != null && proslist.size() > 0){
return proslist ;
}
return null;
}
//根局一級分類查詢二級分類的個數
public int findcountbycid(Integer cid) {
String hql="select count(*) from pros p where p.secondLei.categroy.cid=?";
List<Long> listcount= getSession().createQuery(hql).setInteger(0, cid).list();
if(listcount != null && listcount.size() > 0){
return listcount.get(0).intValue();
}
return 0;
}
3接下來就是在Jsp顯示查詢資訊即可
<s:iterator var ="pb" value="pagebean.list">
<li>
<a href="${ pageContext.request.contextPath }/pros_findpro.action?pid=<s:property value="#pb.pid"/>">
<img src="${pageContext.request.contextPath}/<s:property value="#pb.imagepath"/>" width="170" height="170" style="display: inline-block;">
<p style='color:green'>
商品名字: <s:property value="#pb.pname"/>
</p>
<p style='color:red'>
商品價格: <s:property value="#pb.shopprice"/>
</p>
</a>
</li>
</s:iterator>
span>第 <s:property value="pagebean.page"/>/<s:property value="pagebean.totalPage"/> 頁</span>
<s:if test="cid!=null">
<a href="${ pageContext.request.contextPath }/pros_findfirstid.action?cid=<s:property value="cid"/>&page=1" class="firstPage"> </a>
<a href="${ pageContext.request.contextPath }/pros_findfirstid.action?cid=<s:property value="cid"/>&page=<s:property value="pagebean.page-1"/>" class="previousPage"> </a>
<s:iterator var="i" begin="1" end="pagebean.totalPage">
<a href="${ pageContext.request.contextPath }/pros_findfirstid.action?cid=<s:property value="cid"/>&page=<s:property value="#i"/>"><s:property value="#i"/></a>
</s:iterator>
<a class="nextPage" href="${ pageContext.request.contextPath }/pros_findfirstid.action?cid=<s:property value="cid"/>&page=<s:property value="pagebean.page+1"/>"> </a>
<a class="lastPage" href="${ pageContext.request.contextPath }/pros_findfirstid.action?cid=<s:property value="cid"/>&page=<s:property value="pagebean.totalPage"/>"> </a>
</s:if>
<s:if test="csid!=null">
<a href="${ pageContext.request.contextPath }/pros_findcsid.action?csid=<s:property value="csid"/>&page=1" class="firstPage"> </a>
<a href="${ pageContext.request.contextPath }/pros_findcsid.action?csid=<s:property value="csid"/>&page=<s:property value="pagebean.page-1"/>" class="previousPage"> </a>
<s:iterator var="i" begin="1" end="pagebean.totalPage">
<a href="${ pageContext.request.contextPath }/pros_findcsid.action?csid=<s:property value="csid"/>&page=<s:property value="#i"/>"><s:property value="#i"/></a>
</s:iterator>
<a class="nextPage" href="${ pageContext.request.contextPath }/pros_findcsid.action?csid=<s:property value="csid"/>&page=<s:property value="pagebean.page+1"/>"> </a>
<a class="lastPage" href="${ pageContext.request.contextPath }/pros_findcsid.action?csid=<s:property value="csid"/>&page=<s:property value="pagebean.totalPage"/>"> </a>
</s:if>