本篇文章为大家展示了使用Spring Data怎么实现分页与排序,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、虚拟空间、营销软件、网站建设、文登网站维护、网站推广。
1.创建扩展PagingAndSortingRepository的存储库。
@Repository public interface PersonRepositary extends PagingAndSortingRepository,QueryDslPredicateExecutor { @Query("select p from Person p where p.country like ?1 order by country") List findByCountryContains(String country); List 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) Listhobby; 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 getHobby() { return hobby; } public void setHobby(List hobby) { this.hobby = hobby; } public void addHobby(Hobby ihobby) { if(hobby == null) { hobby = new ArrayList (); } 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) { IterablepList = 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怎么实现分页与排序,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。