질문 1 : 고객 테이블에서 2019년 이후 등록한 여성 고객을 검색한다. (2019년 이후 등록 고객은 고객코드가 '2019000'보다 큰 고객이다.)
1) MySQL
select *
from customer c
where c.customer_code > '2019000'
and c.flag = 'W';
2) JPA
- 숫자 비교가 아니라 문자열에 대한 비교이기 때문에 GreaterThan 이 아니라 After를 사용해야 한다.
public interface CustomerRepository extends JpaRepository<Customer, Long> {
List<Customer> findByCustomerCodeAfterAndFlag(String customerCode, String flag);
}
@Test
@DisplayName("AND 테스트")
public void andTest(){
List<Customer> customers = customerRepository.findByCustomerCodeAfterAndFlag("2019000", "W");
customers.forEach(System.out::println);
}
Customer(customerCode=2019001, customerName=김진숙, flag=W, birthDay=20010426, phoneNumber=002-9842-0074, email=jskim$dreami.org, totalPoint=12820, regDate=20190101080518)
Customer(customerCode=2019281, customerName=이아름, flag=W, birthDay=19940513, phoneNumber=003-2620-0723, email=aulee@hoki.com, totalPoint=35600, regDate=20190709201308)
질문 2 : 고객 테이블에서 남성이면서 출생 연도가 1990년 미만인 고객을 검색한다.
1) MySQL
select *
from customer c
where c.birth_day < '1990101'
and c.flag = 'M';
2) JPA
- 숫자 비교가 아니라 문자열에 대한 비교이기 때문에 LessThen 이 아니라 Before를 사용해야 한다.
public interface CustomerRepository extends JpaRepository<Customer, Long> {
List<Customer> findByBirthDayBeforeAndFlag(String birthDay, String flag);
}
@Test
@DisplayName("AND2 테스트")
public void andTest2(){
List<Customer> customers = customerRepository.findByBirthDayBeforeAndFlag("1990101", "M");
customers.forEach(System.out::println);
}
Customer(customerCode=2017042, customerName=강원진, flag=M, birthDay=19810603, phoneNumber=002-8202-8790, email=wjgang@navi.com, totalPoint=280300, regDate=20170123132432)
Customer(customerCode=2017108, customerName=박승대, flag=M, birthDay=19711430, phoneNumber=, email=sdpark@haso.com, totalPoint=23450, regDate=20170508203450)
Customer(customerCode=2019095, customerName=남궁소망, flag=M, birthDay=19620728, phoneNumber=003-6273-8539, email=, totalPoint=890, regDate=20190312124558)
Customer(customerCode=2019167, customerName=한찬희, flag=M, birthDay=19711106, phoneNumber=002=1202=5563, email=chhan@ecom.co.kr, totalPoint=6800, regDate=20190508155600)