강의

멘토링

커뮤니티

Inflearn Community Q&A

theking149124's profile image
theking149124

asked

[Beginner] Let's try it! Calling API with Kotlin and Spring

4.10. Add search term ranking

findTop10ByOrderByCntDesc 함수의 동작 원리에 대한 질문

Written on

·

302

0

interface WordRepository :CrudRepository<WordCount, String>
{
    fun findTop10ByOrderByCntDesc() :List<WordCount>
}

위 findTop10ByOrderByCntDesc 함수는 선언만 되고 실제 구현이 안되어서 CrudRepository 에도 들어가봤지만 virtual 함수도 아닌걸로 보이는데요..

예상하는 바는 repository에서 함수명을 query로 자동 변환하여 가져오는 형식으로 느껴집니다.

만약 그렇다면, 이 것은 원래 springframework 기능인 것인가요? (완전 웹개발 초보라서 이렇게 문의드립니다...)

springkotlinspring-boot

Answer 1

1

kimkimgod님의 프로필 이미지
kimkimgod
Instructor

안녕하세요.

김정훈님 질문 남겨주셔서 감사합니다.

 

질문하신 함수(메서드)들을 Query Methods 라고 합니다.

이것은 Spring Data JPA에 포함되어 있는 기능인데요.

 

Repository interface에 메소드 명으로 query를 자동으로 생성해줍니다.

fun findTop10ByOrderByCntDesc() :List<WordCount>

이렇게 되어있는 메소드 명은 아래의 SQL 만들어서 호출 시 사용하게 됩니다.

select
    w1_0.word,
    w1_0.cnt 
from
    wordcount w1_0 
order by
    w1_0.cnt desc limit ?

 

Query Methods도 JPA에서 좀 더 쉽게 Query를 사용하기 위한 방법이여서 작성 규칙이 있습니다.

아래의 레퍼런스 확인하시면 작성법 학습 하실 수 있습니다.

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

 

보시고 더 궁금하신 내용 있으시면 질문 남겨주세요 :)

theking149124님의 프로필 이미지
theking149124
Questioner

문서 링크까지 친절하게 답변해주셔서 감사합니다!

theking149124's profile image
theking149124

asked

Ask a question