Spring_JPA_페이지네이션

2022. 12. 28. 17:01Spring

728x90

1. DTO

import lombok.*;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PagenationDto {
    private int start;
    private int num;
}

2. Controller

List<GameMapper> gameMapper = userGameService.myGameInfo(pagenationDto.getStart(),pagenationDto.getNum(),userEmail);

3. Service

public List<GameMapper> myGameInfo(int start, int num, String userEmail){
        return gameRepository.getUserGames(PageRequest.of(start, num), userEmail);
    }

4. Repository

public interface GameRepository extends JpaRepository<Game, String> {
    @Query(value = "select new com.ssafy.backend.entity.Game(g.gameId, g.gameYear, g.gameMonth, g.gameDay, g.gameTime, g.gameScore) from Game g " +
            "left join UserGame ug " +
            "on g.gameId = ug.game.gameId " +
            "left join User u " +
            "on u.userId = ug.user.userId " +
            "where u.userEmail = :userEmail " +
            "order by g.gameYear desc , g.gameMonth desc , g.gameDay desc , g.gameTime desc ")
    List<GameMapper> getUserGames(Pageable pageable, @Param(value="userEmail") String userEmail);
}

 

start : 페이지 번호

num : 표시할 항목 수

'Spring' 카테고리의 다른 글

Spring_MySQL 연결_gradle  (0) 2023.03.22
Spring_tinyLog  (0) 2023.03.09
Spring_env 파일 설정  (0) 2022.12.28
Spring_네이버 메일 인증  (0) 2022.12.28
Spring_stomp  (0) 2022.12.28