对 分页插件进一步封装

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 @GetMapping("/test/page")
    public Result testPage(@PageableDefault(page = 1,size = 5) Pageable page) {
        log.info("test-sort = {}",page.getSort());


        Sort s = page.getSort();
        List list = s.stream().map(order -> {
            Sort.Direction direction = order.getDirection();
            String property = order.getProperty();
            return property + " " + direction;
        }).collect(Collectors.toList());
        return ResultUtil.mapOf("data",page,"sortList",list);
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
{
  "code": 200,
  "message": "OK",
  "data": {
    "data": {
      "sort": {
        "empty": false,
        "sorted": true,
        "unsorted": false
      },
      "offset": 50,
      "pageNumber": 1,
      "pageSize": 50,
      "unpaged": false,
      "paged": true
    },
    "sortList": [
      "id ASC"
    ]
  }
}

参考博客园给的例子

https://www.cnblogs.com/cielosun/p/11222127.html

这个工具类要导入 java 的依赖

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import com.github.pagehelper.PageHelper;
import com.google.common.base.CaseFormat;
import org.springframework.data.domain.Pageable;

import java.util.stream.Collectors;

public class PageUtil {
    public static Page startPage(Pageable pageable) {
         return PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize()
                , pageable.getSort().stream().map(order -> CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order.getProperty()) + " " + order.getDirection()).collect(Collectors.joining(",")));
    }
}