2015-08-25 65 views
0

我想在春季MVC中实现分页和排序。根据我的理解,我们可以使用PagingAndSortingRepository或JpaRepository(http://springinpractice.com/2012/05/11/pagination-and-sorting-with-spring-data-jpa)。春季MVC分页和排序

但这两个都使用默认的findAll方法来做到这一点。

我要创造我自己的方法和执行自定义查询并进行分页以及排序上(可以说按类别名称和排序创建日期搜索)。我不知道如何通过使用PagingAndSortingRepository或JpaRepository来做到这一点。

如果我能有某种指导来达到这个目标,那将会很棒。

在此先感谢。

+0

我在这里没有看到问题.... – ChiefTwoPencils

+0

@ChiefTwoPencils我已更新/编辑问题 –

回答

1

使用JPA,只需指定方法签名即可完成多种查询组合。请咨询http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE/reference/html/jpa.repositories.html

在你的资料库界面,您可以

List<Person> findAll(); // this is standard 
List<Person> findById(String id); // looking person that have specific Id 
List<Person> findByNameLike(String name); // you can put the name "foo%" 

如果你想分页和排序...

Page<Person> findByNameLike(String name, PageRequest pageRequest); 

你使用它像

int page = 0; // first page 
int size = 10; // show 10 result max per page 
Page personPage = repo.findByNameLike("A%", new PageRequest(page,size,Sort.Direction.ASC, "birthDate")); // pagination for person with name, page 0, 10 item per page, and sorted by the person.birthDate. 

好运

+0

感谢您的解释。你可以请分享一样的详细教程吗? –

+0

你可以看到这个文档http://docs.spring.io/spring-data/data-jpa/docs/1.0.0.M1/reference/html/#repositories.special-parameters 从这个自带的http:// stackoverflow.com/questions/14120153/spring-data-jpa-apply-sorting-pagination-along-with-a-where-clause 它非常简单直接 –