【Spring Data JPA】JPA 常用查询函数

119 阅读1分钟

@[TOC]


前言

函数查询的表格参考了官网的 2.7.3 版本的文档,JPA 的这种函数式查询方法改动不大,如果想知道更多的复杂查询,可以参考这篇文章 【Spring Data JPA】基于 JpaRepository 增删改查

官方文档地址 Spring Data JPA 2.7.3官方文档


函数查询表格

关键字关键字意思函数写法SQL 写法
Distinctdistinct 去重findDistinctByLastnameAndFirstnameselect distinct …​ where x.lastname = ?1 and x.firstname = ?2
Andand 关联findByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2
Oror 或者findByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
Isis 是(与Equals一样)findByFirstname
findByFirstnameIs
… where x.firstname = ?1
Equals等于findByFirstname
findByFirstnameEquals
… where x.firstname = ?1
Betweenbetween 之间findByStartDateBetween… where x.startDate between ?1 and ?2
LessThanlessThan 小于(<)findByAgeLessThan… where x.age < ?1
LessThanEquallessThanEqual 小于等于(<=)findByAgeLessThanEqual… where x.age <= ?1
GreaterThangreaterThan 大于(>)findByAgeGreaterThan… where x.age > ?1
GreaterThanEqualgreaterThan 大于等于(>=)findByAgeGreaterThanEqual… where x.age >= ?1
After大于(一般用在时间)findByStartDateAfter… where x.startDate > ?1
Before小于(一般用在时间)findByStartDateBefore… where x.startDate < ?1
IsNull是 nullfindByAgeIsNull… where x.age is null
Null是 nullfindByAgeNull… where x.age is null
IsNotNull不是 nullfindByAgeIsNotNull… where x.age not null
NotNull不是 nullfindByAgeNotNull… where x.age not null
Like模糊查询findByFirstnameLike… where x.firstname like ?1
NotLike不模糊查询findByFirstnameNotLike… where x.firstname not like ?1
OrderBy排序findByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname desc
NotnotfindByLastnameNot… where x.lastname <> ?1
Inin 查询findByAgeIn(Collection ages)… where x.age in ?1
NotInnot in 查询findByAgeNotIn(Collection ages)… where x.age not in ?1
True是 truefindByActiveTrue()… where x.active = true
False是 falsefindByActiveFalse… where x.active = false