前言
在實際項目中對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://www.jdon.com/50891