내일배움캠프 27일차 - Query Methods에 대하여
Spring은 Java와 아주 많이 다른 것을 느낀다.
아이고 두야!!
그냥 할 뿐이다..!
가보즈아!
Query Methods에 대하여!
1. Spring을 배우면 하나하나가 정말 어려운 언어라는 것을 느낀다.
2. 하지만 현업에 계신 분들은 어떻게 이것을 했을까....!
3. 대단함 담대함 그 자체이다.
4. 초보 개발자는 그냥 무지성 할 뿐이다.
5. Query Methods란 Spring Data JPA에서는 메서드 이름으로 SQL을 생성할 수 있는 Query Methods 기능을 제공합니다.
6. 순수 JPA보다 스프링 데이터 JPA는 알아서 메소드 이름을 분석해서 JPQL을 생성하고 실행해주므로 다음과 같이 간결하게 코드를 줄일 수 있습니다. 대신 필터 조건에 부합하지 않는 키워드는 사용할 수 없으니 주의해야 합니다.
7. Query Methods에는 여러 문법이 있습니다. 중간에 있는 문법으로 Spring에 활용하면 됩니다.
Distinct | findDistinctByLastnameAndFirstname | select distinct … where x.lastname = ?1 and x.firstname = ?2 |
And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
Is,Equals | findByFirstname, 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,Null | findByAge(Is)Null | … where x.age is null |
IsNotNull,NotNull | findByAge(Is)NotNull | … where x.age is not null |
Like | findByFirstnameLike | … where x.firstname like ?1 |
NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 |
StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1(매개변수는 추가된 로 바인딩됨 %) |
EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1(매개변수는 앞에 붙은 %) |
Containing | findByFirstnameContaining | … where x.firstname like ?1(매개변수가 로 묶임 %) |
OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc |
Not | findByLastnameNot | … where x.lastname <> ?1 |
In | findByAgeIn(Collection<Age> ages) | … where x.age in ?1 |
NotIn | findByAgeNotIn(Collection<Age> ages) | … where x.age not in ?1 |
True | findByActiveTrue() | … where x.active = true |
False | findByActiveFalse() | … where x.active = false |
IgnoreCase | findByFirstnameIgnoreCase |
8. 그만큼 활용을 하면 다양한 기능들을 더욱 가독성 좋게 만들 수 있습니다.
9. Spring에서 여러 난관이 있지만 Query Methods을 통해 더욱 편리하게 썼으면 좋겠습니다.
https://seongwon.dev/Spring-MVC/20220913-Query-Method%EB%9E%80/
[JPA] Spring Data JPA의 Query Method
1. Spring Data JPA에서 쿼리를 만드는 방법 이전 게시글에서 Spring Data JPA에 대해 알아보며 해당 모듈은 Data Access Layer의 구현을 최대한 개선하는 것을 목표로 만들어졌다고 하였다. 또한 Data JPA에서
seongwon.dev
https://ppomelo.tistory.com/155
[Spring Data JPA] Query Methods - 메소드 이름으로 쿼리 생성
스프링 데이터 JPA가 제공하는 쿼리 메소드 기능 중 메소드 이름으로 쿼리 생성에 대해 알아보자. 스프링 데이터 JPA가 제공하는 쿼리 메소드 기능 메소드 이름으로 쿼리 생성 메소드 이름으로 JPA
ppomelo.tistory.com
https://docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html
JPA Query Methods :: Spring Data JPA
By default, Spring Data JPA uses position-based parameter binding, as described in all the preceding examples. This makes query methods a little error-prone when refactoring regarding the parameter position. To solve this issue, you can use @Param annotati
docs.spring.io
<출처>
오늘의 느낀 점
1. 다시 보자
2. 그냥 걷자
3. 꾸준히 걷자
just do it