传统分页(mysql和redis)

时间:2019-09-17
本文章向大家介绍传统分页(mysql和redis),主要包括传统分页(mysql和redis)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

分页

Mysql

前端

<%--分页--%>
<div id="page" class="page_div">aaaa</div>
<script src="/testServlet_war/js/jquery.min.js"></script>
<script type="text/javascript" src="/testServlet_war/js/paging.js"></script>
<script>
    //分页
    $("#page").paging({
        pageNo:${pageBean.currentPage},
        totalPage: ${pageBean.totolPage},
        totalSize: ${pageBean.totolCount},
        callback: function(num) {
            $(window).attr('location',"/testServlet_war/UserListServlet?currentPage="+num)
        }
    });
</script>

后端

pojo:PageBean(get和set以及toString方法省略)

public class PageBean {
    //当前是哪一页
    private Integer currentPage;
    //共多少页
    private Integer totolPage;
    //共多少记录
    private Integer totolCount;
    //当前页用户
    private List<T> list = new ArrayList<>();

}

Controller层

转发servlet(获取数据转发到index)
//获取页码
String currentPage = req.getParameter("currentPage");
//把页码给业务层,根据页码给我一个pagebean
PageBean pageBean=userService.getPageBean(Integer.parseInt(currentPage.trim()));
//把pageBean写到request里面
req.setAttribute("pageBean",pageBean);
//转发到分页页面
req.getRequestDispatcher("/index.jsp").forward(req,resp);
请求servlet(登录servlet)
//重定向到转发servlet
resp.sendRedirect("/项目名/转发servlet?currentPage=1");

Service

public PageBean getPageBean(Integer currentPage) throws SQLException {
    PageBean<User> pageBean = new PageBean<User>();
    
    //设置当前页
    pageBean.setCurrentPage(currentPage);
    
    //获取总记录数
    //从数据库里面查询
    Long count = userDao.getCount();
    //设置总记录数
    pageBean.setTotolCount(count.intValue());

    //一页展示多少数据
    Integer pageCount = 5;
    //总页数
    double totolPage = Math.ceil(1.0 * count / pageCount);
    pageBean.setTotolPage((int)totolPage);

    //当前页的角标,根据limit(index,pageCount)查当前页数据
    Integer index = (currentPage-1)*pageCount;
    List<User> pageData =  userDao.getPageData(index,pageCount);
    //设置当前页数据
    pageBean.setUserslist(pageData);

    return pageBean;
}

Dao层

public Long getCount() throws SQLException {
    String sql = "select count(*) from user";
    Long count = (Long) qr.query(sql, new ScalarHandler<>());
    return count;
}



public List<User> getPageData(Integer index, Integer pageCount) throws SQLException {
    String sql = "select * from user limit ?,?";
    List<User> pageUser = qr.query(sql, new BeanListHandler<User>(User.class), index, pageCount);
    return pageUser;
}

redis

    //分页
    //全部学生数据
    List<Student> students = new ArrayList<>();
    
    PageBean<User> pageBean = new PageBean<User>();
    //获取页码
    String currentPage = req.getParameter("currentPage");
    
    //设置当前页
    pageBean.setCurrentPage(Integer.parseInt(currentPage.trim()));
    
    //设置总记录数
    int size = students.size();
    pageBean.setTotolCount(size);
    
    //设置总页数
    //一页展示多少条数据
    Integer pageCount=10;
    double totolPage = Math.ceil(1.0 * size / pageCount);
    pageBean.setTotolPage((int)totolPage);
    
    //设置当前页数据
    //每页的起始索引
    int pageNo=(Integer.parseInt(currentPage)-1)*pageCount;
    //每页的数据
    List<Student> currentStudents=new ArrayList<>();
    if (pageNo+pageCount > size) {
         currentStudents = students.subList(pageNo,size );
    }else {
        currentStudents=students.subList(pageNo,pageNo+pageCount);
    }
    pageBean.setStudentList(currentStudents);
    //把pageBean写到request里面
    req.setAttribute("pageBean",pageBean);
    //转发到分页页面
    req.getRequestDispatcher("/index.jsp").forward(req,resp);

$flag 上一页 下一页