Spring Data分頁和排序
當我們執行批量操作時,比如從資料庫中查詢“Person”的所有例項或者根據國家查詢每個人,我們經常進行分頁,以便我們可以向終端使用者提供一個小資料塊,並在下一個請求中,我們獲取下一個資料塊。
Spring Data為分頁提供支援。它建立了實現分頁的所有邏輯,例如所有頁面的行計數等等。
在Spring Data中實現分頁非常簡單。我們只需要按照以下步驟操作:
- 在自定義儲存庫中,擴充套件 PagingAndSortingRepository。
- 建立PageRequest物件,該物件是Pageable介面的實現。 此PageRequest物件獲取頁碼,頁面大小以及排序方向和排序欄位。
- 通過傳遞請求的頁碼和頁面限制,您可以獲取此頁面的資料。如果您傳遞錯誤的頁碼,Spring Data將負責處理並且不返回任何資料。
1.建立擴充套件PagingAndSortingRepository的儲存庫。
@Repository <b>public</b> <b>interface</b> PersonRepositary <b>extends</b> PagingAndSortingRepository<Person, Long>,QueryDslPredicateExecutor<Person> { @Query(<font>"select p from Person p where p.country like ?1 order by country"</font><font>) List<Person> findByCountryContains(String country); List<Person> findPersonByHobbyName(String name); @Query(</font><font>"select p from Person p where p.id = ?1 andcountry='America'"</font><font>) Person findOne(Long id); } </font>
2. 建立域物件。
@Entity <b>public</b> <b>class</b> Person { @Id @GeneratedValue(strategy=GenerationType.AUTO) <b>private</b> Long id; <b>private</b> String name; <b>private</b> String country; <b>private</b> String gender; @OneToMany(mappedBy=<font>"person"</font><font>,targetEntity=Hobby.<b>class</b>, fetch=FetchType.EAGER,cascade=CascadeType.ALL) List<Hobby> hobby; <b>public</b> String getName() { <b>return</b> name; } <b>public</b> <b>void</b> setName(String name) { <b>this</b>.name = name; } <b>public</b> String getCountry() { <b>return</b> country; } <b>public</b> <b>void</b> setCountry(String country) { <b>this</b>.country = country; } <b>public</b> String getGender() { <b>return</b> gender; } <b>public</b> <b>void</b> setGender(String gender) { <b>this</b>.gender = gender; } <b>public</b> Long getId() { <b>return</b> id; } <b>public</b> <b>void</b> setId(Long id) { <b>this</b>.id = id; } <b>public</b> List<Hobby> getHobby() { <b>return</b> hobby; } <b>public</b> <b>void</b> setHobby(List<Hobby> hobby) { <b>this</b>.hobby = hobby; } <b>public</b> <b>void</b> addHobby(Hobby ihobby) { <b>if</b>(hobby == <b>null</b>) { hobby = <b>new</b> ArrayList<Hobby>(); } hobby.add(ihobby); } @Override <b>public</b> String toString() { <b>return</b> </font><font>"Person [id="</font><font> + id + </font><font>", name="</font><font> + name + </font><font>", country="</font><font> + country + </font><font>", gender="</font><font> + gender + </font><font>"]"</font><font>; } } </font>
3.獲取所有人員。建立一個限制為1的PageRequest物件並請求第一頁。
@SpringBootApplication @EnableJpaRepositories(<font>"com.example.repo"</font><font>) <b>public</b> <b>class</b> PersonApplication { @Autowired HobbyRepository hRepo; <b>private</b> <b>static</b> <b>final</b> Logger log = LoggerFactory.getLogger(PersonApplication.<b>class</b>); @Bean <b>public</b> CommandLineRunner demo(PersonRepositary repository) { findAll(repository); <b>return</b> <b>null</b>; } <b>private</b> PageRequest gotoPage(<b>int</b> page) { PageRequest request = <b>new</b> PageRequest(page,1) <b>return</b> request; } <b>private</b> <b>void</b> findAll(PersonRepositary repository) { Iterable<Person> pList = repository.findAll(gotoPage(0)); <b>for</b>(Person p : pList) log.info(</font><font>"Person "</font><font> + p); } <b>public</b> <b>static</b> <b>void</b> main(String[] args) { SpringApplication.run(PersonApplication.<b>class</b>, args); } } </font>
執行時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 方法如下:
<b>private</b> PageRequest gotoPage(<b>int</b> page) { PageRequest request = <b>new</b> PageRequest(page,1,Sort.Direction.ASC,<font>"country"</font><font>); <b>return</b> request; } </font>
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 ?