分页的需求:
    select from ssm_goods limit 0,3
    首页
    上一页
    下一页
    尾页
    起始索引位置 = (当前页-1)
    分页单位
    分页单位
    当前页页码值

    技巧:当有多个参数需要传到持久层时,可以创建集合,将参数封装到集合,将集合传过去。

    分页逻辑:

    1. //接收当前页的页码值
    2. if (currentPage == null || currentPage.equals("")){
    3. currentPage = "1";
    4. }else {
    5. System.out.println("当前页的页码值为 "+currentPage);
    6. }
    7. //计算上一页的页码值
    8. Integer prevPage = 0;
    9. if (currentPage.equals("1")){
    10. prevPage = 1;
    11. }else {
    12. prevPage = Integer.parseInt(currentPage) - 1;
    13. }
    14. //计算尾页的值
    15. Integer lastPage = 0;
    16. //获取商品的总记录条数
    17. Integer count = goodsService.getCount();
    18. System.out.println("总记录条数为 "+count);
    19. //指定分页单位
    20. Integer pageSize = 2;
    21. if (count % pageSize == 0){
    22. lastPage = count / pageSize;
    23. }else{
    24. lastPage = (count / pageSize) + 1;
    25. }
    26. //计算下一页的值
    27. Integer nextPage = 0;
    28. if (currentPage.equals(lastPage+"")){
    29. nextPage = lastPage;
    30. }else {
    31. nextPage = Integer.parseInt(currentPage) + 1;
    32. }
    33. //计算起始索引位置
    34. Integer startIndex = (Integer.parseInt(currentPage) - 1) * pageSize;
    35. //将分页所需要的条件传到集合里
    36. hashMap.put("startIndex",startIndex);
    37. hashMap.put("pageSize",pageSize);

    分页工具类:

    1. package com.ssm.shop.utils;
    2. import org.springframework.stereotype.Component;
    3. @Component
    4. public class PageUtils {
    5. //当前页
    6. private String currentPage;
    7. //上一页
    8. private Integer prevPage;
    9. //下一页
    10. private Integer nextPage;
    11. //尾页(也可以表示总页数)
    12. private Integer lastPage;
    13. //总记录条数
    14. private Integer count;
    15. //起始索引位置
    16. private Integer startIndex;
    17. //分页单位
    18. private Integer pageSize;
    19. public String getCurrentPage() {
    20. return currentPage;
    21. }
    22. public void setCurrentPage(String currentPage) {
    23. this.currentPage = currentPage;
    24. }
    25. public Integer getPrevPage() {
    26. return prevPage;
    27. }
    28. public void setPrevPage(Integer prevPage) {
    29. this.prevPage = prevPage;
    30. }
    31. public Integer getNextPage() {
    32. return nextPage;
    33. }
    34. public void setNextPage(Integer nextPage) {
    35. this.nextPage = nextPage;
    36. }
    37. public Integer getLastPage() {
    38. return lastPage;
    39. }
    40. public void setLastPage(Integer lastPage) {
    41. this.lastPage = lastPage;
    42. }
    43. public Integer getCount() {
    44. return count;
    45. }
    46. public void setCount(Integer count) {
    47. this.count = count;
    48. }
    49. public Integer getStartIndex() {
    50. return startIndex;
    51. }
    52. public void setStartIndex(Integer startIndex) {
    53. this.startIndex = startIndex;
    54. }
    55. public Integer getPageSize() {
    56. return pageSize;
    57. }
    58. public void setPageSize(Integer pageSize) {
    59. this.pageSize = pageSize;
    60. }
    61. //计算上一页的值
    62. public void initPrevPage(){
    63. prevPage = currentPage.equals("1") ? 1 : Integer.parseInt(currentPage) - 1;
    64. }
    65. //计算尾页的值
    66. public void initLastPage(){
    67. lastPage = count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1;
    68. }
    69. //计算下一页的值
    70. public void initNextPage(){
    71. nextPage = currentPage.equals(lastPage+"") ? lastPage : Integer.parseInt(currentPage) + 1;
    72. }
    73. //计算起始索引位置
    74. public void initStartIndex(){
    75. startIndex = (Integer.parseInt(currentPage) - 1) * pageSize;
    76. }
    77. //初始化的方法
    78. public void init(String currentPage,Integer pageSize,Integer count){
    79. if (currentPage == null || currentPage.equals("")){
    80. currentPage = "1";
    81. }
    82. this.currentPage = currentPage;
    83. this.pageSize = pageSize;
    84. this.count = count;
    85. }
    86. //调用的方法
    87. public void initPage(String currentPage,Integer pageSize,Integer count){
    88. init(currentPage,pageSize,count);
    89. initStartIndex();
    90. initPrevPage();
    91. initLastPage();
    92. initNextPage();
    93. }
    94. }