实现Springdata jpa 分页排序参数的快速绑定及输出pageable对象。
package com.bz.heimdallr.vo;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@Data
@Slf4j
@ApiModel
public class PageVO {
Integer page;
Integer limit;
List<Order> sort;
public Pageable toPageable() {
if(limit == null){
limit = Integer.MAX_VALUE;
}
if (page == null){
page = 1;
}
List<Sort.Order> orders = new ArrayList<>(4);
if(sort != null){
for (PageVO.Order order: sort){
orders.add(new Sort.Order(Sort.Direction.fromString(order.direction), order.column));
}
}
return PageRequest.of(page - 1, limit, Sort.by(orders));
}
@Data
public static class Order{
private String column;
private String direction;
}
}
前端参数-》
page: 1
limit: 10
sort[0].column: ctime
sort[0].direction: DESC
网友评论