PageBean封装

大量数据进行分页展示PageBean的封装

package com.feilong.shop.entity;

import java.io.Serializable;
import java.util.List;

/**
 * @author FeiLong
 * @version 1.8
 * @date 2020/9/15 11:48
 */
@SuppressWarnings({"ALL", "AlibabaAvoidCommentBehindStatement"})
public class PageBean<T> implements Serializable {
    private List<T> list;         //要封装的list泛型集合
    private int currentPage;     //当前页码
    private int pageSize;        //每页展示的条数
    private long totalCount;     //数据总数
    private int totalPage;      //总页数

    public PageBean() {
    }

    public PageBean(List<T> list, int currentPage, int pageSize, long totalCount) {
        this.list = list;
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public int getTotalPage() {
        return (int) Math.ceil(totalCount * 1.0 / pageSize);//设置总页数
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    @Override
    public String toString() {
        return "PageBean{" +
                "list=" + list +
                ", currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", totalCount=" + totalCount +
                ", totalPage=" + totalPage +
                '}';
    }
}

Dao层代码

 public List<Product> selectProductByPage(String tid, int page, int pageSize) {
        String sql = "select p_id pid,t_id tid,p_name pname,p_time ptime,p_image pimage," +
                "p_price pprice,p_state pstate,p_info pinfo from product where t_id=? limit ?,?";
        List<Product> products = null;
        try {
            products = queryRunner.query(sql, new BeanListHandler<Product>(Product.class), tid, (page - 1) * pageSize, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return products;
    }

Service层代码

 public PageBean<Product> findAll(String tid, int page, int pageSize) {
        long count = productDao.selectCount(tid);//查询总条数
        List<Product> list = productDao.selectProductByPage(tid, page, pageSize);//根据tid查询当前页的数据
        return new PageBean<Product>(list, page, pageSize, count);
    }

Servlet代码

 public String show(HttpServletRequest request, HttpServletResponse response) {
        String tid = request.getParameter("tid");
        String currentPage = request.getParameter("currentPage");
        int pageSize = 8;//定义每页展示8条数据
        int page = 1;//没拿到数据,从第一页展示
        if (currentPage != null) {
            page = Integer.parseInt(currentPage);
        }
        //调用业务逻辑层需要展示的PageBean对象
        PageBean<Product> pageBean = productService.findAll(tid, page, pageSize);
        System.out.println(pageBean);
        //存储到session中数据转发jsp页面进行展示
        request.setAttribute("pageBean", pageBean);
        return Constants.FORWARD + "/goodsList.jsp";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值