前言
在实际项目中对spring data的各种使用相当多,简单的增删改查spring data提供了现成的方法,一些复杂的,我们可以在接口方法写and,not等关键字来搞定,想写原生sql,cql(neo4j),query dsl (elasticsearch)的,直接使用@query([......])注解搞定,真的是方便到不行!
当我们执行批量操作时,比如从数据库中查找[person]的所有实例或者根据国家查找每个人,我们经常进行 分页 ,以便我们可以向最终用户提供一个小数据块,并在下一个请求中,我们获取下一个数据块。
spring data为分页提供支持。它创建了实现分页的所有逻辑,例如所有页面的行计数等等。
在spring data中实现分页非常简单。我们只需要按照以下步骤操作:
在自定义存储库中,扩展 pagingandsortingrepository。 创建pagerequest对象,该对象是pageable接口的实现。 此pagerequest对象获取页码,页面大小以及 排序 方向和排序字段。 通过传递请求的页码和页面限制,您可以获取此页面的数据。如果您传递错误的页码,spring data将负责处理并且不返回任何数据。1.创建扩展pagingandsortingrepository的存储库。
1 2 3 4 5 6 7 8 9 10 11 |
@repository public interface personrepositary extends pagingandsortingrepository<person, long >,querydslpredicateexecutor<person> {
@query ( "select p from person p where p.country like ?1 order by country" ) list<person> findbycountrycontains(string country);
list<person> findpersonbyhobbyname(string name);
@query ( "select p from person p where p.id = ?1 and country='america'" ) person findone( long id); } |
2. 创建域对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
@entity public class person { @id @generatedvalue (strategy=generationtype.auto) private long id; private string name; private string country; private string gender; @onetomany (mappedby= "person" ,targetentity=hobby. class , fetch=fetchtype.eager,cascade=cascadetype.all) list<hobby> hobby; public string getname() { return name; } public void setname(string name) { this .name = name; } public string getcountry() { return country; } public void setcountry(string country) { this .country = country; } public string getgender() { return gender; } public void setgender(string gender) { this .gender = gender; } public long getid() { return id; } public void setid( long id) { this .id = id; } public list<hobby> gethobby() { return hobby; } public void sethobby(list<hobby> hobby) { this .hobby = hobby; } public void addhobby(hobby ihobby) { if (hobby == null ) { hobby = new arraylist<hobby>(); } hobby.add(ihobby); } @override public string tostring() { return "person [id=" + id + ", name=" + name + ", country=" + country + ", gender=" + gender + "]" ; } } |
3.获取所有人员。创建一个限制为1的pagerequest对象并请求第一页。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
@springbootapplication @enablejparepositories ( "com.example.repo" ) public class personapplication { @autowired hobbyrepository hrepo;
private static final logger log = loggerfactory.getlogger(personapplication. class );
@bean public commandlinerunner demo(personrepositary repository) { findall(repository); return null ; }
private pagerequest gotopage( int page) { pagerequest request = new pagerequest(page, 1 ) return request; }
private void findall(personrepositary repository) { iterable<person> plist = repository.findall(gotopage( 0 )); for (person p : plist) log.info( "person " + p); }
public static void main(string[] args) { springapplication.run(personapplication. class , args); } } |
运行时sql输出:
hibernate:
select
count(person0_.id) as col_0_0_
from
person person0_
hibernate:
select
person0_.id as id1_1_,
person0_.country as country2_1_,
person0_.gender as gender3_1_,
person0_.name as name4_1_
from
person person0_ limit ?
person person [id=13, name=samir mitra, country=america, gender=male]
分页和排序代码实现
要进行排序,我们必须传递排序方向和排序字段以及页码和限制。假设我们想按国家名称按升序排序 - 我们修改 goto 方法如下:
1 2 3 4 5 |
private pagerequest gotopage( int page) { pagerequest request = new pagerequest(page, 1 ,sort.direction.asc, "country" ); return request; } |
sql输出:
select
count(person0_.id) as col_0_0_
from
person person0_
hibernate:
select
person0_.id as id1_1_,
person0_.country as country2_1_,
person0_.gender as gender3_1_,
person0_.name as name4_1_
from
person person0_
order by
person0_.country asc limit ?
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。
原文链接:https://HdhCmsTestjdon测试数据/50891
查看更多关于Spring Data分页与排序的实现方法的详细内容...