스프링 부트, 입문!

스프링 부트, 입문!

쉽고 빠르게 배우는, 스프링 부트 첫걸음!

12 데이터 목록 조회(read)

# 데이터 목록 조회 ## 미션 DB 속 모든 게시글을 목록으로 조회하시오. ![홍팍-스프링-부트-입문-데이터-목록-조회-미션](http://drive.google.com/uc?export=view&id=1dwpVN4BgcU02bURX_LYJP0qnXTJ_8x6i) ## 01:18 브라우저 요청 받기 #### ../controller/ArticleController ``` ... @Controller @Slf4j public class ArticleController { ... @GetMapping("/articles") public String index() { // 1: 모든 Article을 가져온다! // 2: 가져온 Article 묶음을 뷰로 전달! // 3: 뷰 페이지를 설정! return ""; } } ``` ## 03:32 리파지터리와 타입 캐스팅 - Iterable, List, ArrayList #### ../controller/ArticleController ``` ... @Controller @Slf4j public class ArticleController { ... @GetMapping("/articles") public String index() { // 1: 모든 Article을 가져온다! List<Article> articleEntityList = articleRepository.findAll(); // 2: 가져온 Article 묶음을 뷰로 전달! // 3: 뷰 페이지를 설정! return ""; } } ``` ## 04:42 라파지터리 오버라이딩 - findAll() #### ../repository/ArticleRepository ``` package com.example.firstproject.repository; import com.example.firstproject.entity.Article; import org.springframework.data.repository.CrudRepository; import java.util.ArrayList; public interface ArticleRepository extends CrudRepository<Article, Long> { @Override ArrayList<Article> findAll(); } ``` ## 06:09 모델과 데이터 등록 - addAttribute() #### ../controller/ArticleController ``` ... @Controller @Slf4j public class ArticleController { ... @GetMapping("/articles") public String index(Model model) { // 1: 모든 Article을 가져온다! List<Article> articleEntityList = articleRepository.findAll(); // 2: 가져온 Article 묶음을 뷰로 전달! model.addAttribute("articleList", articleEntityList); // 3: 뷰 페이지를 설정! return "articles/index"; } } ``` ## 06:49 뷰 페이지 연결 및 작성 #### ../articles/index.mustache ``` {{>layouts/header}} <table class="table"> <thead> <tr> <th scope="col">ID</th> <th scope="col">Title</th> <th scope="col">Content</th> </tr> </thead> <tbody> {{#articleList}} <tr> <th>{{id}}</th> <td>{{title}}</td> <td>{{content}}</td> </tr> {{/articleList}} </tbody> </table> {{>layouts/footer}} ``` ## 🔥 구글링 훈련하기 - Java Iterable - Java List Arraylist 차이 - mustache 반복문