spring-data-jpa中的查询方法

本文详细介绍了Spring Data JPA中的查询方法,包括如何在Repository中定义查询,支持的语法关键字,如And、Or、Between等,以及如何实现排序、分页和限制结果集。此外,还讲解了如何通过对象属性导航进行查询。

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

查询方法,就是根据方法名来检索数据。按照一定的规则,通过方法名描述要检索的字段,过滤的条件,排序的策略等等,它们大都以find, get… 等开头。spring-data-jpa会自动解析,并且完成检索。省时省力。

在 Repository 中定义查询方法

public interface UserRepository extends Repository<User, Long> {
  // 根据emailAddress和lastname 检索所有的记录
  List<User> findByEmailAddressAndLastname(String emailAddress, String lastname);
}

这个方法最终执行的JPQL

select u from User u where u.emailAddress = ?1 and u.lastname = ?2

支持的语法

关键字例如最终执行的JPQL 片段
AndfindByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2
OrfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
IsEqualsfindByFirstname , findByFirstnameIs , findByFirstnameEquals… where x.firstname = ?1
BetweenfindByStartDateBetween… where x.startDate between ?1 and ?2
LessThanfindByAgeLessThan… where x.age < ?1
LessThanEqualfindByAgeLessThanEqual… where x.age <= ?1
GreaterThanfindByAgeGreaterThan… where x.age > ?1
GreaterThanEqualfindByAgeGreaterThanEqual… where x.age >= ?1
AfterfindByStartDateAfter… where x.startDate > ?1
BeforefindByStartDateBefore… where x.startDate < ?1
IsNullNullfindByAge(Is)Null… where x.age is null
IsNotNullNotNullfindByAge(Is)NotNull… where x.age not null
LikefindByFirstnameLike… where x.firstname like ?1
NotLikefindByFirstnameNotLike… where x.firstname not like ?1
StartingWithfindByFirstnameStartingWith… where x.firstname like ?1  (parameter bound with appended  % )
EndingWithfindByFirstnameEndingWith… where x.firstname like ?1  (parameter bound with prepended  % )
ContainingfindByFirstnameContaining… where x.firstname like ?1  (parameter bound wrapped in  % )
OrderByfindByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname desc
NotfindByLastnameNot… where x.lastname <> ?1
InfindByAgeIn(Collection<Age> ages)… where x.age in ?1
NotInfindByAgeNotIn(Collection<Age> ages)… where x.age not in ?1
TruefindByActiveTrue()… where x.active = true
FalsefindByActiveFalse()… where x.active = false
IgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstame) = UPPER(?1)

使用到InNotIn等对集合进行计算关键的字方法,支持使用Collection子类,或者数组作为参数的形参。

支持对象属性导航

class User {
    // User对象关联了一个Address 对象
	Address address;
}
class Address {
	String name;
}
public interface UserRepositroy extends JpaRepository<User, Integer>{
    // 从address属性导航到它的name属性
	findByAddressName(String name); 

    // 更为科学的写法, 通过下户线标识遍历的节点
	findByAddress_Name(String name); 
}

JPA里面, 下户线是保留标识符,但是下划线又破话了Java的驼峰规则

分页和排序

只需要在方法的最后一个参数定义: Sort / Pageable 对象,即可自动的完成排序/分页

SortPageable是JPA定义用来排序和分页的对象

也可以通过 First/Top 方法名限制结果集

findFirst10ByName(String name);
findTop10ByName(String name);

以上2个方法,都表示根据 name 属性检索前10条记录

官方文档

这个花样确实多,不过用到的就那么几个,有兴趣,可以阅读官方文档系统学习
https://docs.spring.io/spring-data/jpa/docs/current-SNAPSHOT/reference/html/#jpa.query-methods

原文:https://springboot.io/t/topic/2208

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值