ssh中的分页查询商品

1.封装一个PageBean类

public class PageBean<T> {
    private Integer page;// 当前页数.
    private Integer limit;// 每页显示记录数
    private Integer totalCount;// 总记录数
    private Integer totalPage;// 总页数.
    private List<T> list; // 显示到浏览器的数据.
    public Integer getPage() {
        return page;
    }
    public void setPage(Integer page) {
        this.page = page;
    }
    public Integer getLimit() {
        return limit;
    }
    public void setLimit(Integer limit) {
        this.limit = limit;
    }
    public Integer getTotalCount() {
        return totalCount;
    }
    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }
    public Integer getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }
    public List<T> getList() {
        return list;
    }
    public void setList(List<T> list) {
        this.list = list;
    }

}

2.点击按钮传递参数,页数page

3.action中接受page参数,通过调用service中的方法去查询,返回PageBean对象

4.在serviceImpl中实现分页的操作,数据封装到PageBean中

int limit = 12 ;//每页显示记录数
        int totalPage = 0; //总页数

        //创建PageBean
        PageBean<Product> pageBean = new PageBean<Product>();

        //封装页数和每页显示的记录数
        pageBean.setPage(page);
        pageBean.setLimit(limit);

        //查询总记录数然后封装
        Integer totalCount = productDao.findTotalCount(cid);
        pageBean.setTotalCount(totalCount);

        //总页数的封装
        if(totalCount%limit==0){
            totalPage = totalCount/limit ;
        }else{
            totalPage = totalCount/limit +1 ;
        }
        pageBean.setTotalPage(totalPage);

        //商品数据集合和封装
        int begin = (page-1)*limit;
        List<Product> list = productDao.findProductByCidAndPage(cid,begin,limit);
        pageBean.setList(list);

5.在dao层的操作

//查询某个分类下的总记录数
    public Integer findTotalCount(Integer cid) {
        String hql = "select count(*) from Product p , CategorySecond cs where p.categorySecond = cs and cs.category.cid = ?";
        //String hql = "select count(*) from Product p join p.categorySecond cs join cs.category c where c.cid = ?";
        List<Long> list = this.getHibernateTemplate().find(hql,cid);
        //System.out.println("list:============="+list.get(0).intValue());
        return list.get(0).intValue();
    }

    //查询商品集合
    @Override
    public List<Product> findProductByCidAndPage(Integer cid, Integer begin,
            int limit) {
        String hql = "select p from Product p , CategorySecond cs where p.categorySecond = cs and cs.category.cid = ?";
        List<Product> list = this.getHibernateTemplate().executeFind(new PageHibernateCallback<Product>(hql, new Object[]{cid}, begin, limit));
        return list;
    }

6.在jsp中进行操作显示商品情况

<div id="result" class="result table clearfix">
                        <ul>
                                <s:iterator var="p" value="pageBean.list">
                                    <li>
                                        <a href="./京华亿家分页面.htm">
                                            <img src="${pageContext.request.contextPath}/<s:property value="image"/>" width="170" height="170"  style="display: inline-block;">

                                            <span style='color:green'>
                                                <s:property value="#p.pname"/>
                                            </span>

                                            <span class="price">
                                                亿家价: ¥<s:property value="#p.market_price"/>
                                            </span>

                                        </a>
                                    </li>
                                </s:iterator>   
                        </ul>
                </div>

7.进行分页操作查询商品

<div class="pagination"><s:property value="pageBean.page"/>/<s:property value="pageBean.totalPage"/><s:if test="pageBean.page != 1">
                <a href="${ pageContext.request.contextPath }/product/product_findByCid.action?cid=<s:property value="cid"/>&page=1" class="firstPage">&nbsp;</a>      
                <a href="${ pageContext.request.contextPath }/product/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.page-1"/>" class="previousPage">&nbsp;</a>   
            </s:if> 
            <s:iterator var="i" begin="1" end="pageBean.totalPage" step="1">
                <s:if test="pageBean.page==#i">
                    <span class="currentPage"><s:property value="#i"/></span>
                </s:if>
                <s:else>
                    <a href="${ pageContext.request.contextPath }/product/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="#i"/>"><s:property value="#i"/></a>
                </s:else>
            </s:iterator>

            <s:if test="pageBean.page != pageBean.totalPage">
                <a class="nextPage" href="${ pageContext.request.contextPath }/product/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.page+1"/>">&nbsp;</a>
                <a class="lastPage" href="${ pageContext.request.contextPath }/product/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.totalPage"/>">&nbsp;</a>
            </s:if> 
    </div>

这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值