Spring Data JPAは属性名に基づいて検索します.

2856 ワード

1つ てんせい心
Spring データ JPAはRepositoryインターフェースを定義する方法で名来でクエリーを定義することをサポートしていますが、法名はエンティティクラスの属性に基づいて名来で決定されます.
一般クエリー
属性名来に従ってクエリー方法を定義する例は以下の通りである.
public interface PersonRepo extends JpaRepository{
    //         ,   name
    List findByName(String name);
    //    like  ,   name
    List findByNameLike(String name);
    //         ,   name address
    List findByNameAndAddress(String name,String address);
}
コードから見ると、findBy、Like、Andのようなキーワードが使用されており、findByはfind、read、readBy、query、queryBy、get、getByに置き換えられている.
三番目 結果の数を制限する
その結果の数は、topとfirstのキーワードで実現され、例は以下の通りである.
public interface PersonRepo extends JpaRepository{
    //          10   
    List findFirst10ByName(String name);
    //          30   
    List findTop30ByName(String name);
}
クエリーキーリスト
公式サイトを参照:https://docs.spring.io/spring-data/jpa/docs/2.0.9.RELEASE/reference/html/
キーワード

機能はJPQLと同じです 
And
findByLastname AndFirstname
…where x.lastname=?1 and x.firstname=?2
Or
findByLastname Or Firstname
…where x.lastname=?1 or x.firstname=2
Is,Equals
findByFirstname,findByFirstnameIs,findByFirstname Equals
…where x.firstname=?1
Between
findByStartDateBetween
…where x.startDate between?1 and2
LessThan
findByAgeLessThan
…where x.age<1
LessThanEqual
findByAgeLessThanEqual
…where x.age<=1
グレア・ティーン
findByAgeGreterThan
…where x.age>1
Greter ThanEqual
findByAgeGreter ThanEqual
…where x.age>=1
After
findByStartDateAfter
…where x.startDate>?1
Before
findByStartDateBefore
…where x.startDateIsNull
findByAgeIsNull
…where x.age is null
Is NotNull,NotNull
findByAge(Is)NotNull
…where x.age not null
Like
findBy FirstnameLike
…where x.firstname like?1
Not Like
findBy Firstname NotLike
…where x.firstname not like?1
スターリングWith
findBy Firstnament With
…where x.firstname like?1(parameter bound with apped%)
Ending With
findBy Firstnamen With
…where x.firstname like?1(parameter bound with prepended%)
コンタニング
findBy FirstnameContining
…where x.firstname like?1(parameter bound wrapped in%)
OrderBy
findByAgeOrderByLastnameDesc
…where x.age=1 order by x.lastname desc
Not
findByLastname Not
…where x.lastname<>?1
In
findByAgeIn(Collection agems)
…where x.age in1
NotIn
findByAgeNotIn(Collection agems)
…where x.age not in?1
True
findByActive True()
…where x.active=true
False
findByActive False()
…where x.active=false
IgnoreCase
findBy FirstnamegnoreCase
…where UPER(x.firstame)=UPER(?1)