TIL/Web Back
[Sparta] 내일배움캠프 TIL 27일차
헤르로우워르드
2024. 5. 29. 11:07
숙련주차 개인과제
1. 쿼리 메서드 findAll 정렬하는법
scheduleRepository.findAll().stream().map(ScheduleResponseDto::new).sorted(Comparator.comparing(ScheduleResponseDto::getCreateAt).reversed()).toList();
// 기존코드, 많은 리소스 사용으로 좋지 않은 코드기때문에 db에서 정렬후 가져오는 방식으로 수정
1. findAllOrderBycreatedAtDesc // 오류, By엔티티 형식으로 먼저하지않고 바로 OrderBy x
2. findAllByOrderBycreatedAtDesc // 오류, 엔티티명으로 줄때 앞은 대문자
3. findAllByOrderBytimeAtDesc // 오류, 클래스로 가서 column(name = "time") 으로 했지만 이건 영향 x
최종 : scheduleRepository.findAllByOrderByCreatedAtDesc().stream().map(ScheduleResponseDto::new).toList();