1.JpaRepository支持接口规范方法名查询。意思是只要在接口中定义的查询方法符合规范,就不用写方法是如何实现的。
目前常用的关键词如下:
Keyword | Sample | JPQL snippet |
---|---|---|
NotNull | findByAgeNotNull | … where x.age not null |
Like | findByNameLike | … where x.name like ? |
NotLike | findByNameNotLike | … where x.name not like ? |
OrderBy | findByNameOrderByAge | … where x.name= ? order by x.age desc |
Not | findByNameNot | … where x.name <> ? |
In | findByAgeIn | … where x.age in ? |
NotIn | findByAgeNotIn | … where x.age not in ? |
True | findByActiveTrue | … where x.avtive = true |
Flase | findByActiveFalse | … where x.active = false |
And | findByNameAndAge | … where x.name = ? and x.age = ? |
Or | findByNameOrAge | … where x.name = ? or x.age = ? |
Between | findBtAgeBetween | … where x.age between ? and ? |
LessThan | findByAgeLessThan | … where x.age < ? |
GreaterThan | findByAgeGreaterThan | … where x.age > ? |
IsNull | findByAgeIsNull | … where x.age is null |
2.JpaRepository相关查询功能
Spring Data JPA框架在进行方法名解析时,会先把方法名多余的前缀截取掉,比如find、findBy、read、readBy、get、getBy,然后对剩下部分进行解析.