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>

时间: 2024-08-03 11:59:16

ssh框架中的分页查询的相关文章

SSH框架的多表查询和增删查改 (方法一)上

原创作品,允许转载,转载时请务必标明作者信息和声明本文章==>  http://www.cnblogs.com/zhu520/p/7772823.html   因为最近在做Android 练习的项目,使用增删查改的时候还是使用jdbc的增删查改 发现实在是太麻烦了,所有果断抛弃它,使用ssh, 但是发现不会....经过了四天的时间我终于弄懂了. 哪个大神看到有问题指点一下. 在弄这前要先明白一下@Component @Controller @Service @Repository 这些注释 可以

spring在SSH框架中的作用

从网上了搜了下sring 在ssh中起的作用,在百度知道上看到下面的回答,觉得简单移动,记录下来备查,原文地址: http://zhidao.baidu.com/link?url=JiONrax-Flkpi5hqs_g1HQOrMm1Dk8U1WT88l5T0Gg01bm3M8fMcjUCpqastVxQTd63gxUl5NLyHUQPdq6E-OK 在SSH框假中spring充当了管理容器的角色.我们都知道Hibernate用来做持久层,因为它将JDBC做了一个良好的封装,程序员在与数据库进行

在SSH框架中,如何得到POST请求的URL和参数列表

在做项目的API通知接口的时候,发现在SSH框架中无法获取到对方服务器发来的异步通知信息.最后排查到的原因可能是struts2对HttpServletRequest进行了二次处理,那么该如何拿到post的请求数据呢,方法很多,下面只给出一种方式. 在servlet中GET请求可以通过HttpServletRequest的getRequestURL方法和getQueryString()得到完 整的请求路径和请求所有参数列表, POST的需要getParameterMap()方法遍历得到,不论GET

ssh框架中.xml文件小技巧分离xml

struts.xml文件 struts.xml文件里的action可以分离出来,如: <!-- 预警信息监测 --> <include file="config/struts/warningInformAtion-struts.xml"></include> 注: include是放在</struts>标签的前面 在src下面新建 package 名为:config.struts,再新建xml文件为warningInformAtion-s

mongo中的分页查询

/** * @param $uid * @param $app_id * @param $start_time * @param $end_time * @param $start_page * @param $limit_page * mongodb中的分页查询 */ public static function getUserRevenueInfoList($uid, $source,$app_id, $start_time, $end_time,$skip, $limit){ $match

在SSH框架中使用Spring的好处(转)

以下是我总结下今天笔试中SSh中的总结: 在SSH框架中spring充当了管理容器的角色.我们都知道Hibernate用来做持久层,因为它将JDBC做了一个良好的封装,程序员在与数据库进行交互时可以不用书写大量的SQL语句.Struts是用来做应用层的,他它负责调用业务逻辑serivce层.所以SSH框架的流程大致是:Jsp页面----Struts------Service(业务逻辑处理类)---Hibernate(左到右)    struts负责控制Service(业务逻辑处理类),从而控制了

SSH框架中hibernate 出现 user is not mapped 问题

SSH框架中hibernate 出现 user is not mapped 问题 在做SSH框架整合时,在进行DAO操作时.这里就只调用了chekUser()方法.运行时报  user is not mapped 错误 :  chekUser()方法如下:  public boolean chekUser(User user){           String hql="from user u where u.id=? and u.name=?";        Query quer

SQL Server中的分页查询 select top

SQL Server中的分页查询 https://blog.csdn.net/tswc_byy/article/details/82053091 零.码仙励志 比我差的人还没放弃,比我好的人仍在努力,我就更没资格说我无能为力 一.建库和建表 create database scort use scort create table emp ( empno int primary key, ename nvarchar(10), sal int, deptno int ) insert into e

SSH——基于datagrid实现分页查询

1. 修改页面中datagrid的URL地址,访问action // 取派员信息表格 $('#grid').datagrid( { iconCls : 'icon-forward', fit : true, border : false, rownumbers : true,//显示行号 striped : true, pageList: [3,5,10], pagination : true, toolbar : toolbar,//工具栏 url : "${pageContext.reque