Engineering Note

[스프링 부트 입문] 12 데이터 목록 보기 본문

Server/Spring

[스프링 부트 입문] 12 데이터 목록 보기

Software Engineer Kim 2022. 7. 12. 21:22

본 내용은 홍팍 스프링부트 강좌를 참고하였습니다.

https://www.youtube.com/watch?v=rxRcIxU_d4I&list=PLyebPLlVYXCiYdYaWRKgCqvnCFrLEANXt&index=13

지난번까지는 article/{id}를 통해 특정 게시글을 조회하는 controller를 개발했는데 오늘은 DB속 전체 게시글 목록을 조회하는 코드를 개발해보자

Mission

  • DB속 모든 게시글 목록을 조회하시오.

Repository interface를 사용해 data를 가져온다.

DB에서 데이터를 조회하기 위해서는 Repository 객체를 이용한다.

컨트롤러의 동작을 세분화 하면 다음과 같다.

  1. CrudRepository를 상속받은 Repository 객체로 JPA를 통해 데이터를 가져온다.
  2. 가져온 Article 묶음을 뷰로 전달한다.(뷰로 전달 할때는 mocel객체를 사용)
  3. 뷰 페이지를 설정해준다.
@GetMapping("/articles")
    public String index(Model model){
        // 1. 모든 Article을 가져온다!
        List<Article> articleEntityList = articleRepository.findAll();

        // 2: 가져온 Article 묶음을 뷰로 전달!
        model.addAttribute("articleList",articleEntityList);

        // 3. 뷰 페이지 설정!
        return "articles/index";
    }
Comments