软件

SpringMVC分页和排序

2017-09-14  本文已影响9人  menglj

使用org.springframework.data.domain.Pageable接口类型作为入参即可实现查询列表的分页和排序,见下面例子:

@RequestMapping(value = "blogs", method=RequestMethod.GET)
public Page<Blog> getEntryByPageable(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC) 
    Pageable pageable) {
    return blogRepository.findAll(pageable);
}

当Spring发现这个参数时,会自动的根据request的参数来组装该pageable对象,Spring支持的request参数如下:

结果如下:

{
  "content":[
    {"id":123,"title":"blog122","content":"this is blog content"},
...
    {"id":109,"title":"blog108","content":"this is blog content"}],
  "last":false,
  "totalPages":9,
  "totalElements":123,
  "size":15,
  "number":0,
  "first":true,
  "sort":[{
    "direction":"DESC",
    "property":"id",
    "ignoreCase":false,
    "nullHandling":"NATIVE",
    "ascending":false
  }],
  "numberOfElements":15
}

上述json字段说明如下:

参考

  1. 整合Spring Data JPA与Spring MVC: 分页和排序
上一篇 下一篇

猜你喜欢

热点阅读