PageUtil.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 me.zhengjie.utils;
  17. import org.springframework.data.domain.Page;
  18. import java.util.*;
  19. /**
  20. * 分页工具
  21. * @author Zheng Jie
  22. * @date 2018-12-10
  23. */
  24. public class PageUtil extends cn.hutool.core.util.PageUtil {
  25. /**
  26. * List 分页
  27. */
  28. public static List toPage(int page, int size , List list) {
  29. int fromIndex = page * size;
  30. int toIndex = page * size + size;
  31. if(fromIndex > list.size()){
  32. return new ArrayList();
  33. } else if(toIndex >= list.size()) {
  34. return list.subList(fromIndex,list.size());
  35. } else {
  36. return list.subList(fromIndex,toIndex);
  37. }
  38. }
  39. /**
  40. * Page 数据处理,预防redis反序列化报错
  41. */
  42. public static Map<String,Object> toPage(Page page) {
  43. Map<String,Object> map = new LinkedHashMap<>(2);
  44. map.put("content",page.getContent());
  45. map.put("totalElements",page.getTotalElements());
  46. return map;
  47. }
  48. /**
  49. * 自定义分页
  50. */
  51. public static Map<String,Object> toPage(Object object, Object totalElements) {
  52. Map<String,Object> map = new LinkedHashMap<>(2);
  53. map.put("content",object);
  54. map.put("totalElements",totalElements);
  55. return map;
  56. }
  57. }