Spring Data分頁與排序的實現(xiàn)方法
前言
在實際項目中對Spring Data的各種使用相當多,簡單的增刪改查Spring Data提供了現(xiàn)成的方法,一些復雜的,我們可以在接口方法寫And,Not等關(guān)鍵字來搞定,想寫原生SQL,CQL(Neo4j),Query DSL (Elasticsearch)的,直接使用@Query(“......”)注解搞定,真的是方便到不行!
當我們執(zhí)行批量操作時,比如從數(shù)據(jù)庫中查找“Person”的所有實例或者根據(jù)國家查找每個人,我們經(jīng)常進行分頁,以便我們可以向最終用戶提供一個小數(shù)據(jù)塊,并在下一個請求中,我們獲取下一個數(shù)據(jù)塊。
Spring Data為分頁提供支持。它創(chuàng)建了實現(xiàn)分頁的所有邏輯,例如所有頁面的行計數(shù)等等。
在Spring Data中實現(xiàn)分頁非常簡單。我們只需要按照以下步驟操作:
- 在自定義存儲庫中,擴展 PagingAndSortingRepository。
- 創(chuàng)建PageRequest對象,該對象是Pageable接口的實現(xiàn)。 此PageRequest對象獲取頁碼,頁面大小以及排序方向和排序字段。
- 通過傳遞請求的頁碼和頁面限制,您可以獲取此頁面的數(shù)據(jù)。如果您傳遞錯誤的頁碼,Spring Data將負責處理并且不返回任何數(shù)據(jù)。
1.創(chuàng)建擴展PagingAndSortingRepository的存儲庫。
@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. 創(chuàng)建域?qū)ο蟆?/p>
@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.獲取所有人員。創(chuàng)建一個限制為1的PageRequest對象并請求第一頁。
@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]
分頁和排序代碼實現(xiàn)
要進行排序,我們必須傳遞排序方向和排序字段以及頁碼和限制。假設我們想按國家名稱按升序排序 - 我們修改 goto 方法如下:
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 ?
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Spring Boot實現(xiàn)郵件發(fā)送必會的5種姿勢
這篇文章主要給大家介紹了關(guān)于Spring Boot實現(xiàn)郵件發(fā)送必會的5種姿勢,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-07-07
Mybatis動態(tài)SQL foreach標簽用法實例
這篇文章主要介紹了Mybatis動態(tài)SQL foreach標簽用法實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10
IDEA最新版2020.1的maven工程本地依賴倉庫無法使用問題(已解決)
這篇文章主要介紹了IDEA最新版2020.1的maven工程本地依賴倉庫無法使用問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
Java并發(fā)編程ThreadLocalRandom類詳解
這篇文章主要介紹了Java并發(fā)編程ThreadLocalRandom類詳解,通過提出問題為什么需要ThreadLocalRandom展開詳情,感興趣的朋友可以參考一下2022-06-06
Java redis存Map對象類型數(shù)據(jù)的實現(xiàn)
本文主要介紹了Java redis存Map<String,RedisCustom>對象類型數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05
使用Springboot+poi上傳并處理百萬級數(shù)據(jù)EXCEL
這篇文章主要介紹了使用Springboot+poi上傳并處理百萬級數(shù)據(jù)EXCEL,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12

