1 方式一:使用Spring Data JPA中接口定義的方法進行查詢
在繼承JpaRepository,和JpaRepository接口后,我們就可以使用接口中定義的方法進行查詢
繼承JpaRepository后的方法列表

繼承JpaSpecificationExecutor的方法列表

2 方式二: 使用JPQL的方式查詢
使用Spring Data JPA提供的查詢方法已經可以解決大部分的應用場景,但是對於某些業務來說,我們還需要靈活的構造查詢條件,這時就可以使用@Query注解,結合JPQL的語句方式完成查詢
@Query 注解的使用非常簡單,只需在方法上面標注該注解,同時提供一個JPQL查詢語句即可
public interface CustomerDao extends JpaRepository<Customer, Long>,JpaSpecificationExecutor<Customer> { //@Query 使用jpql的方式查詢。 @Query(value="from Customer") public List<Customer> findAllCustomer(); //@Query 使用jpql的方式查詢。?1代表參數的占位符,其中1對應方法中的參數索引 @Query(value="from Customer where Name = ?1") public Customer findCustomer(String Name); }
此外,也可以通過使用 @Query 來執行一個更新操作,為此,我們需要在使用 @Query 的同時,用 @Modifying 來將該操作標識為修改查詢,這樣框架最終會生成一個更新的操作,而非查詢
@Query(value="update Customer set Name = ?1 where Id = ?2") @Modifying public void updateCustomer(String Name,Long Id);
3 方式三 :使用SQL語句查詢
Spring Data JPA同樣也支持sql語句的查詢,如下:
/** * nativeQuery : 使用本地sql的方式查詢 */ @Query(value="select * from cst_customer",nativeQuery=true) public void findSql();
4 方式四:方法命名規則查詢
顧名思義,方法命名規則查詢就是根據方法的名字,就能創建查詢。只需要按照Spring Data JPA提供的方法命名規則定義方法的名稱,就可以完成查詢工作。Spring Data JPA在程序執行的時
候會根據方法名稱進行解析,並自動生成查詢語句進行查詢
按照Spring Data JPA 定義的規則,查詢方法以findBy開頭,涉及條件查詢時,條件的屬性用條件關鍵字連接,要注意的是:條件屬性首字母需大寫。框架在進行方法名解析時,會先把方法名多余的前綴截取掉,然后對剩下部分進行解析。
//方法命名方式查詢(根據客戶名稱查詢客戶) public Customer findByName(String Name);
具體的關鍵字,使用方法和生產成SQL如下表所示
| Keyword |
Sample |
JPQL |
| And |
findByLastnameAndFirstname |
… where x.lastname = ?1 and x.firstname = ?2 |
| Or |
findByLastnameOrFirstname |
… where x.lastname = ?1 or x.firstname = ?2 |
| Is,Equals |
findByFirstnameIs, findByFirstnameEquals |
… where x.firstname = ?1 |
| Between |
findByStartDateBetween |
… where x.startDate between ?1 and ?2 |
| LessThan |
findByAgeLessThan |
… where x.age < ?1 |
| LessThanEqual |
findByAgeLessThanEqual |
… where x.age ⇐ ?1 |
| GreaterThan |
findByAgeGreaterThan |
… where x.age > ?1 |
| GreaterThanEqual |
findByAgeGreaterThanEqual |
… where x.age >= ?1 |
| After |
findByStartDateAfter |
… where x.startDate > ?1 |
| Before |
findByStartDateBefore |
… where x.startDate < ?1 |
| IsNull |
findByAgeIsNull |
… where x.age is null |
| IsNotNull,NotNull |
findByAge(Is)NotNull |
… where x.age not null |
| Like |
findByFirstnameLike |
… where x.firstname like ?1 |
| NotLike |
findByFirstnameNotLike |
… where x.firstname not like ?1 |
| StartingWith |
findByFirstnameStartingWith |
… where x.firstname like ?1 (parameter bound with appended %) |
| EndingWith |
findByFirstnameEndingWith |
… where x.firstname like ?1 (parameter bound with prepended %) |
| Containing |
findByFirstnameContaining |
… where x.firstname like ?1 (parameter bound wrapped in %) |
| OrderBy |
findByAgeOrderByLastnameDesc |
… where x.age = ?1 order by x.lastname desc |
| Not |
findByLastnameNot |
… where x.lastname <> ?1 |
| In |
findByAgeIn(Collection ages) |
… where x.age in ?1 |
| NotIn |
findByAgeNotIn(Collection age) |
… where x.age not in ?1 |
| TRUE |
findByActiveTrue() |
… where x.active = true |
| FALSE |
findByActiveFalse() |
… where x.active = false |
| IgnoreCase |
findByFirstnameIgnoreCase |
… where UPPER(x.firstame) = UPPER(?1) |
