PageUtils.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.ruoyi.common.utils;
  2. import cn.hutool.http.HttpStatus;
  3. import com.baomidou.mybatisplus.core.metadata.OrderItem;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.ruoyi.common.core.page.PagePlus;
  6. import com.ruoyi.common.core.page.TableDataInfo;
  7. import com.ruoyi.common.utils.sql.SqlUtil;
  8. import java.util.List;
  9. /**
  10. * 分页工具
  11. *
  12. * @author Lion Li
  13. */
  14. public class PageUtils {
  15. /**
  16. * 当前记录起始索引
  17. */
  18. public static final String PAGE_NUM = "pageNum";
  19. /**
  20. * 每页显示记录数
  21. */
  22. public static final String PAGE_SIZE = "pageSize";
  23. /**
  24. * 排序列
  25. */
  26. public static final String ORDER_BY_COLUMN = "orderByColumn";
  27. /**
  28. * 排序的方向 "desc" 或者 "asc".
  29. */
  30. public static final String IS_ASC = "isAsc";
  31. /**
  32. * 当前记录起始索引 默认值
  33. */
  34. public static final int DEFAULT_PAGE_NUM = 1;
  35. /**
  36. * 每页显示记录数 默认值 默认查全部
  37. */
  38. public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;
  39. /**
  40. * 构建 plus 分页对象
  41. *
  42. * @param <T> domain 实体
  43. * @param <K> vo 实体
  44. * @return 分页对象
  45. */
  46. public static <T, K> PagePlus<T, K> buildPagePlus() {
  47. Integer pageNum = ServletUtils.getParameterToInt(PAGE_NUM, DEFAULT_PAGE_NUM);
  48. Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE, DEFAULT_PAGE_SIZE);
  49. String orderByColumn = ServletUtils.getParameter(ORDER_BY_COLUMN);
  50. String isAsc = ServletUtils.getParameter(IS_ASC);
  51. if (pageNum <= 0) {
  52. pageNum = DEFAULT_PAGE_NUM;
  53. }
  54. PagePlus<T, K> page = new PagePlus<>(pageNum, pageSize);
  55. OrderItem orderItem = buildOrderItem(orderByColumn, isAsc);
  56. page.addOrder(orderItem);
  57. return page;
  58. }
  59. public static <T> Page<T> buildPage() {
  60. return buildPage(null, null);
  61. }
  62. /**
  63. * 构建 MP 普通分页对象
  64. *
  65. * @param <T> domain 实体
  66. * @return 分页对象
  67. */
  68. public static <T> Page<T> buildPage(String defaultOrderByColumn, String defaultIsAsc) {
  69. Integer pageNum = ServletUtils.getParameterToInt(PAGE_NUM, DEFAULT_PAGE_NUM);
  70. Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE, DEFAULT_PAGE_SIZE);
  71. String orderByColumn = ServletUtils.getParameter(ORDER_BY_COLUMN, defaultOrderByColumn);
  72. String isAsc = ServletUtils.getParameter(IS_ASC, defaultIsAsc);
  73. if (pageNum <= 0) {
  74. pageNum = DEFAULT_PAGE_NUM;
  75. }
  76. Page<T> page = new Page<>(pageNum, pageSize);
  77. OrderItem orderItem = buildOrderItem(orderByColumn, isAsc);
  78. page.addOrder(orderItem);
  79. return page;
  80. }
  81. private static OrderItem buildOrderItem(String orderByColumn, String isAsc) {
  82. // 兼容前端排序类型
  83. if ("ascending".equals(isAsc)) {
  84. isAsc = "asc";
  85. } else if ("descending".equals(isAsc)) {
  86. isAsc = "desc";
  87. }
  88. if (StringUtils.isNotBlank(orderByColumn)) {
  89. String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
  90. orderBy = StringUtils.toUnderScoreCase(orderBy);
  91. if ("asc".equals(isAsc)) {
  92. return OrderItem.asc(orderBy);
  93. } else if ("desc".equals(isAsc)) {
  94. return OrderItem.desc(orderBy);
  95. }
  96. }
  97. return null;
  98. }
  99. public static <T, K> TableDataInfo<K> buildDataInfo(PagePlus<T, K> page) {
  100. TableDataInfo<K> rspData = new TableDataInfo<>();
  101. rspData.setCode(HttpStatus.HTTP_OK);
  102. rspData.setMsg("查询成功");
  103. rspData.setRows(page.getRecordsVo());
  104. rspData.setTotal(page.getTotal());
  105. return rspData;
  106. }
  107. public static <T> TableDataInfo<T> buildDataInfo(Page<T> page) {
  108. TableDataInfo<T> rspData = new TableDataInfo<>();
  109. rspData.setCode(HttpStatus.HTTP_OK);
  110. rspData.setMsg("查询成功");
  111. rspData.setRows(page.getRecords());
  112. rspData.setTotal(page.getTotal());
  113. return rspData;
  114. }
  115. public static <T> TableDataInfo<T> buildDataInfo(List<T> list) {
  116. TableDataInfo<T> rspData = new TableDataInfo<>();
  117. rspData.setCode(HttpStatus.HTTP_OK);
  118. rspData.setMsg("查询成功");
  119. rspData.setRows(list);
  120. rspData.setTotal(list.size());
  121. return rspData;
  122. }
  123. public static <T> TableDataInfo<T> buildDataInfo() {
  124. TableDataInfo<T> rspData = new TableDataInfo<>();
  125. rspData.setCode(HttpStatus.HTTP_OK);
  126. rspData.setMsg("查询成功");
  127. return rspData;
  128. }
  129. }