分页查询

前台代码:

<a href="${ pageContext.request.contextPath }/yourAction_findByPage.action?page=1">分页查询</a>

工具类

//用来存储分页查询返回的记录集以及页数信息
public class PageBean<T> {
    private int page;//当前页数
    private int totalCount;//总记录书
    private int totalPage;//总页数
    private int limit;//每页显示的记录数
    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 getLimit() {
        return limit;
    }
    public void setLimit(int limit) {
        this.limit = limit;
    }
    public List<T> getList() {
        return list;
    }
    public void setList(List<T> list) {
        this.list = list;
    }

}

action代码

//接收页数
private Integer page;
public void setPage(Integer page) {
        this.page = page;
}
public String findByPage(){
    PageBean<T>pageBean=yourService.findByPage(page);
        ActionContext.getContext().getValueStack().set("pageBean", pageBean);
        return "findByPage";
}

逻辑层代码

public PageBean<T> findByPage(Integer page) {
        PageBean<T> pageBean = new PageBean<T>();
        //设置当前页数
        pageBean.setPage(page);
        //设置每页显示的条数
        int limit=10;
        pageBean.setLimit(limit);
        //查询总的记录数
        int totalCount=orderDao.findCount();
        int totalPage = 0;
        //设置总页数
        if (totalCount% limit == 0) {
            totalPage=totalCount / limit ;
        }else {
            totalPage = totalCount/limit +1;
        }
        pageBean.setTotalCount(totalCount);
        pageBean.setTotalPage(totalPage);
        //定义开始的条数
        int begin =0;
        begin = (page-1)*limit;
        List<T>list=orderDao.findByPage(begin,limit);
        pageBean.setList(list);
        return pageBean;
    }

持久层代码

public int findCount() {
        String hql = "select count(*) from T";
        org.hibernate.Query query = this.getSession().createQuery(hql); 
        int count = ((Long) query.iterate().next()).intValue(); 
        return count;
    }
public List<T> findByPage(int begin, int limit) {
        String hql = "from T order by datetime desc";
        List<T> list=this.getHibernateTemplate().execute(new PageHibernateCallback<T>(hql, null, begin, limit));
        if (list != null && list.size()>0) {
            return list;
        }
        return null;
    }

spring配置

    <bean id="yourAction" class="myTest.YourAction" scope="prototype">
        <property name="yourService" ref="yourService"></property>
    </bean>

struts配置

<action name="your_*" class="yourAction" method="{1}">
            <result name="findByPage">/webInfo/list.jsp</result>

</action>

list.jsp代码

<tr align="center">
                        <td colspan="7"><s:property value="pageBean.page"/>/<s:property value="pageBean.totalPage"/><s:if test="pageBean.page != 1">
                                <a href="${ pageContext.request.contextPath }/your_findByPage.action?page=1">首页</a>|
                                <a href="${ pageContext.request.contextPath }/your_findByPagel.action?page=<s:property value="pageBean.page-1"/>">上一页</a>|
                            </s:if>
                            <s:if test="pageBean.page != pageBean.totalPage">
                                <a href="${ pageContext.request.contextPath }/your_findByPage.action?page=<s:property value="pageBean.page+1"/>">下一页</a>|
                                <a href="${ pageContext.request.contextPath }/your_findByPage.action?page=<s:property value="pageBean.totalPage"/>">尾页</a>
                            </s:if>
                        </td>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值