Spring Boot 2.x实战60 - Spring Data 4 - Spring Data JPA查询(根据实体属性名推导查询)

2.6 查询

2.6.1 查询方法
2.6.1.1 推导查询

Spring Data JPA支持方法名来中的属性来推导出查询语句。

public interface PersonRepository extends JpaRepository<Person, Long> {
    List<Person> findByNameAndAge(String name, Integer age);
}

根据方法名findByNameAndAge翻译成查询JPQL语句:

select p from Person where p.name = ?1 and p.age = ?2

方法名中除了属性外的关键字包含:

关键字 示例 JPQL片段
And findByNameAndAge where p.name = ?1 and p.age = ?2
Or findByNameOrAge where p.name = ?1 or x.age = ?2
Is,Equals findByName
findByNameIs
findByNameEquals
where p.name = ?1
Between findByCreateTimeBetween where p.createTime between ?1 and ?2
LessThan findByAgeLessThan where p.age < ?1
LessThanEqual findByAgeLessThanEqual where p.age <= ?1
GreaterThan findByAgeGreaterThan where p.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual where p.age >= ?1
After findByCreateTimeAfter where p.createTime > ?1
Before findByCreateTimeBefore where p.createTime < ?1
IsNull findByAgeIsNull where p.age is null
IsNotNull,NotNull findByAge(Is)NotNull where p.age not null
Like findByNameLike where p.name like ?1
NotLike findByNameNotLike where p.name not like ?1
StartingWith findByNameStartingWith where p.name like ?1 (参数前附加%)
EndingWith findByNameEndingWith where p.name like ?1 (参数后附加%)
Containing findByNameContaining where p.name like ?1 (参数两边附加%)
OrderBy findByAgeOrderByNameDesc where p.age = ?1 order by p.name desc
Not findByNameNot where p.name <> ?1
In findByAgeIn(Collection ages) where p.age in ?1
NotIn findByAgeNotIn(Collection ages) where p.age not in ?1
True findByActiveTrue() where p.active = true
False findByActiveFalse() where p.active = false
IgnoreCase findByFirstnameIgnoreCase where UPPER(p.name) = UPPER(?1)

若以值对象的属性作为查询条件,则同样向后加属性名,中间可以用_隔开:

public interface PersonRepository extends JpaRepository<Person, Long> {
    List<Person> findByAddress_City(String city);
    List<Person> findByAddressCity(String city);
    List<Person> findByChildren_Name(String name);
    List<Person> findByChildrenName(String name);
}

我们使用CommandLineRunner验证:

@Bean
CommandLineRunner sortQuery(PersonRepository personRepository){
   return args -> {
      List<Person> people1 = personRepository.findByAgeLessThan(40, Sort.by("name"));
      List<Person> people2 = personRepository.findByAgeLessThanWithJqal(40, JpaSort.by(Sort.Direction.DESC, "name"));
      List<Person> people3 = personRepository.findAll(Sort.by("address.city"));
      List<Person> people4 = personRepository.findAll(JpaSort.by(Sort.Direction.DESC, "age"));
      Page<Person> people5 = personRepository.findByAgeLessThan(40, PageRequest.of(0, 2, Sort.by("age")));
         Page<Person> people6 = personRepository.findAll(PageRequest.of(0, 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);
      };
}

Spring Boot 2.x实战60 - Spring Data 4 - Spring Data JPA查询(根据实体属性名推导查询)_第1张图片

新书推荐:

我的新书《从企业级开发到云原生微服务: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)
多谢大家支持。

你可能感兴趣的:(Spring,Boot2.x实战全集,Spring,Boot2.x实战,-,Spring,Data)