Spring Boot 2.x实战62 - Spring Data 6 - Spring Data JPA查询(排序和分页、命名参数、修改查询)

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.DESC, "name")); //2
      List<Person> people3 = personRepository.findAll(Sort.by("address.city")); //3
      List<Person> people4 = personRepository.findAll(JpaSort.by(Sort.Direction.DESC, "age")); 
     	Page<Person> people5 = personRepository.findByAgeLessThan(40, PageRequest.of(1, 2, Sort.by("age"))); //4
 			Page<Person> people6 = personRepository.findAll(PageRequest.of(1, 2, Sort.by("name")));

      people1.forEach(System.out::println);
      System.out.println("--------------");
      people2.forEach(System.out::println);
      System.out.println("--------------");
      people3.forEach(System.out::println);
      System.out.println("--------------");
      people4.forEach(System.out::println);
     	System.out.println("--------------");
			people5.forEach(System.out::println);
			System.out.println("--------------");
			people6.forEach(System.out::println);
      };
}
  1. 可以使用Sort的静态方法by构建Sort对象,默认为升序,按照name排序;
  2. 可以使用JpaSort的静态方法by构建Sort对象,使用Direction来设定升序还是降序;
  3. 可以通过address.city来根据内嵌对象的属性排序;
  4. PageRequestPageable接口的实现类,可以用它的静态方法of来构造分也和排序;分页开始索引是0,每页数量设置为2。
    在这里插入图片描述
2.6.1.6 命名参数

前面我们入参是通过参数位置来设置的,这是Spring Data JPA的默认行为。我们还可以通过使用@Param注解来绑定参数,在查询语句中使用“:参数名”。

public interface PersonRepository extends JpaRepository<Person, Long> {

    @Query("select p from Person p where p.name = :name")
    List<Person> findByJpqlWithNamedParameter(@Param("name") String name);

}
@Bean
CommandLineRunner namedParamQuery(PersonRepository personRepository){
   return args -> {
      List<Person> people = personRepository.findByJpqlWithNamedParameter("wyf");
      people.forEach(System.out::println);
   };
}
2.6.1.7 修改查询

当我们使用@Query中使用新增、更新、删除时,我们使用@Modifying来注解这个语句时个修改查询语句。

public interface PersonRepository extends JpaRepository<Person, Long> {

    @Transactional
    @Modifying
    @Query("update Person p set p.name = ?1 where p.name =?2")
    int updatePersonName(String newName, String oldName);
}

JpaRepository的代理实现SimpleJpaRepository对全局的事务设置为@Transactional(readOnly = true),所以此处要额外声明@Transactional来覆盖默认配置。

@Bean
CommandLineRunner modifyingQuery(PersonRepository personRepository){
   return args -> {
      int result = personRepository.updatePersonName("foooo", "foo");
      List<Person> people = personRepository.findByName("foooo");
      people.forEach(System.out::println);

   };
}

新书推荐:

我的新书《从企业级开发到云原生微服务:Spring Boot 实战》已出版,内容涵盖了丰富Spring Boot开发的相关知识
购买地址:https://item.jd.com/12760084.html

在这里插入图片描述
主要包含目录有:

第一章 初识Spring Boot(快速领略Spring Boot的美丽)
第二章 开发必备工具(对常用开发工具进行介绍:包含IntelliJ IDEA、Gradle、Lombok、Docker等)
第三章 函数式编程
第四章 Spring 5.x基础(以Spring 5.2.x为基础)
第五章 深入Spring Boot(以Spring Boot 2.2.x为基础)
第六章 Spring Web MVC
第七章 数据访问(包含Spring Data JPA、Spring Data Elasticsearch和数据缓存)
第八章 安全控制(包含Spring Security和OAuth2)
第九章 响应式编程(包含Project Reactor、Spring WebFlux、Reactive NoSQL、R2DBC、Reactive Spring Security)
第十章 事件驱动(包含JMS、RabbitMQ、Kafka、Websocket、RSocket)
第11章 系统集成和屁股里(包含Spring Integration和Spring Batch)
第12章 Spring Cloud与微服务
第13章 Kubernetes与微服务(包含Kubernetes、Helm、Jenkins、Istio)
多谢大家支持。

猜你喜欢

转载自blog.csdn.net/wiselyman/article/details/106395745
今日推荐