PageUtil.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2019-2020 Zheng Jie
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package easydo.technology.utils;
  17. import com.baomidou.mybatisplus.core.metadata.IPage;
  18. import com.baomidou.mybatisplus.core.metadata.OrderItem;
  19. import org.springframework.data.domain.Page;
  20. import org.springframework.data.domain.Pageable;
  21. import org.springframework.data.domain.Sort;
  22. import java.util.*;
  23. /**
  24. * 分页工具
  25. *
  26. * @author Zheng Jie
  27. * @date 2018-12-10
  28. */
  29. public class PageUtil {
  30. /**
  31. * List 分页
  32. */
  33. public static <T> List<T> toPage(int page, int size, List<T> list) {
  34. int fromIndex = page * size;
  35. int toIndex = page * size + size;
  36. if (fromIndex > list.size()) {
  37. return new ArrayList<>();
  38. } else if (toIndex >= list.size()) {
  39. return list.subList(fromIndex, list.size());
  40. } else {
  41. return list.subList(fromIndex, toIndex);
  42. }
  43. }
  44. /**
  45. * Page 数据处理,预防redis反序列化报错
  46. */
  47. public static <T> Map<String, Object> toPage(Page<T> page) {
  48. Map<String, Object> map = new LinkedHashMap<>(2);
  49. map.put("content", page.getContent());
  50. map.put("totalElements", page.getTotalElements());
  51. return map;
  52. }
  53. /**
  54. * 自定义分页
  55. */
  56. public static Map<String, Object> toPage(Object object, Object totalElements) {
  57. Map<String, Object> map = new LinkedHashMap<>(2);
  58. map.put("content", object);
  59. map.put("totalElements", totalElements);
  60. return map;
  61. }
  62. public static <T> IPage<T> toMybatisPage(Pageable pageable) {
  63. return toMybatisPage(pageable, false);
  64. }
  65. public static <T> IPage<T> toMybatisPage(Pageable pageable, boolean ignoreOrderBy) {
  66. com.baomidou.mybatisplus.extension.plugins.pagination.Page<T> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(
  67. pageable.getPageNumber() + 1, pageable.getPageSize());
  68. if (!ignoreOrderBy) {
  69. for (Sort.Order order : pageable.getSort()) {
  70. OrderItem orderItem = new OrderItem();
  71. orderItem.setAsc(order.isAscending());
  72. orderItem.setColumn(
  73. com.baomidou.mybatisplus.core.toolkit.StringUtils.camelToUnderline(order.getProperty()));
  74. page.addOrder(orderItem);
  75. }
  76. }
  77. return page;
  78. }
  79. }