spring-data-jpa中的查询方法
2021-04-19 00:27
标签:order ddr between 集合 lis lse als wrap head 查询方法,就是根据方法名来检索数据。按照一定的规则,通过方法名描述要检索的字段,过滤的条件,排序的策略等等,它们大都以 这个方法最终执行的JPQL 使用到 JPA里面, 下户线是保留标识符,但是下划线又破话了Java的驼峰规则 只需要在方法的最后一个参数定义: 以上2个方法,都表示根据 这个花样确实多,不过用到的就那么几个,有兴趣,可以阅读官方文档系统学习 原文:https://springboot.io/t/topic/2208 spring-data-jpa中的查询方法 标签:order ddr between 集合 lis lse als wrap head 原文地址:https://www.cnblogs.com/kevinblandy/p/13292625.htmlfind, get... 等开头。spring-data-jpa会自动解析,并且完成检索。省时省力。在 Repository 中定义查询方法
public interface UserRepository extends Repositoryselect 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
Is ,? Equals
findByFirstname , findByFirstnameIs , findByFirstnameEquals
… where x.firstname = ?1
BetweenfindByStartDateBetween… where x.startDate between ?1 and ?2
LessThanfindByAgeLessThan… where x.age
LessThanEqualfindByAgeLessThanEqual… where x.age
GreaterThanfindByAgeGreaterThan… where x.age > ?1
GreaterThanEqualfindByAgeGreaterThanEqual… where x.age >= ?1
AfterfindByStartDateAfter… where x.startDate > ?1
BeforefindByStartDateBefore… where x.startDate
IsNull ,? Null
findByAge(Is)Null… where x.age is null
IsNotNull ,? NotNull
findByAge(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 ages)… where x.age in ?1
NotInfindByAgeNotIn(Collection ages)… where x.age not in ?1
TruefindByActiveTrue()… where x.active = true
FalsefindByActiveFalse()… where x.active = false
IgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstame) = UPPER(?1)
In和NotIn等对集合进行计算关键的字方法,支持使用Collection子类,或者数组作为参数的形参。支持对象属性导航
class User {
// User对象关联了一个Address 对象
Address address;
}
class Address {
String name;
}
public interface UserRepositroy extends JpaRepository
分页和排序
Sort / Pageable 对象,即可自动的完成排序/分页
Sort和Pageable是JPA定义用来排序和分页的对象也可以通过 First/Top 方法名限制结果集
findFirst10ByName(String name);
findTop10ByName(String name);
name 属性检索前10条记录官方文档
https://docs.spring.io/spring-data/jpa/docs/current-SNAPSHOT/reference/html/#jpa.query-methods