PageUtils.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. public static <T, K> PagePlus<T, K> buildPagePlus() {
  33. Integer pageNum = ServletUtils.getParameterToInt(PAGE_NUM);
  34. Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE);
  35. String orderByColumn = ServletUtils.getParameter(ORDER_BY_COLUMN);
  36. String isAsc = ServletUtils.getParameter(IS_ASC);
  37. PagePlus<T, K> page = new PagePlus<>(pageNum, pageSize);
  38. if (StrUtil.isNotBlank(orderByColumn)) {
  39. String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
  40. if ("asc".equals(isAsc)) {
  41. page.addOrder(OrderItem.asc(orderBy));
  42. } else if ("desc".equals(isAsc)) {
  43. page.addOrder(OrderItem.desc(orderBy));
  44. }
  45. }
  46. return page;
  47. }
  48. public static <T> Page<T> buildPage() {
  49. Integer pageNum = ServletUtils.getParameterToInt(PAGE_NUM);
  50. Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE);
  51. String orderByColumn = ServletUtils.getParameter(ORDER_BY_COLUMN);
  52. String isAsc = ServletUtils.getParameter(IS_ASC);
  53. Page<T> page = new Page<>(pageNum, pageSize);
  54. if (StrUtil.isNotBlank(orderByColumn)) {
  55. String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
  56. if ("asc".equals(isAsc)) {
  57. page.addOrder(OrderItem.asc(orderBy));
  58. } else if ("desc".equals(isAsc)) {
  59. page.addOrder(OrderItem.desc(orderBy));
  60. }
  61. }
  62. return page;
  63. }
  64. public static <T, K> TableDataInfo<K> buildDataInfo(PagePlus<T, K> page) {
  65. TableDataInfo<K> rspData = new TableDataInfo<>();
  66. rspData.setCode(HttpStatus.HTTP_OK);
  67. rspData.setMsg("查询成功");
  68. rspData.setRows(page.getRecordsVo());
  69. rspData.setTotal(page.getTotal());
  70. return rspData;
  71. }
  72. public static <T> TableDataInfo<T> buildDataInfo(Page<T> page) {
  73. TableDataInfo<T> rspData = new TableDataInfo<>();
  74. rspData.setCode(HttpStatus.HTTP_OK);
  75. rspData.setMsg("查询成功");
  76. rspData.setRows(page.getRecords());
  77. rspData.setTotal(page.getTotal());
  78. return rspData;
  79. }
  80. public static <T> TableDataInfo<T> buildDataInfo(List<T> list) {
  81. TableDataInfo<T> rspData = new TableDataInfo<>();
  82. rspData.setCode(HttpStatus.HTTP_OK);
  83. rspData.setMsg("查询成功");
  84. rspData.setRows(list);
  85. rspData.setTotal(list.size());
  86. return rspData;
  87. }
  88. }