ssm实现分页开发

工具类:pageBean.java

package com.lmzg.model;

import java.util.List;

public class PageBean<T> {
    //已知数据
    private int pageNum;   //当前页,从请求那边传过来
    private int pageSize;  //每页显示的数据条数
    private int totalRecord;  //总的数据条数
    //需要计算的来
    private int totalPage;  //总页数,通过totalRecord和pageSize计算可以得来
    //开始索引,也就是我们在数据库中要从第几行数据开始查,有了startIndex和pageSize,
    //就知道了limit语句的两个数据,就能获得每页需要显示的数据了
    
    private int startIndex;
    //将每页要显示的数据放在list集合中
    private List<T> list;
    
    
    //分页显示的页数,比如在页面上显示1,2,3,4,5页,start就为1,end就为5,这个也是算过来的
    private int start;
    private int end;
    
    
    
    //通过pageSize,pageNum,totalRecord计算得来totalPage和startIndex
    //构造方法中将pageNum,pageSize,totalRecord获得
    
    public PageBean() {
        super();
    }

    public PageBean(int pageNum, int pageSize, int totalRecord) {
        super();
        this.pageNum = pageNum;

        this.pageSize = pageSize;
        this.totalRecord = totalRecord;
        //totalPage总页数
        if(totalRecord%pageSize==0) {
            this.totalPage=totalRecord/pageSize;
        }else {
            this.totalPage=totalRecord/pageSize+1;
        }
        //开始索引
        if(pageNum>getTotalPage())
        {
          pageNum=getTotalPage();
          this.pageNum=getTotalPage();
        } 
        if(pageNum<1)
        {
          pageNum=1;
          this.pageNum=1;
        }
        this.startIndex = (pageNum-1)*pageSize;
        this.start = 1;
        this.end = 5;
        
        //显示页数的算法
        if (totalPage<=5) {
            //总页数都小于5,那么end就为总页数的值
            this.end=this.totalPage;
            
        }else {
            //总页数大于5,那么就要根据当前是第几页,来判断start和end为多少了,
            this.start=pageNum-2;
            this.end=pageNum+2;
            
            if (start<=0) {
                
                //比如当前页是第一页或者第二页,那么就不符合这个规则
                this.start=1;
                this.end=5;
            }
            if (end>=this.totalPage) {
                //比如当前页是倒数第二页或者最后一页,也同样不符合上面这个规则
                this.end=totalPage;
                this.start=end-5;
            }
        }
    }

    public int getTotalRecord() {
         if(totalRecord < 0) {
                return 0;
            }
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        
        this.totalRecord = totalRecord;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public List<T> getList() {
        return list;
    }

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

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        if(pageNum>getTotalPage())
        {
          this.pageNum=getTotalPage();
        }
        else {
          this.pageNum = pageNum;
        }  
        if(pageNum<1)
        {
          this.pageNum=1;
        }
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    

}

前端页面:

<!-- 分页导航 -->
    <div id="page">
        共有${pageBean.totalRecord }个员工,共有${pageBean.totalPage }页,当前为第${pageBean.pageNum }页
        <a href="${pageContext.request.contextPath }/selectPage.do?pageNum=1">首页</a>
        <!-- 如果为第一页时,上一页隐藏 -->
        <c:if test="${pageBean.pageNum==1 }">
            <c:forEach begin="${pageBean.start }" end="${pageBean.end }" step="1" var="i">
            <c:if test="${pageBean.pageNum==i }">
            ${i }
            </c:if>
            <c:if test="${pageBean.pageNum!=i }">
                <a href="${pageContext.request.contextPath }/selectPage.do?pageNum=${i}">${i }</a>            
            </c:if>
            
            </c:forEach>
        <a href="${pageContext.request.contextPath }/selectPage.do?pageNum=${pageBean.pageNum+1 }">下一页 </a>
        </c:if>
        
        <c:if test="${pageBean.pageNum>1 && pageBean.pageNum<pageBean.totalPage }">
            <a href="${pageContext.request.contextPath }/selectPage.do?pageNum=${pageBean.pageNum-1 }">上一页 </a>
            <c:forEach begin="${pageBean.start }" end="${pageBean.end }" step="1" var="i">
            <c:if test="${pageBean.pageNum==i }">
            ${i }
            </c:if>
            <c:if test="${pageBean.pageNum!=i }">
                <a href="${pageContext.request.contextPath }/selectPage.do?pageNum=${i}">${i }</a>            
            </c:if>
            
