스프링 부트, 입문!

스프링 부트, 입문!

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

08 데이터 생성 with JPA

# 데이터 생성 with JPA ## 미션 JPA를 활용하여 폼 데이터를 DB로 저장하시오. ![홍팍-스프링-부트-입문-JPA-데이터-생성-미션](http://drive.google.com/thumbnail?export=view&sz=w960&id=1dqtkcA-yGU-Dy6RsXNtoYjUOAkfS3QSl) ## 03:49 DTO를 엔티티로 변환하기 #### ../controller/ArticleController ``` ... @Controller public class ArticleController { ... @PostMapping("/articles/create") public String createArticle(ArticleForm form) { System.out.println(form.toString()); // 1. Dto를 Entity 변환 Article article = form.toEntity(); // 2. Repository에게 Entity를 DB로 저장하게 함 return ""; } } ``` ## 04:17 엔티티 작성하기 #### ../entity/Article ``` package com.example.firstproject.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Article { @Id @GeneratedValue private Long id; @Column private String title; @Column private String content; public Article(Long id, String title, String content) { this.id = id; this.title = title; this.content = content; } @Override public String toString() { return "Article{" + "id=" + id + ", title='" + title + '\'' + ", content='" + content + '\'' + '}'; } } ``` ## 07:17 변환 메소드 추가 #### ../dto/ArticleForm ``` package com.example.firstproject.dto; import com.example.firstproject.entity.Article; public class ArticleForm { ... public Article toEntity() { return new Article(null, title, content); } } ``` ## 08:32 리파지터리를 통한 엔티티 저장하기 #### ../controller/ArticleController ``` package com.example.firstproject.controller; import com.example.firstproject.dto.ArticleForm; import com.example.firstproject.entity.Article; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @Controller public class ArticleController { private ArticleRepository articleRepository; ... @PostMapping("/articles/create") public String createArticle(ArticleForm form) { System.out.println(form.toString()); // 1. Dto를 Entity 변환 Article article = form.toEntity(); // 2. Repository에게 Entity를 DB로 저장하게 함 Article saved = articleRepository.save(article); return ""; } } ``` ## 09:30 리파지터리 작성 #### ../repository/ArticleRepository ``` package com.example.firstproject.repository; import com.example.firstproject.entity.Article; import org.springframework.data.repository.CrudRepository; public interface ArticleRepository extends CrudRepository<Article, Long> { } ``` ## 13:42 의존성 주입하기(DI) #### ../controller/ArticleController ``` package com.example.firstproject.controller; import com.example.firstproject.dto.ArticleForm; import com.example.firstproject.entity.Article; import com.example.firstproject.repository.ArticleRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @Controller public class ArticleController { @Autowired // 스프링 부트가 미리 생성해놓은 리파지터리 객체를 가져옴(DI) private ArticleRepository articleRepository; ... @PostMapping("/articles/create") public String createArticle(ArticleForm form) { System.out.println(form.toString()); // 1. Dto를 Entity 변환 Article article = form.toEntity(); // 2. Repository에게 Entity를 DB로 저장하게 함 Article saved = articleRepository.save(article); return ""; } } ``` ## 15:14 데이터 저장 흐름 확인 #### ../controller/ArticleController ``` package com.example.firstproject.controller; import com.example.firstproject.dto.ArticleForm; import com.example.firstproject.entity.Article; import com.example.firstproject.repository.ArticleRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @Controller public class ArticleController { @Autowired private ArticleRepository articleRepository; @GetMapping("/articles/new") public String newArticleForm() { return "articles/new"; } @PostMapping("/articles/create") public String createArticle(ArticleForm form) { System.out.println(form.toString()); // 1. Dto를 Entity 변환 Article article = form.toEntity(); System.out.println(article.toString()); // 2. Repository에게 Entity를 DB로 저장하게 함 Article saved = articleRepository.save(article); System.out.println(saved.toString()); return ""; } } ``` ## 🔥 구글링 훈련하기 - JPA 개념 - JPA 리파지터리란 - JPA Entity 사용법 - @Entity - @Table - @Id - @GeneratedValue - @Column - DTO Entity 분리 장점 - JPA CrudRepository - @Autowired