@[TOC]
前言
函数查询的表格参考了官网的 2.7.3 版本的文档,JPA 的这种函数式查询方法改动不大,如果想知道更多的复杂查询,可以参考这篇文章 【Spring Data JPA】基于 JpaRepository 增删改查
官方文档地址 Spring Data JPA 2.7.3官方文档
函数查询表格
| 关键字 | 关键字意思 | 函数写法 | SQL 写法 |
|---|---|---|---|
| Distinct | distinct 去重 | findDistinctByLastnameAndFirstname | select distinct … where x.lastname = ?1 and x.firstname = ?2 |
| And | and 关联 | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
| Or | or 或者 | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
| Is | is 是(与Equals一样) | findByFirstname findByFirstnameIs | … where x.firstname = ?1 |
| Equals | 等于 | findByFirstname findByFirstnameEquals | … where x.firstname = ?1 |
| Between | between 之间 | findByStartDateBetween | … where x.startDate between ?1 and ?2 |
| LessThan | lessThan 小于(<) | findByAgeLessThan | … where x.age < ?1 |
| LessThanEqual | lessThanEqual 小于等于(<=) | findByAgeLessThanEqual | … where x.age <= ?1 |
| GreaterThan | greaterThan 大于(>) | findByAgeGreaterThan | … where x.age > ?1 |
| GreaterThanEqual | greaterThan 大于等于(>=) | findByAgeGreaterThanEqual | … where x.age >= ?1 |
| After | 大于(一般用在时间) | findByStartDateAfter | … where x.startDate > ?1 |
| Before | 小于(一般用在时间) | findByStartDateBefore | … where x.startDate < ?1 |
| IsNull | 是 null | findByAgeIsNull | … where x.age is null |
| Null | 是 null | findByAgeNull | … where x.age is null |
| IsNotNull | 不是 null | findByAgeIsNotNull | … where x.age not null |
| NotNull | 不是 null | findByAgeNotNull | … 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 |
| Not | not | findByLastnameNot | … where x.lastname <> ?1 |
| In | in 查询 | findByAgeIn(Collection ages) | … where x.age in ?1 |
| NotIn | not in 查询 | findByAgeNotIn(Collection ages) | … where x.age not in ?1 |
| True | 是 true | findByActiveTrue() | … where x.active = true |
| False | 是 false | findByActiveFalse | … where x.active = false |