            </c:forEach>
        <a href="${pageContext.request.contextPath }/selectPage.do?pageNum=${pageBean.pageNum+1 }">下一页 </a>
        </c:if>
        <!-- 如果为最后一页时,下一页隐藏 -->
        <c:if test="${ pageBean.pageNum==pageBean.totalPage }">
            <a href="${pageContext.request.contextPath }/selectPage.do?pageNum=${pageBean.pageNum-1 }">上一页 </a>
            <c:forEach begin="${pageBean.start }" end="${pageBean.end }" step="1" var="i">
            <c:if test="${pageBean.pageNum==i }">
            ${i }
            </c:if>
            <c:if test="${pageBean.pageNum!=i }">
                <a href="${pageContext.request.contextPath }/selectPage.do?pageNum=${i}">${i }</a>            
            </c:if>
            </c:forEach>
        </c:if>
        <!-- 尾页 -->
        <a href="${pageContext.request.contextPath }/selectPage.do?pageNum=${pageBean.totalPage}">尾页</a>
    </div>

更加详细的见:https://www.cnblogs.com/whgk/p/6474396.html

模糊查询分页:

一、所有的web香茅油都会用到分页显示和模糊查询,对于有些人不知道该怎么写

二、今天我用springMVC 和 myBatis 写的分页和模糊分享给大家,不喜勿喷

三、数据库是mysql

四、其实写分页就是新建一个分页的类,定义 页码  每页数量  共几页  当前页数  总数量

五、判断多少页,获取总数量除以每页显示的数量,有余数+1页

六、sql语句就是用 limit  显示的数量,把从多少条开始,到显示几条传到sql语句上

七、目前每页的显示的数量是定义好的,等过两天有时间把自己可以选择显示页数的代码写出来

八、源码下载地址  点击打开链接

 
  1. 用户实体类

  2.  
  3. public class User {

  4.  
  5. private int id;

  6. private String name;

  7. private String sex;

  8.  
  9. public int getId() {

  10. return id;

  11. }

  12. public void setId(int id) {

  13. this.id = id;

  14. }

  15. public String getSex() {

  16. return sex;

  17. }

  18. public void setSex(String sex) {

  19. this.sex = sex;

  20. }

  21. public String getName() {

  22. return name;

  23. }

  24. public void setName(String name) {

  25. this.name = name;

  26. }

  27.  
  28. }

  29.  
  30. 分页的实体类

  31.  
  32. public class Page {

  33. /**

  34. * 页码

  35. */

  36. private int pageNum;

  37. /**

  38. * 每页显示的行数

  39. */

  40. private int pageRows;

  41. /**

  42. * 总行数

  43. */

  44. private int totalRows;

  45. /**

  46. * 总页数

  47. */

  48. private int totalPages;

  49. /**

  50. * 起始行号

  51. */

  52. private int beginRownum;

  53. /**

  54. * 结束行号

  55. */

  56. private int endRownum;

  57.  
  58. public Page(int pageNum, int pageRows, int totalRows) {

  59. this.pageNum = pageNum;

  60. this.pageRows = pageRows;

  61. this.totalRows = totalRows;

  62. // 计算总页数:总行数%每页行数==0?总行数/每页行数:总行数/每页行数+1

  63. this.totalPages = totalRows % pageRows == 0 ? totalRows / pageRows : (totalRows / pageRows) + 1;

  64. beginRownum = (pageNum - 1) * pageRows;

  65. endRownum = pageNum * pageRows;

  66. }

  67.  
  68. public int getPageNum() {

  69. return pageNum;

  70. }

  71.  
  72. public void setPageNum(int pageNum) {

  73. this.pageNum = pageNum;

  74. }

  75.  
  76. public int getPageRows() {

  77. return pageRows;

  78. }

  79.  
  80. public void setPageRows(int pageRows) {

  81. this.pageRows = pageRows;

  82. }

  83.  
  84. public int getTotalRows() {

  85. return totalRows;

  86. }

  87.  
  88. public void setTotalRows(int totalRows) {

  89. this.totalRows = totalRows;

  90. }

  91.  
  92. public int getTotalPages() {

  93. return totalPages;

  94. }

  95.  
  96. public void setTotalPages(int totalPages) {

  97. this.totalPages = totalPages;

  98. }

  99.  
  100. public int getBeginRownum() {

  101. return beginRownum;

  102. }

  103.  
  104. public void setBeginRownum(int beginRownum) {

  105. this.beginRownum = beginRownum;

  106. }

  107.  
  108. public int getEndRownum() {

  109. return endRownum;

  110. }

  111.  
  112. public void setEndRownum(int endRownum) {

  113. this.endRownum = endRownum;

  114. }

  115.  
  116. }

 
  1. mapper 和 映射文件

  2.  
  3. public interface UserMapper {

  4.  
  5. public List<User> selectAll(Map<String,Object> map);

  6.  
  7. //查询总行数

  8. public int selTotalRows(Map<String,Object> map);

  9. }

  10.  
  11. <mapper namespace="com.mapper.UserMapper">

