首页 > 其他分享 >分页查询功能

分页查询功能

时间:2022-08-17 14:57:09浏览次数:54  
标签:功能 rows 分页 int list 查询 totalCount currentPage public

分页查询功能分析

 

 

 

 

 

分析具体步骤

PageBean实体类:

/**
 * 分页对象
 */
public class PageBean<T> {
    private int totalCount; //总记录数
    private int totalPage;  //总页码
    private List<T> list;   //每页的数据
    private int currentPage;    //当前页码
    private int rows;   //每页显示的记录数

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

    public PageBean() {
    }

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

    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<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 getRows() {
        return rows;
    }

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

 

 

 

 

 

后台代码实现

UserDao接口:

    /**
     * 查询总记录数
     * @return
     */
    int FindTotalCount();

    /**
     * 分页查询每页记录
     * @param start
     * @param rows
     * @return
     */
    List<User> fingByPage(int start, int rows);

UserDaoImpl实现类:

    @Override
    public int FindTotalCount() {
        String sql = "select count(*) from user";

        return template.queryForObject(sql,Integer.class);
    }

    @Override
    public List<User> fingByPage(int start, int rows) {
        String sql = "select * from user limit ?,?";
        return template.query(sql,new BeanPropertyRowMapper<User>(User.class),start,rows);
    }

UserService接口:

    /**
     * 分页查询
     * @param currentPage
     * @param rows
     * @return
     */
    PageBean<User> findUserByPage(String currentPage, String rows);

UserServiceImpl实现类:

@Override
    public PageBean<User> findUserByPage(String _currentPage, String _rows) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        //判读最小页码
        if (currentPage < 1){
            currentPage = 1;
        }

        //1、创建空的PageBean对象
        PageBean<User> pb = new PageBean<>();

        //3、调用dao查询总记录数
        int totalCount = dao.FindTotalCount();
        pb.setTotalCount(totalCount);

        //5、计算总页码
        int totalPage = (totalCount%rows)==0 ? totalCount/rows : (totalCount/rows)+1;

        //判断最大页码
        if (currentPage >totalPage){
            currentPage = totalPage;
        }

        //2、设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //4、调用dao查询list集合
        //计算开始的记录索引
        int start = (currentPage-1)*rows;
        List<User> list = dao.fingByPage(start,rows);
        pb.setList(list);


        pb.setTotalPage(totalPage);

        return pb;
    }

FindUserByPageServlet类:

@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取参数
        String currentPage = request.getParameter("currentPage");//当前页码
        String rows = request.getParameter("rows");//每页显示条数

        if (currentPage == null || "".equals(currentPage)){
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)){
            rows = "5";
        }

        //2、调用servic查询
        UserService service = new UserServiceImpl();
        PageBean<User> pb = service.findUserByPage(currentPage,rows);
        //3、将PageBean存入request
        request.setAttribute("pb",pb);
        //4、转发到list.jsp
        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

 

 

 

 

 

 

前台代码实现

list.jsp:

    <div>
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <c:if test="${pb.currentPage == 1}">
                    <li class="disabled">
                </c:if>
                <c:if test="${pb.currentPage != 1}">
                    <li>
                </c:if>

                    <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage-1}&rows=5" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>

                <c:forEach begin="1" end="${pb.totalPage}" var="i">
                    <c:if test="${pb.currentPage == i}">
                        <li class="active"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5">${i}</a></li>
                    </c:if>

                    <c:if test="${pb.currentPage != i}">
                        <li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5">${i}</a></li>
                    </c:if>
                </c:forEach>
                <li>
                    <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage+1}&rows=5" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
                <span style="font-size: 25px;margin-left: 5px;">
                    共${pb.totalCount}条记录,共${pb.totalPage}页
                </span>

            </ul>
        </nav>


    </div>

index.jsp:

  <a
          href="${pageContext.request.contextPath}/findUserByPageServlet" style="text-decoration:none;font-size:33px">查询所有用户信息
  </a>

 

标签:功能,rows,分页,int,list,查询,totalCount,currentPage,public
From: https://www.cnblogs.com/xjw12345/p/16594535.html

相关文章