페이징 처리(1)

이시원·2022년 10월 11일
post-thumbnail


page : 0(1페이지)부터 시작
size : 몇 개 보여줄 건지


  • controller > BoardController.java

package com.study.board.controller;

import com.study.board.entity.Board;
import com.study.board.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class BoardController {

    @Autowired
    private BoardService boardService;

    @GetMapping("/board/list")
    public String boardList(Model model, @PageableDefault(page = 0, size = 10, sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {
        // "list"라는 이름으로 boardService에 있는 boardList를 담아서 넘기겠다.
        model.addAttribute("list", boardService.boardList(pageable));
        return "boardlist";
    }
}

@PageableDefault(page = 0, size = 10, sort = "id", direction = Sort.Direction.DESC)
: 디폴트 값 -> 0페이지, 10개 보여줌, id로 분류, 최신 게시물부터 보여줌

  • service > BoardService.java

package com.study.board.service;

import com.study.board.entity.Board;
import com.study.board.repository.BoardRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Service
public class BoardService {

    @Autowired
    private BoardRepository boardRepository;

    // 게시글 리스트 처리
    public Page<Board> boardList(Pageable pageable) {
        return boardRepository.findAll(pageable);
    }
}


profile
코딩공부

0개의 댓글