Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

Query Method 활용2 본문

JPA

Query Method 활용2

Seung__ 2021. 5. 26. 21:51

앞서 Query Method에서 사용한 예시 외에도 부가적으로 제공하는 키워드가 있다.

 

and, or등 여러 logical keywork가 존재한다. 써보자!

 

1.And  /  Or


먼저 And!!!!!

email과 name을 둘다 사용해서 찾아보자! --> findByEmailAndName(String email, String name)을 만들들고 테스트!

List<User> findByEmailAndName(String email, String name);
System.out.println("find by email and name : " 
	+ userRepository.findByEmailAndName("hyun@google.com", "hyun"));

테스트 결과를 보면 where 조건에서 email과 name이 같은지 확인하는 것을 볼 수 있다.

where
        user0_.email=? 
        and user0_.name=?
find by email and name : [User(name=hyun, email=hyun@google.com, 
createdAt=2021-05-26T20:28:53.692767, updatedAt=2021-05-26T20:28:53.692767, id=5)]

 

다음엔 Or

 List<User> findByEmailOrName(String email, String name);
System.out.println("find by email or name : " 
+ userRepository.findByEmailOrName("hyun@google.com", "kim"));

실행 결과, where에서 or조건이 작동하는걸 확인했다!

where
        user0_.email=? 
        or user0_.name=?
find by email or name : [
  User(name=kim, email=kim@google.com, createdAt=2021-05-26T20:35:56.313434, 
      updatedAt=2021-05-26T20:35:56.313434, id=4),
  User(name=hyun, email=hyun@google.com, createdAt=2021-05-26T20:35:56.313434, 
      updatedAt=2021-05-26T20:35:56.313434, id=5)]

 

 

2. After / Before


After : 초과, Before : 미만

어제 이후로 생성된 entity가 어떤게 있는지 찾아보자.

 List<User> findByCreatedAtAfter(LocalDateTime yesterDay);
System.out.println("find by created at After : " 
+ userRepository.findByCreatedAtAfter(LocalDateTime.now().minusDays(1L)));

where 조건에서 created_at 이 확인된다. 날짜도 보면 다 어제 ?(오늘은 26일..)이후로 생성되었다.

where
        user0_.created_at>?
find by created at After : [User(name=hyun, email=hyun@naver.com, createdAt=2021-05-26T20:41:37.356083,
		updatedAt=2021-05-26T20:41:37.356083, id=1), 
     User(name=park, email=park@google.com, createdAt=2021-05-26T20:41:37.372078,
     	updatedAt=2021-05-26T20:41:37.372078, id=2), 
     User(name=lee, email=lee@naver.com, createdAt=2021-05-26T20:41:37.372078, 
     	updatedAt=2021-05-26T20:41:37.372078, id=3), 
     User(name=kim, email=kim@google.com, createdAt=2021-05-26T20:41:37.372078, 
     	updatedAt=2021-05-26T20:41:37.372078, id=4),
     User(name=hyun, email=hyun@google.com, createdAt=2021-05-26T20:41:37.372078, 
     	updatedAt=2021-05-26T20:41:37.372078, id=5)]

 흠 그러면 before로 해보자. 다 안나오겠지?

List<User> findByCreatedAtBefore(LocalDateTime yesterDay);
System.out.println("find by created at Before : " 
+ userRepository.findByCreatedAtBefore(LocalDateTime.now().minusDays(1L)));

하나도 없다!!! 역시

 where
        user0_.created_at<?
find by created at Before : []

 

이번엔 id가 3을 넘는 entity를 찾아보자 -->findByIdAfter(long)

List<User> findByIdAfter(Long id);
System.out.println("find by id after : " + userRepository.findByIdAfter(3L));

id가 4, 5인 entity만 찾았다!

  where
        user0_.id>?
find by id after : [User(name=kim, email=kim@google.com, createdAt=2021-05-26T21:25:00.066437, 
		updatedAt=2021-05-26T21:25:00.066437, id=4), 
     User(name=hyun, email=hyun@google.com, createdAt=2021-05-26T21:25:00.066437, 
     	updatedAt=2021-05-26T21:25:00.066437, id=5)]

 

3. greater / less than


greaterThan : 이상, lessThan : 이하 

created At 이 어제 보다 큰 경우만 찾아보자.

greater / less than은 before/after보다 좀 더 범용적으로 사용하기 좋다.

List<User> findByCreatedAtGreaterThan(LocalDateTime yesterday);
System.out.println("find by created at Greater than  : " 
+ userRepository.findByCreatedAtGreaterThan(LocalDateTime.now().minusDays(1L)));

5/26 이후에 생성된 enitty가 모두 조회되었당.

 

4.between


Between : A이상 B이하

 

어제부터~ 내일 사이에 생성된 entity, id가 3~4 사이인 entity를 조회하자.

  List<User> findByCreatedAtBetween(LocalDateTime yesterday, LocalDateTime tomorrow);
  List<User> findByIdBetween(Long id1, Long id2);
System.out.println("find by created at Between : "
	+ userRepository.findByCreatedAtBetween(LocalDateTime.now().minusDays(1L)
    			    	              , LocalDateTime.now().plusDays(1L)));
System.out.println("find by id at Between : " + userRepository.findByIdBetween(2L, 4L));

created At 어제~ 내일

id가 3이상 4이하인 entity가 조회되었다.

between id 3 & 4

'JPA' 카테고리의 다른 글

Query Method 3 : 정렬  (0) 2021.05.27
Query Method 3  (0) 2021.05.27
Query Method 활용1  (0) 2021.05.26
SimpleJpaRepository분석  (0) 2021.05.24
Page , Query By Example(QBE)  (0) 2021.05.23
Comments