본문 바로가기
웹공뷰/스프링

[JPA] MSSQL 최근 날짜로 정렬하여 아이템 3개 조회하기

by 이노키_ 2022. 12. 24.

만일 이 구문을 mssql 로 직접 쿼리를 날린다면

select TOP 3 * from kogas.dbo.tb_pcm_orders order by creation_date DESC;

 

이 구문을 JPA로 한다면?

public interface UserRepository extends JpaRepository<User, Long> {
    public List<User> findTop3ByIndustryOrderByCreationDateDesc(Industry industry);
}

findTop3ByIndustryOrderByCreationDateDesc

Top3 : 몇개 가져올지. 10개라면 Top10, 5개라면 Top5

ByIndustry : User 리스트를 가져올 때의 조건. 

OrderBy : 어떤 조건이냐면

CreationDate : 생성 날짜 기준으로

Desc : 내림차순으로 

 

 

댓글