大量数据进行分页展示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";
}