jparepository查询所有_JPA – JpaRepository 中使用的查询方法
默認(rèn)方法
User user=new User();
userRepository.findAll();
userRepository.findOne(1l);
userRepository.save(user);
userRepository.delete(user);
userRepository.count();
userRepository.exists(1l);
自定義查詢
User findByUserName(String userName);
也使用一些加一些關(guān)鍵字And、?Or
User findByUserNameOrEmail(String username, String email);
修改、刪除、統(tǒng)計(jì)也是類似語(yǔ)法
Long deleteById(Long id);
Long countByUserName(String userName)
基本上 SQL 體系中的關(guān)鍵詞都可以使用,例如:LIKE、?IgnoreCase、?OrderBy。
ListfindByEmailLike(String email);
User findByUserNameIgnoreCase(String userName);
ListfindByUserNameOrderByEmailDesc(String email);
Query 自定義查詢
@Query 書寫時(shí),數(shù)據(jù)庫(kù)名和字段名要寫成代碼中的大小寫格式,而不是數(shù)據(jù)庫(kù)中的字段格式
使用 nativeQuery ,數(shù)據(jù)庫(kù)名和字段名要寫成數(shù)據(jù)庫(kù)中的字段大小寫
一、本地語(yǔ)法直接查詢(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注解注入?yún)?shù)
@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);
五、分頁(yè)查詢
Page findALL(Pageable pageable);
Page findByUserName(String userName,Pageable pageable);
在查詢的方法中,需要傳入?yún)?shù)Pageable?,當(dāng)查詢中有多個(gè)參數(shù)的時(shí)候Pageable建議做為最后一個(gè)參數(shù)傳入 。
Pageable?是 Spring 封裝的分頁(yè)實(shí)現(xiàn)類,使用的時(shí)候需要傳入頁(yè)數(shù)、每頁(yè)條數(shù)和排序規(guī)則。
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個(gè)元素
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);
基礎(chǔ)語(yǔ)法
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)
參考:
總結(jié)
以上是生活随笔為你收集整理的jparepository查询所有_JPA – JpaRepository 中使用的查询方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: HDU 5617 Jam's maze
- 下一篇: ajaxSubmit、ajaxSubmi