PageUtils.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /**
  63. * 构建 MP 普通分页对象
  64. * @param <T> domain 实体
  65. * @return 分页对象
  66. */
  67. public static <T> Page<T> buildPage() {
  68. Integer pageNum = ServletUtils.getParameterToInt(PAGE_NUM, DEFAULT_PAGE_NUM);
  69. Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE, DEFAULT_PAGE_SIZE);
  70. String orderByColumn = ServletUtils.getParameter(ORDER_BY_COLUMN);
  71. String isAsc = ServletUtils.getParameter(IS_ASC);
  72. Page<T> page = new Page<>(pageNum, pageSize);
  73. if (StrUtil.isNotBlank(orderByColumn)) {
  74. String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
  75. if ("asc".equals(isAsc)) {
  76. page.addOrder(OrderItem.asc(orderBy));
  77. } else if ("desc".equals(isAsc)) {
  78. page.addOrder(OrderItem.desc(orderBy));
  79. }
  80. }
  81. return page;
  82. }
  83. public static <T, K> TableDataInfo<K> buildDataInfo(PagePlus<T, K> page) {
  84. TableDataInfo<K> rspData = new TableDataInfo<>();
  85. rspData.setCode(HttpStatus.HTTP_OK);
  86. rspData.setMsg("查询成功");
  87. rspData.setRows(page.getRecordsVo());
  88. rspData.setTotal(page.getTotal());
  89. return rspData;
  90. }
  91. public static <T> TableDataInfo<T> buildDataInfo(Page<T> page) {
  92. TableDataInfo<T> rspData = new TableDataInfo<>();
  93. rspData.setCode(HttpStatus.HTTP_OK);
  94. rspData.setMsg("查询成功");
  95. rspData.setRows(page.getRecords());
  96. rspData.setTotal(page.getTotal());
  97. return rspData;
  98. }
  99. public static <T> TableDataInfo<T> buildDataInfo(List<T> list) {
  100. TableDataInfo<T> rspData = new TableDataInfo<>();
  101. rspData.setCode(HttpStatus.HTTP_OK);
  102. rspData.setMsg("查询成功");
  103. rspData.setRows(list);
  104. rspData.setTotal(list.size());
  105. return rspData;
  106. }
  107. }