面向对象的方式来实现分页的功能——分页类PageBean

面向对象的方式来实现分页的功能——分页类PageBean

1.面向对象的方式来实现分页的功能——分页类PageBean,先实现一个工具类

代码如下:

package cn.itcast.shop.utils;

import java.util.List;

/*
 * 分页类的封装
 */
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;
    }

}

2.action中使用PageBean

//根据分类的id查询的方法
        public String findByCid(){
            //List<Category> cList=categoryService.findAll();
            PageBean<Product> pageBean= productService.findByPageCid(cid,page);//根据一级分类查询商品,带分页的查询
            return "findByCid";
        }

3.Service业务逻辑层中的代码:

//根据一级分类的cid带有分页的查询商品
        public PageBean<Product> findByPageCid(Integer cid, int page) {
            PageBean<Product> pageBean=new PageBean<Product>();
            //设置当前页数
            pageBean.setPage(page);
            //设置每页显示的记录数
            int limit=8;
            pageBean.setLimit(limit);
            //设置总的记录数
            int totalCount=0;
            totalCount=productDao.findCountCid(cid);
            pageBean.setTotalCount(totalCount);
            //设置总页数
            int totalPage=0;
            if(totalCount%limit==0){
                totalPage=totalCount/limit;
            }else{
                totalPage=totalCount/limit+1;
            }
            pageBean.setTotalPage(totalPage);
            //每页显示数据的集合
            //从哪开始
            int begin=(page-1)*limit;

            List<Product> list=productDao.findByPageCid(cid,begin,limit);
            pageBean.setList(list);
            return pageBean;
        }

4.持久层代码

//根据分类id查询商品个数
        public int findCountCid(Integer cid) {
            String hql="select count(*) from Product p where p.categorySecond.category.cid=?";
            List<Long> list=this.getHibernateTemplate().find(hql,cid);
            if(list!=null&&list.size()>0){
                return list.get(0).intValue();
            }
            return 0;
        }
        //根据分类id查询商品集合
        public List<Product> findByPageCid(Integer cid, int begin, int limit) {
            String hql="select p from Product p join p.categorySecond cs join cs.category where c.cid=?";
            //分页的另一种写法
            return null;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值