PageUtils.java 4.2 KB

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