  12.  
  13. <select id="selectAll" resultType="com.entity.User">

  14. select * from user

  15. <where>

  16. <if test="name != null and name != ''">

  17. and name like #{name}

  18. </if>

  19. <if test="sex != null and sex != ''">

  20. and sex = #{sex}

  21. </if>

  22. </where>

  23. limit #{page.beginRownum},5

  24. </select>

  25.  
  26. <!-- 查询总行数 -->

  27. <select id="selTotalRows" resultType="int">

  28. select count(id) from user

  29. <where>

  30. <if test="name != null and name != ''">

  31. and name like #{name}

  32. </if>

  33. <if test="sex != null and sex != ''">

  34. and sex = #{sex}

  35. </if>

  36. </where>

  37. </select>

  38.  
  39. </mapper>

 
  1. 控制层

  2.  
  3. public class UserController {

  4.  
  5. @Autowired

  6. private UserService userService;

  7.  
  8. @RequestMapping("/showAll.action")

  9. public String selAll(HttpServletRequest request){

  10. Map<String,Object> map = new HashMap<String, Object>();

  11. map.put("name", null);

  12. map.put("sex", null);

  13. Page page = new Page(1,5,userService.selTotalRows(map));

  14. map.put("page", page);

  15. List<User> userList = userService.selectAll(map);

  16. request.setAttribute("userList", userList);

  17. request.setAttribute("page", page);

  18. return "two.jsp";

  19. }

  20.  
  21.  
  22. //查询所有用户(带模糊查询、分页)

  23. @RequestMapping("/showPage.action")

  24. public String selAllLikePage(int pageNum,String name,String sex,HttpServletRequest request){

  25. Map<String,Object> map = new HashMap<String, Object>();

  26. if("".equals(name)){

  27. map.put("name", null);

  28. }else{

  29. map.put("name", "%"+name+"%");

  30. }

  31. if("".equals(sex)){

  32. map.put("sex", null);

  33. }else{

  34. map.put("sex", sex);

  35. }

  36. Page page = new Page(pageNum,5,userService.selTotalRows(map));

  37. map.put("page", page);

  38. List<User> userList = userService.selectAll(map);

  39. request.setAttribute("userList",userList);

  40. request.setAttribute("page", page);

  41. request.setAttribute("name", name);

  42. request.setAttribute("sex", sex);

  43. return "two.jsp";

  44. }

  45. }

 
  1. jsp页面

  2.  
  3. <body>

  4. <div>

  5. <form action="showPage.action?pageNum=1" method="post">

  6. <span>

  7. 姓名:<input name="name" value="${name }" />   

  8. 性别:<input name="sex" value="${sex }" />  

  9. <input type="submit" value="查询" />

  10. </span>

  11. </form>

  12. </div>

  13. <br><br>

  14. <div>

  15. <table width="500" border="1" cellspacing="0" cellpadding="0">

  16. <tr>

  17. <th width="100px">序号</th>

  18. <th width="200px">姓名</th>

  19. <th width="200px">性别</th>

  20. </tr>

  21. <c:forEach items="${userList }" var="user">

  22. <tr>

  23. <td>${user.id }</td>

  24. <td>${user.name }</td>

  25. <td>${user.sex }</td>

  26. </tr>

  27. </c:forEach>

  28. <tr>

  29. <td class="scott" colspan="20" style="text-align: center;">

  30. <c:if test="${page.pageNum > 1 }">

  31. <a href="showPage.action?name=${name }&sex=${sex }&pageNum=1">首页</a>

  32. <a href="showPage.action?name=${name }&sex=${sex }&pageNum=${page.pageNum - 1 }">上一页</a>

  33. </c:if>

  34. <c:forEach begin="1" end="${page.totalPages}" step="1" var="num">

  35. <c:if test="${page.pageNum == num }">${num }</c:if>

  36. <c:if test="${page.pageNum != num }">

  37. <a href="showPage.action?name=${name }&sex${sex }&pageNum=${num }">${num }</a>

  38. </c:if>

  39. </c:forEach>

  40. <c:if test="${page.pageNum < page.totalPages }">

  41. <a href="showPage.action?name=${name }&sex=${sex }&pageNum=${page.pageNum + 1 }">下一页</a>

  42. <a href="showPage.action?name=${name }&sex=${sex }&pageNum=${page.totalPages }">尾页</a>

  43. </c:if>

  44. 总${page.pageNum}/${page.totalPages }页

  45. </td>

  46. </tr>

  47. </table>

  48.  
  49. </div>

  50. </body>

转载自:https://blog.csdn.net/yp18810420654/article/details/73331573/

猜你喜欢

转载自blog.csdn.net/wzy_king/article/details/81189001
今日推荐