ssh框架中的分页查询

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">&nbsp;</a>
<a href="${ pageContext.request.contextPath }/pros_findfirstid.action?cid=<s:property value="cid"/>&page=<s:property value="pagebean.page-1"/>" class="previousPage">&nbsp;</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"/>">&nbsp;</a>
<a class="lastPage" href="${ pageContext.request.contextPath }/pros_findfirstid.action?cid=<s:property value="cid"/>&page=<s:property value="pagebean.totalPage"/>">&nbsp;</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">&nbsp;</a>
<a href="${ pageContext.request.contextPath }/pros_findcsid.action?csid=<s:property value="csid"/>&page=<s:property value="pagebean.page-1"/>" class="previousPage">&nbsp;</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"/>">&nbsp;</a>
<a class="lastPage" href="${ pageContext.request.contextPath }/pros_findcsid.action?csid=<s:property value="csid"/>&page=<s:property value="pagebean.totalPage"/>">&nbsp;</a>


</s:if>

 

转载于:https://www.cnblogs.com/zhangzhiqin/p/8017166.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值