2.6.1.5 排序和分页
我们只需要在Repository的方法里使用Sort
作为参数即可;方法接受Pageable
参数即可分页也可排序。
public interface PersonRepository extends JpaRepository<Person, Long> {
List<Person> findByAgeLessThan(Integer age, Sort sort);
@Query("select p from Person p where p.age < ?1")
List<Person> findByAgeLessThanWithJqal(Integer age, Sort sort);
Page<Person> findByAgeLessThan(Integer age, Pageable pageable);
}
我们使用代码验证:
@Bean
CommandLineRunner sortQuery(PersonRepository personRepository){
return args -> {
List<Person> people1 = personRepository.findByAgeLessThan(40, Sort.by("name")); //1
List<Person> people2 = personRepository.findByAgeLessThanWithJqal(40, JpaSort.by(Sort.Direction