Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
Tags
- Selection Sorting
- datastructure
- insertion sort
- coding test
- JSON
- list 컬렉션
- stream
- C programming
- 혼자 공부하는 C언어
- R
- 이스케이프 문자
- C 언어 코딩 도장
- 윤성우 열혈자료구조
- 메모리구조
- s
- 이것이 자바다
- Algorithm
- Serialization
- buffer
- Stack
- Graph
- 윤성우의 열혈 자료구조
- 알기쉬운 알고리즘
Archives
- Today
- Total
Engineering Note
[스프링부트 입문 08] 데이터 생성하기 with JPA 본문
본 내용은 홈팍 스프링 부트 강좌를 참고하였습니다.
https://www.youtube.com/watch?v=ZGgf_1OXcAY&list=PLyebPLlVYXCiYdYaWRKgCqvnCFrLEANXt&index=9
지난 코드에서는 Client가 전송한 Form data를 Spring boot controller에서 Object로 받고 제대로 들어 왔는지 확인해보았다.
이번시간에는 DTO는 자바 객체이므로 DB가 이해할 수 있는 규격화 된 객체, 데이터로 변환해주어야 한다. 이렇게 변환된 데이터를 Entity라고 한다. 그리고 이렇게 DTO에서 Entity로 변환된 데이터를 Repository라는 도구를 통해 DB에 저장할 수 있다. (JPA가 Entity와 Repository를 제공해준다. )
Article Controller
package com.example.boardproject.controller;
import com.example.boardproject.dto.ArticleForm;
import com.example.boardproject.entity.Article;
import com.example.boardproject.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){
//0. Form데이터, DTO 제대로 왔는지 출력
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 "redirect:/";
}
}
Article Entity
package com.example.boardproject.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity //DB가 해당객체를 인식 하게 해줌
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 + '\'' +
'}';
}
}
Article Repository
package com.example.boardproject.repository;
import com.example.boardproject.entity.Article;
import org.springframework.data.repository.CrudRepository;
public interface ArticleRepository extends CrudRepository<Article, Long> {
}
ArticleRepository는 CrudRepository를 상속 받아서 사용한다. CrudRepository는 스프링 부트 JPA에서 제공해주고 있다. 이 안에 DB CRUD에 필요한 메서드를 제공해준다.
CrudRepository를 상속받을 때, 첫 번째 인자는 관리 대상 Entity를 넣어준다. 두번째 인자는 대표값 id의 타입을 넣어준다.
'Server > Spring' 카테고리의 다른 글
| [스프링 부트 입문 10] 롬복과 리팩토링 (0) | 2022.07.08 |
|---|---|
| [스프링 부트 입문 09]DB 테이블과 SQL (0) | 2022.07.08 |
| [스프링 부트 입문 07] 폼 데이터 주고 받기 (0) | 2022.07.08 |
| [스프링 부트 입문 04] 뷰 템플릿과 MVC 패턴 (0) | 2022.07.08 |
| [Spring] Spring boot 자동 재실행 by devtools (0) | 2022.07.08 |
Comments