前言
在實際項目中對Spring Data的各種使用相當多,簡單的增刪改查Spring Data提供了現成的方法,一些復雜的,我們可以在接口方法寫And,Not等關鍵字來搞定,想寫原生SQL,CQL(Neo4j),Query DSL (Elasticsearch)的,直接使用@Query(“......”)注解搞定,真的是方便到不行!
當我們執行批量操作時,比如從數據庫中查找“Person”的所有實例或者根據國家查找每個人,我們經常進行分頁,以便我們可以向最終用戶提供一個小數據塊,并在下一個請求中,我們獲取下一個數據塊。
Spring Data為分頁提供支持。它創建了實現分頁的所有邏輯,例如所有頁面的行計數等等。
在Spring Data中實現分頁非常簡單。我們只需要按照以下步驟操作:
1.創建擴展PagingAndSortingRepository的存儲庫。
@Repositorypublic 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. 創建域對象。
@Entitypublic 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對象并請求第一頁。
@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 方法如下:
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 ?
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VeVb武林網的支持。
新聞熱點
疑難解答
圖片精選