java底层实现分页

查询分页显示的框架层出不穷,自己也实现了一组,因为比较笨,还没有学到框架,而是用mvc实现。自己按照百度的分页,也实现了,就是有点丑。

需求:

【首页】【上一页】【1】【2】【3】【下一页】【尾页】

分页功能分析:

  1.物理分页--每次点击查询

    优点:查询快

    缺点:和数据库交互太频繁

  2.逻辑分页--一次查询所有

    优点:和数据库交互少

    缺点:自行脑补。

数据库:Mysql

  查询语句:select * from product order by pdate limit  begin(这里填写从第几个查询),end(查询几个);

逻辑分析

1.进入商品展示页就应该显示分页信息

2.进入商品页之前应该通过Servlet

3.传数据到前台,前台使用el+jstl

4.前台应传当前页到Servlet

详细分析:

currPage  (当前页)   begin(从多少查询)  pSize(查询多少)

  1          0          10

  2          10          10

  3          20          10

  4          30          10

  ...          ...          ... 

所以进入这个页面时就应该传入当前页并跳转到Servlet

<body>
  <h1>欢迎来到商品信息管理平台</h1>
  <h3>
    <a href="${ pageContext.request.contextPath }/ProductFindAllServlet">查询所有商品</a>|
    <a href="${ pageContext.request.contextPath }/ProductFindByPage?currPage=1">分页查询商品</a>
  </h3>
</body>
 
   
  //后台向前台传递的数据封装成对象
   package com.itheima.domain; import java.util.List;
public class PageBean { private Integer cPage; // 当前页数 private Integer tPage; // 总页数 private Integer tCount; // 总商品数 private Integer pSize; // 分页大小 private List<Product> plist; // 传输的对象 public PageBean(Integer cPage, Integer tPage, Integer tCount, Integer pSize, List<Product> plist) { this.cPage = cPage; this.tPage = tPage; this.tCount = tCount; this.pSize = pSize; this.plist = plist; } public PageBean() { } public Integer getcPage() { return cPage; } public void setcPage(Integer cPage) { this.cPage = cPage; } public Integer gettPage() { return tPage; } public void settPage(Integer tPage) { this.tPage = tPage; } public Integer gettCount() { return tCount; } public void settCount(Integer tCount) { this.tCount = tCount; } public Integer getpSize() { return pSize; } public void setpSize(Integer pSize) { this.pSize = pSize; } public List<Product> getPlist() { return plist; } public void setPlist(List<Product> plist) { this.plist = plist; } }

ProductFindByPage中的Servlet
//首先获取参数转换成int
int currPage = Integer.parseInt(request.getParameter("currPage"));
//调用Service层

ProductService ps = new ProductService();
PageBean pageBean = ps.SelectByPage(currPage);

//将对象放入到request域中
request.setAttribute("pageBean",pageBean);

//转发到列表页
request.getRequestDispatcher("/product_page.jsp").forward(request.response);

ProductService中的代码

public PageBean SelectByPage(String currPage){ 
  //封装页面对象
  PageBean pageBean = new PageBean();
  //设置属性
  
  
   private Integer cPage; // 当前页数
    private Integer tPage; // 总页数
    private Integer tCount; // 总商品数
    private Integer pSize; // 分页大小
    private List<Product> plist; // 传输的对象

  pageBean.setCurrPage(currPage);
  //设置总商品数
  ProductDao productdao = new ProductDao();
  int tCount = productdao.findProductCount();
  
  pageBean.setTCount(tCount);

  //总页数=总商品数/分页大小 --向上取整
  //设置总页数
  double count = (double) tCount;
  
  Double tPage = Math.ceil(tCount/Psize);
  pageBean.setTPage(tPage);
  
  productdao.selectProduct(currPage,pSize);

  return pageBean;
  
}

猜你喜欢

转载自www.cnblogs.com/Hymen-/p/9085186.html