分页

PageBean.java
package entity;

import java.util.List;

/**
 * @author DAJ
 * @version 1.0
 * @ClassName PageBean
 * @date 2019/7/24 16:52
 */
public class PageBean {
    private int totalCount;        //总记录数
    private int totalPage;         //总页数
    private List list;          //每页的数据
    private int currentPage;        //当前页码
    private int rows;               //每页的记录数

    public PageBean(int totalCount, int totalPage, List list, int currentPage, int rows) {
        this.totalCount = totalCount;
        this.totalPage = totalPage;
        this.list = list;
        this.currentPage = currentPage;
        this.rows = rows;
    }

    public PageBean() {

    }

    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 List getList() {
        return list;
    }

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

    public int getCurrentPage() {
        return currentPage;
    }

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

    public int getRows() {
        return rows;
    }



    public void setRows(int rows) {
        this.rows = rows;
    }

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

dao层

public int getTotalCount(Map map) {
        String sql = "select count(*) from web4list where 1=1";
        System.out.println("jt      " + jt);

        //1.定义StringBulider 用来拼接我们的sql语句
        StringBuilder sb = new StringBuilder(sql);
        //2.map的遍历
        Set set = map.keySet();
        //3.准备List集合以存储查询条件
        List list = new ArrayList<>();
        for (String key : set) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = map.get(key)[0];
            if (value != null && !"".equals(value)) {
                sb.append(" and " + key + " like ?");
                list.add("%" + value + "%");
            }
        }
        //StringBuilder转化sql
        sql = sb.toString();
        System.out.println(sql);
        System.out.println(list);
        return jt.queryForObject(sql, Integer.class, list.toArray());
    }


    @Override
    public List getAthList(int start, int rows, Map map) {
        //limit是用来分页
//        String sql = "select * from web limit ?,?";
//        List query = jt.query(sql, new BeanPropertyRowMapper(Person.class), start, rows);
        String sql = "select * from web4list where 1=1";
        StringBuilder sb = new StringBuilder(sql);
        Set set = map.keySet();
        List list = new ArrayList<>();

        for (String key : set) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = map.get(key)[0];
            if (value != null && !"".equals(value)) {
                sb.append(" and " + key + " like ?");
                list.add("%" + value + "%");
            }
        }
        sb.append(" limit ? , ?");
        list.add(start);
        list.add(rows);
        sql = sb.toString();


        return jt.query(sql, new BeanPropertyRowMapper(User.class),list.toArray());
    }
 
  

serive

  public PageBean getPageBean(String _currentPage, String _rows, Map map) {
        //创建一个新的pageBean对象
        PageBean pageBean = new PageBean<>();
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        pageBean.setCurrentPage(currentPage);
        pageBean.setRows(rows);

        //获得总记录数
        int totalCount = dao.getTotalCount(map);
        pageBean.setTotalCount(totalCount);

        //获得总页数
        int totalPage = totalCount % rows == 0 ? totalCount / rows : (totalCount / rows) + 1;
        pageBean.setTotalPage(totalPage);

        //传入 limit?,?;第一个?全部页码*页面大小
        int start = (currentPage - 1) * rows;
        if (currentPage > totalPage) {
            currentPage = totalPage;
            if (totalPage == 0) {
                totalPage = 1;
            } else {
                start = (totalPage - 1) * rows;
            }
        }
        List list = dao.getAthList(start, rows, map);
        pageBean.setList(list);

        return pageBean;
    }

servlet

package servlet;

import entity.PageBean;
import entity.User;
import service.IPersonService;
import service.impl.IPersonServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        IPersonService is = new IPersonServiceimpl();
        //查询全部信息
//        List list = is.selectALL();
//        request.setAttribute("list", list);
//        request.getRequestDispatcher("member-list.jsp").forward(request,response);

        //分页查询
        //获取当前的页数跟行数
        String currenrPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");
        //判断页数为空时,显示第一页
        if (currenrPage == null || "".equals(currenrPage)) {
            currenrPage = "1";
        } else if ("0".equals(currenrPage)) {
            currenrPage = "1";
        }
        //设置一页显示的行数
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }

        Map map = request.getParameterMap();

        IPersonService serivce = new IPersonServiceimpl();
        PageBean pageBean = serivce.getPageBean(currenrPage, rows, map);
        //转发到前台界面进行展示
        request.setAttribute("pb", pageBean);
        request.setAttribute("map", map);
        request.getRequestDispatcher("member-list.jsp").forward(request, response);


    }
}

member-list.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>



    
    欢迎页面
    
    
    
    
    
    
    
    



<%--
--%> <%----%> <%--
--%> <%--
--%> <%----%> <%--
--%>
编号 用户名 密码 性别 年龄 地址 状态 操作
${s.count} ${user.name} ${user.password} ${user.gender} ${user.age} ${user.address} 已启用 已拉黑 <%----%> <%----%> <%----%> <%----%>
<%--
<%––%> <%––%> <%–<%–1–%>–%> <%–<%–2–%>–%> <%–<%–3–%>–%> <%–<%–489–%>–%> <%––%> <%––%> < a href=" ">${i} < a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&gender=${condition.gender[0]}&age=${condition.age[0]}&address=${condition.address[0]}">${i}
--%>

分页_第1张图片

你可能感兴趣的:(分页)