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

本文深入探讨了Spring Data JPA的推导查询功能,通过方法名中的属性来自动构建JPQL查询语句。内容涵盖如何利用属性名和值对象属性作为查询条件,以及实际应用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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片段
AndfindByNameAndAgewhere p.name = ?1 and p.age = ?2
OrfindByNameOrAgewhere p.name = ?1 or x.age = ?2
Is,EqualsfindByName
findByNameIs
findByNameEquals
where p.name = ?1
BetweenfindByCreateTimeBetweenwhere p.createTime between ?1 and ?2
LessThanfindByAgeLessThanwhere p.age < ?1
LessThanEqualfindByAgeLessThanEqualwhere p.age <= ?1
GreaterThanfindByAgeGreaterThanwhere p.age > ?1
GreaterThanEqualfindByAgeGreaterThanEqualwhere p.age >= ?1
AfterfindByCreateTimeAfterwhere p.createTime > ?1
BeforefindByCreateTimeBeforewhere p.createTime < ?1
IsNullfindByAgeIsNullwhere p.age is null
IsNotNull,NotNullfindByAge(Is)NotNullwhere p.age not null
LikefindByNameLikewhere p.name like ?1
NotLikefindByNameNotLikewhere p.name not like ?1
StartingWithfindByNameStartingWithwhere p.name like ?1 (参数前附加%)
EndingWithfindByNameEndingWithwhere p.name like ?1 (参数后附加%)
ContainingfindByNameContainingwhere p.name like ?1 (参数两边附加%)
OrderByfindByAgeOrderByNameDescwhere p.age = ?1 order by p.name desc
NotfindByNameNotwhere p.name <> ?1
InfindByAgeIn(Collection<Age> ages)where p.age in ?1
NotInfindByAgeNotIn(Collection<Age> ages)where p.age not in ?1
TruefindByActiveTrue()where p.active = true
FalsefindByActiveFalse()where p.active = false
IgnoreCasefindByFirstnameIgnoreCasewhere 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 实战》已出版,内容涵盖了丰富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)
多谢大家支持。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值