Query註解及方法限制
阿新 • • 發佈:2018-03-07
ctx ati IT select ast 實現 res pos param
repository倉庫 public interface PersonRepository extends Repository<Person,Integer>{ //根據lastName來獲取對應的Person Person getByLastName(String lastName); //WHERE lastName LIKE ?% AND id < ? List<Person> getByLastNameStartingWithAndIdLessThan(String lastName,Integer id); //WHERE lastName LIKE %? AND id<? List<Person> getByLastNameEndingWithAndIdLessThan(String lastName,Integer id); //WHERE email IN(?,?,?) OR birth <? List<Person> getByEmailInOrBirthLessThan(List<String> emails,Date birth); //WHERE a.id>? List<Person> getByAddressIdCreaterThan(Integer id); //getByAddress_IdCreaterThan(Integer id); //查詢id值最大的那個Person //使用@Query註解可以自定義JPQL語句以實現更靈活的查詢 @Query("SELECT p FROM Person p WHERE p.id=(SELECT max(p.id) FROM Person p2)") Person getMaxIdPerson(); //1.@Query註解傳遞參數的方式1,使用占位符 @Query("SELECT p FROM Person p WHERE p.lastName=?1 AND p,email =?2") List<Person> testQueryAnnotationParam1(String lastName,String email); //2.位Query註解傳遞參數的方式2:命名參數的方式 @Query("SELECT p FROM Person p WHERE p.lastName= :lastName AND p.email=:email") List<Person> testQueryAnnotationParams2(@Param("email")String email,@Param("lastName")String lastName); //SpringData 允許在占位符上添加 %%. @Query("SELECT p FROM Person p WHERE p.lastName LIKE %?1% OR p.email LIKE %?2%") List<Person> testQueryAnnotationLikeParam(String lastName, String email); //SpringData 允許在占位符上添加 %%. @Query("SELECT p FROM Person p WHERE p.lastName LIKE %:lastName% OR p.email LIKE %:email%") List<Person> testQueryAnnotationLikeParam2(@Param("email") String email, @Param("lastName") String lastName); //設置 nativeQuery=true 即可以使用原生的 SQL 查詢 @Query(value="SELECT count(id) FROM jpa_persons", nativeQuery=true) long getTotalCount(); }
|
Query註解及方法限制