| 항목 | 예제 | 설명 |
|---|---|---|
| And | findBySubjectAndContent(String subject, String content) | 여러 컬럼을 and로 검색 |
| Or | findBySubjectOrContent(String subject, String content) | 여러 컬럼을 or 로 검색 |
| Between | findByCreateDateBetween(LocalDateTime fromDate, LocalDateTime toDate) | 컬럼을 between으로 검색 |
| LessThan | findByIdLessThan(Integer id) | 작은 항목 검색 |
| GreaterThanEqual | findByIdGraterThanEqual(Integer id) | 크거나 같은 항목 검색 |
| Like | findBySubjectLike(String subject) | like 검색 |
| In | findBySubjectIn(String[] subjects) | 여러 값중에 하나인 항목 검색 |
| OrderBy | findBySubjectOrderByCreateDateAsc(String subject) | 검색 결과를 정렬하여 전달 |
package com.mysite.sbb;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SbbApplicationTests {
@Autowired
private QuestionRepository questionRepository;
@Test
void testJpa() {
Question q = this.questionRepository.findBySubjectAndContent(
"sbb가 무엇인가요?", "sbb에 대해서 알고 싶습니다.");
assertEquals(1, q.getId());
}
}
select
question0_.id as id1_1_,
question0_.content as content2_1_,
question0_.create_date as create_d3_1_,
question0_.subject as subject4_1_
from
question question0_
where
question0_.subject=?
and question0_.content=?
*결과가 여러개인 경우 리턴 타입이 List가 되어야 함.
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SbbApplicationTests {
@Autowired
private QuestionRepository questionRepository;
@Test
void testJpa() {
List<Question> qList = this.questionRepository.findBySubjectLike("sbb%");
Question q = qList.get(0);
assertEquals("sbb가 무엇인가요?", q.getSubject());
}
}
sbb%: "sbb"로 시작하는 문자열
%sbb: "sbb"로 끝나는 문자열
%sbb%: "sbb"를 포함하는 문자열