您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用Spring Data怎么實現分頁與排序,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
1.創建擴展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. 創建域對象。
@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對象并請求第一頁。
@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 ?
上述內容就是使用Spring Data怎么實現分頁與排序,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。