转载

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 and  country='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 ?
原文  https://www.jdon.com/50891
正文到此结束
Loading...