jparepository查询所有_JPA – JpaRepository 中使用的查询方法
默認方法
User user=new User();
userRepository.findAll();
userRepository.findOne(1l);
userRepository.save(user);
userRepository.delete(user);
userRepository.count();
userRepository.exists(1l);
自定義查詢
User findByUserName(String userName);
也使用一些加一些關鍵字And、?Or
User findByUserNameOrEmail(String username, String email);
修改、刪除、統計也是類似語法
Long deleteById(Long id);
Long countByUserName(String userName)
基本上 SQL 體系中的關鍵詞都可以使用,例如:LIKE、?IgnoreCase、?OrderBy。
ListfindByEmailLike(String email);
User findByUserNameIgnoreCase(String userName);
ListfindByUserNameOrderByEmailDesc(String email);
Query 自定義查詢
@Query 書寫時,數據庫名和字段名要寫成代碼中的大小寫格式,而不是數據庫中的字段格式
使用 nativeQuery ,數據庫名和字段名要寫成數據庫中的字段大小寫
一、本地語法直接查詢(Native SQL Query)
@Query(value = "select * from Book b where b.name=?1", nativeQuery = true)
List findByName(String name);
二、模糊查詢
方法1:使用 “%”
Repository
List findByNameLike(String name);
Controller
teamRepository.findByNameLike("%"+name+"%");
方法2:使用 Query 自定義 sql 查詢
Repository
@Query(value = "select t from Team t where t.name like %?1%")
List findByNameLike(String name);
Controller
teamRepository.findByNameLike(name);
三、范圍查詢
@Query(value = "select name,author,price from Book b where b.price>?1 and b.price2")
List findByPriceRange(long price1, long price2);
四、@Param注解注入參數
@Query(value = "select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price")
List findByNamedParam(@Param("name") String name, @Param("author") String author, @Param("price") long price);
五、分頁查詢
Page findALL(Pageable pageable);
Page findByUserName(String userName,Pageable pageable);
在查詢的方法中,需要傳入參數Pageable?,當查詢中有多個參數的時候Pageable建議做為最后一個參數傳入 。
Pageable?是 Spring 封裝的分頁實現類,使用的時候需要傳入頁數、每頁條數和排序規則。
int page=1,size=10; Sort sort = new Sort(Direction.DESC, "id"); Pageable pageable = new PageRequest(page, size, sort); userRepository.findALL(pageable); userRepository.findByUserName("testName", pageable);
六、限制查詢
查詢前N個元素
User findFirstByOrderByLastnameAsc();
User findTopByOrderByAgeDesc();
Page queryFirst10ByLastname(String lastname, Pageable pageable);
List findFirst10ByLastname(String lastname, Sort sort);
List findTop10ByLastname(String lastname, Pageable pageable);
七、多表查詢
@Query("select h.city as city, h.name as name, avg(r.rating) as averageRating " - "from Hotel h left outer join h.reviews r where h.city = ?1 group by h")
Page findByCity(City city, Pageable pageable);
@Query("select h.name as name, avg(r.rating) as averageRating " - "from Hotel h left outer join h.reviews r group by h")
Page findByCity(Pageable pageable);
基礎語法
KeywordSampleJPQL snippetAndfindByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2
OrfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
Is,?EqualsfindByFirstname,findByFirstnameIs,findByFirstnameEquals… where x.firstname = ?1
BetweenfindByStartDateBetween… where x.startDate between ?1 and ?2
LessThanfindByAgeLessThan… where x.age < ?1
LessThanEqualfindByAgeLessThanEqual… where x.age <= ?1
GreaterThanfindByAgeGreaterThan… where x.age > ?1
GreaterThanEqualfindByAgeGreaterThanEqual… where x.age >= ?1
AfterfindByStartDateAfter… where x.startDate > ?1
BeforefindByStartDateBefore… where x.startDate < ?1
IsNull,?NullfindByAge(Is)Null… where x.age is null
IsNotNull,?NotNullfindByAge(Is)NotNull… where x.age not null
LikefindByFirstnameLike… where x.firstname like ?1
NotLikefindByFirstnameNotLike… where x.firstname not like ?1
StartingWithfindByFirstnameStartingWith… where x.firstname like ?1?(parameter bound with appended?%)
EndingWithfindByFirstnameEndingWith… where x.firstname like ?1?(parameter bound with prepended?%)
ContainingfindByFirstnameContaining… where x.firstname like ?1?(parameter bound wrapped in?%)
OrderByfindByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname desc
NotfindByLastnameNot… where x.lastname <> ?1
InfindByAgeIn(Collection ages)… where x.age in ?1
NotInfindByAgeNotIn(Collection ages)… where x.age not in ?1
TruefindByActiveTrue()… where x.active = true
FalsefindByActiveFalse()… where x.active = false
IgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstame) = UPPER(?1)
參考:
總結
以上是生活随笔為你收集整理的jparepository查询所有_JPA – JpaRepository 中使用的查询方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 5617 Jam's maze
- 下一篇: ajaxSubmit、ajaxSubmi