PageUtils.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * @param <T> domain 实体
  42. * @param <K> vo 实体
  43. * @return 分页对象
  44. */
  45. public static <T, K> PagePlus<T, K> buildPagePlus() {
  46. Integer pageNum = ServletUtils.getParameterToInt(PAGE_NUM, DEFAULT_PAGE_NUM);
  47. Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE, DEFAULT_PAGE_SIZE);
  48. String orderByColumn = ServletUtils.getParameter(ORDER_BY_COLUMN);
  49. String isAsc = ServletUtils.getParameter(IS_ASC);
  50. if (pageNum <= 0) {
  51. pageNum = DEFAULT_PAGE_NUM;
  52. }
  53. PagePlus<T, K> page = new PagePlus<>(pageNum, pageSize);
  54. OrderItem orderItem = buildOrderItem(orderByColumn, isAsc);
  55. page.addOrder(orderItem);
  56. return page;
  57. }
  58. public static <T> Page<T> buildPage() {
  59. return buildPage(null, null);
  60. }
  61. /**
  62. * 构建 MP 普通分页对象
  63. * @param <T> domain 实体
  64. * @return 分页对象
  65. */
  66. public static <T> Page<T> buildPage(String defaultOrderByColumn, String defaultIsAsc) {
  67. Integer pageNum = ServletUtils.getParameterToInt(PAGE_NUM, DEFAULT_PAGE_NUM);
  68. Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE, DEFAULT_PAGE_SIZE);
  69. String orderByColumn = ServletUtils.getParameter(ORDER_BY_COLUMN, defaultOrderByColumn);
  70. String isAsc = ServletUtils.getParameter(IS_ASC, defaultIsAsc);
  71. if (pageNum <= 0) {
  72. pageNum = DEFAULT_PAGE_NUM;
  73. }
  74. Page<T> page = new Page<>(pageNum, pageSize);
  75. OrderItem orderItem = buildOrderItem(orderByColumn, isAsc);
  76. page.addOrder(orderItem);
  77. return page;
  78. }
  79. private static OrderItem buildOrderItem(String orderByColumn, String isAsc) {
  80. // 兼容前端排序类型
  81. if ("ascending".equals(isAsc)) {
  82. isAsc = "asc";
  83. } else if ("descending".equals(isAsc)) {
  84. isAsc = "desc";
  85. }
  86. if (StringUtils.isNotBlank(orderByColumn)) {
  87. String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
  88. orderBy = StringUtils.toUnderScoreCase(orderBy);
  89. if ("asc".equals(isAsc)) {
  90. return OrderItem.asc(orderBy);
  91. } else if ("desc".equals(isAsc)) {
  92. return OrderItem.desc(orderBy);
  93. }
  94. }
  95. return null;
  96. }
  97. public static <T, K> TableDataInfo<K> buildDataInfo(PagePlus<T, K> page) {
  98. TableDataInfo<K> rspData = new TableDataInfo<>();
  99. rspData.setCode(HttpStatus.HTTP_OK);
  100. rspData.setMsg("查询成功");
  101. rspData.setRows(page.getRecordsVo());
  102. rspData.setTotal(page.getTotal());
  103. return rspData;
  104. }
  105. public static <T> TableDataInfo<T> buildDataInfo(Page<T> page) {
  106. TableDataInfo<T> rspData = new TableDataInfo<>();
  107. rspData.setCode(HttpStatus.HTTP_OK);
  108. rspData.setMsg("查询成功");
  109. rspData.setRows(page.getRecords());
  110. rspData.setTotal(page.getTotal());
  111. return rspData;
  112. }
  113. public static <T> TableDataInfo<T> buildDataInfo(List<T> list) {
  114. TableDataInfo<T> rspData = new TableDataInfo<>();
  115. rspData.setCode(HttpStatus.HTTP_OK);
  116. rspData.setMsg("查询成功");
  117. rspData.setRows(list);
  118. rspData.setTotal(list.size());
  119. return rspData;
  120. }
  121. public static <T> TableDataInfo<T> buildDataInfo() {
  122. TableDataInfo<T> rspData = new TableDataInfo<>();
  123. rspData.setCode(HttpStatus.HTTP_OK);
  124. rspData.setMsg("查询成功");
  125. return rspData;
  126. }
  127. }