Programing/JPA

JPA Query Methods 정리

리커니 2023. 4. 13.
반응형

오늘은 JPA에서 간단하게 우리가 원하는 데이터를 조회 할 수 있는 방법중에 하나인 Query Methods 에 대해서 알아보도록 하겠습니다.

 

Keyword Usage JPQL 
Where and findByColumne1AndColumn2(col1, col2); select ... where column1 = col1 and column2 = col2
Where or findByColumn1OrColumn2(col1, col2); select ... where column1 = col1 or column2 = col2
Distinct findDistinctByColumn1(col1); select distinct ... where column1 = col1
Is, Equals findByColumn1Is(col1);
findByColumn1Equals(col1);
select ... where column1 = col1
LessThan findByNumColumnLessThan(col1); select ... where numColumn < col1
LessThanEqual findByNumColumnLessThanEqual(col1); select ... where numColumn <= col1
GreaterThan findByNumColumnGreaterThan(col1); select ... where numColumn > col1
GreaterThanEqual findByNumCOlumnGreaterThanEqual(col1); select ... where numColumn >= col1
Between fidnByDateColumnBetween(col1, col2); select ... where dateColumn between col1 and col2
After findByDateColumnAftter(col1); select ... where dateColumn > col1
Before findByDateColumnBefore(col1); select ... where dateColumn < col1
IsNull, Null findByColumn(Is)Null(); select ... where column is null
IsNotNull, NotNull findByColumn(Is)NotNull(); select ... where column is not null
Like findByColumnLike(col1); select ... where column like col1
NotLike findByColumnNotLike(col1); select ... where column not like col1
StartingWith findByColumnStartingWith(col1); select ... where column col1|'%'
EndingWith findByColumnEndingWith(col1); select ... where column '%'|col1
Containing findByColumnContaining(col1); select ... where column '%'|col1|'%'
Not findByColumnNot(col1); select ... where column <> col1
In findByColumnIn(collectionCol); select ... where column in (collectionCol)
NotIn findByColumnNotIn(collectionCol); select ... where column not in (collectionCol)
Order by findByColumnOrderByDesc(col1);
findByColumnOrderByAsc(col1);
select ... order by col1 desc
select ... order by col1 asc
True findByColumnTrue(); select ... where column = true
False findByColumnFalse(); select ... where column  = false
IgnoreCase findByColumnIgnoreCase(col1); select ... UPPER(column) = UPPER(col1)

복잡할 수록 메소드명이 길어지긴 하기만 간편하게 원하는 데이터를 조회할 수 있는 방법이니 기본적인 사용법은 익혀놓고 가도록 합시다.

 

참고 : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation

 

Spring Data JPA - Reference Documentation

Example 119. Using @Transactional at query methods @Transactional(readOnly = true) interface UserRepository extends JpaRepository { List findByLastname(String lastname); @Modifying @Transactional @Query("delete from User u where u.active = false") void del

docs.spring.io

 

반응형

댓글

💲 추천 글