From bf0abbcd2d9a05830014d963fc55872d6c1aed26 Mon Sep 17 00:00:00 2001 From: Dongseon Yang <78548833+dongseon0@users.noreply.github.com> Date: Sun, 24 Dec 2023 14:57:45 +0900 Subject: [PATCH 1/6] Rename S3Controller.java to S3Controller.java --- .../S3Controller.java" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "5\354\243\274\354\260\250 Sever S-Day \352\263\274\354\240\234/S3Controller.java" => "5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller.java" (100%) diff --git "a/5\354\243\274\354\260\250 Sever S-Day \352\263\274\354\240\234/S3Controller.java" "b/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller.java" similarity index 100% rename from "5\354\243\274\354\260\250 Sever S-Day \352\263\274\354\240\234/S3Controller.java" rename to "5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller.java" From 7eaf51da322b6339e2f6de2c92526bf4d2fa8f8f Mon Sep 17 00:00:00 2001 From: Dongseon Yang <78548833+dongseon0@users.noreply.github.com> Date: Sun, 24 Dec 2023 14:58:18 +0900 Subject: [PATCH 2/6] =?UTF-8?q?Rename=20=EC=96=91=EB=8F=99=EC=84=A0=5F5?= =?UTF-8?q?=EC=A3=BC=EC=B0=A8=5F=EA=B3=BC=EC=A0=9C.md=20to=20=EC=96=91?= =?UTF-8?q?=EB=8F=99=EC=84=A0=5F5=EC=A3=BC=EC=B0=A8=5F=EA=B3=BC=EC=A0=9C.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...204\240_5\354\243\274\354\260\250_\352\263\274\354\240\234.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "5\354\243\274\354\260\250 Sever S-Day \352\263\274\354\240\234/\354\226\221\353\217\231\354\204\240_5\354\243\274\354\260\250_\352\263\274\354\240\234.md" => "5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/\354\226\221\353\217\231\354\204\240_5\354\243\274\354\260\250_\352\263\274\354\240\234.md" (100%) diff --git "a/5\354\243\274\354\260\250 Sever S-Day \352\263\274\354\240\234/\354\226\221\353\217\231\354\204\240_5\354\243\274\354\260\250_\352\263\274\354\240\234.md" "b/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/\354\226\221\353\217\231\354\204\240_5\354\243\274\354\260\250_\352\263\274\354\240\234.md" similarity index 100% rename from "5\354\243\274\354\260\250 Sever S-Day \352\263\274\354\240\234/\354\226\221\353\217\231\354\204\240_5\354\243\274\354\260\250_\352\263\274\354\240\234.md" rename to "5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/\354\226\221\353\217\231\354\204\240_5\354\243\274\354\260\250_\352\263\274\354\240\234.md" From 935a16419e48fbd8f84d211c7b1c0ab9bed14e5c Mon Sep 17 00:00:00 2001 From: Dongseon Yang <78548833+dongseon0@users.noreply.github.com> Date: Sun, 24 Dec 2023 14:59:06 +0900 Subject: [PATCH 3/6] =?UTF-8?q?Create=206=EC=A3=BC=EC=B0=A8=20Server=20S-D?= =?UTF-8?q?ay=20=EA=B3=BC=EC=A0=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...243\274\354\260\250 Server S-Day \352\263\274\354\240\234.md" | 1 + 1 file changed, 1 insertion(+) create mode 100644 "6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234.md" diff --git "a/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234.md" "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234.md" new file mode 100644 index 0000000..8b13789 --- /dev/null +++ "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234.md" @@ -0,0 +1 @@ + From 7fb0d4f8be06463d1e06de89a66ac272529bb709 Mon Sep 17 00:00:00 2001 From: Dongseon Yang <78548833+dongseon0@users.noreply.github.com> Date: Sun, 24 Dec 2023 17:30:54 +0900 Subject: [PATCH 4/6] =?UTF-8?q?[=EC=96=91=EB=8F=99=EC=84=A0]6=EC=A3=BC?= =?UTF-8?q?=EC=B0=A8=20=EA=B3=BC=EC=A0=9C=20-=20complete?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 200 ok --- ... Server S-Day \352\263\274\354\240\234.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234.md" "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234.md" index 8b13789..333185a 100644 --- "a/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234.md" +++ "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234.md" @@ -1 +1,27 @@ +## 과정 +### 1. Entity를 완성하고 test 해보기 +스크린샷 2023-12-24 오후 3 04 20 + +- table이 생성된 것을 확인할 수 있다! + +
+ +### 2. 코드 작성 (Controller 만들기) +- application.yml, RdsRepository.java, RdsService.java, BookForm.java, RdsController.java 파일 수정하기 +- localhost:8080으로 확인한 화면 + +스크린샷 2023-12-24 오후 5 09 01 + +### 3. Controller를 API로 만들기 +- Controller를 RestController로 수정 +- 결과: +스크린샷 2023-12-24 오후 5 16 47 +- PostMan 사용 : get, post test + + + +## 보충 (난관-해결..!) +### 쿼리란? + - query란 단어의 뜻은 문의하다, 질문하다라는 뜻이고, 이러한 문의는 요청, 즉 '데이터베이스에 정보를 요청하는 일'을 말한다. 정보를 처리하는 과정에서 query를 보내면 이에 따른 정보를 DB로부터 가져온다. + - 참고 : https://velog.io/@rgfdds98/SQL-query란-무엇일까 From b1b914f4ac14564a6955b666eac2974fe8f29458 Mon Sep 17 00:00:00 2001 From: Dongseon Yang <78548833+dongseon0@users.noreply.github.com> Date: Sun, 24 Dec 2023 17:32:45 +0900 Subject: [PATCH 5/6] =?UTF-8?q?6=EC=A3=BC=EC=B0=A8=20=EA=B3=BC=EC=A0=9C=20?= =?UTF-8?q?-complete?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Book.java" | 21 +++++++++++ .../BookForm.java" | 12 +++++++ .../RdsController.java" | 35 +++++++++++++++++++ .../RdsRepository.java" | 26 ++++++++++++++ .../RdsService.java" | 28 +++++++++++++++ .../application.yml" | 13 +++++++ 6 files changed, 135 insertions(+) create mode 100644 "6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/Book.java" create mode 100644 "6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/BookForm.java" create mode 100644 "6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/RdsController.java" create mode 100644 "6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/RdsRepository.java" create mode 100644 "6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/RdsService.java" create mode 100644 "6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/application.yml" diff --git "a/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/Book.java" "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/Book.java" new file mode 100644 index 0000000..ae8b3cc --- /dev/null +++ "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/Book.java" @@ -0,0 +1,21 @@ +package ServerStudy6Cloud.ServerStudy6Cloud.Domain; + +import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; + +@Entity +@Setter +@Getter +@Table(name = "book_info") +public class Book { + @Id + @GeneratedValue + @Column(name = "book_info") + private Long id; + + private String name;//책 이름 + private String reason;//해당 책을 좋아하는 이유 + // Entity 완성 + //를 입력받고 AWS rds에 저장하는 실습 +} diff --git "a/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/BookForm.java" "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/BookForm.java" new file mode 100644 index 0000000..3d7410a --- /dev/null +++ "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/BookForm.java" @@ -0,0 +1,12 @@ +package ServerStudy6Cloud.ServerStudy6Cloud.Controller; + +import lombok.Getter; +import lombok.Setter; + +@Getter @Setter +public class BookForm { // 폼 객체 + //책 이름, 책 좋아하는 이유 + private String name; + private String reason; + +} \ No newline at end of file diff --git "a/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/RdsController.java" "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/RdsController.java" new file mode 100644 index 0000000..0ca3344 --- /dev/null +++ "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/RdsController.java" @@ -0,0 +1,35 @@ +package ServerStudy6Cloud.ServerStudy6Cloud.Controller; + +import ServerStudy6Cloud.ServerStudy6Cloud.Domain.Book; +import ServerStudy6Cloud.ServerStudy6Cloud.Service.RdsService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +public class RdsController { + private final RdsService rdsService; + //AWS RDS에서 Book list를 가져오는 GetMapping + @GetMapping("/") + public ResponseEntity> readDB(){ + List bookList = rdsService.findBooks(); + return new ResponseEntity<>(bookList, HttpStatus.OK); + } + + //AWS RDS에 Book 객체를 저장하는 PostMapping + @PostMapping("/upload") // upload라는 경로로 가면 저장 + public ResponseEntity> updateDB(BookForm form) { + Book book = new Book(); + book.setName(form.getName()); + book.setReason(form.getReason()); + rdsService.saveBook(book); // book 객체 넣어주고 + return new ResponseEntity<>(HttpStatus.CREATED); + } +} diff --git "a/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/RdsRepository.java" "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/RdsRepository.java" new file mode 100644 index 0000000..4c7dceb --- /dev/null +++ "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/RdsRepository.java" @@ -0,0 +1,26 @@ +package ServerStudy6Cloud.ServerStudy6Cloud.Repository; + +import ServerStudy6Cloud.ServerStudy6Cloud.Domain.Book; +import jakarta.persistence.EntityManager; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +@RequiredArgsConstructor // final이 붙은 필드에 자동으로 생성자 만들어주는 annotation +public class RdsRepository { + private final EntityManager em; //EntityManager : 엔티티 객체와 데베 간 상호작용 관리, DB와 맵핑되려면 꼭 필요 + //DB에 새로운 책 저장하는 메서드 + public void save(Book book){ // 책 객체 저장 메소드 + em.persist(book); // 영속상태 : 자동으로 JPA가 엔티티의 변경을 추적하고 관리할 수 있는 상태 + } + + //DB에서 모든 책 리스트 가져오는 메서드 + public List findAll(){ + // JPQL은 객체 지향적인 방식으로 데베 검색을 정의할 수 있는 쿼리 언어, 이를 생성하는 메소드 + // JQL쿼리와 조회할 class : Book이라는 클래스에서 해당 쿼리를 진행 + return em.createQuery("select b from Book b", Book.class) + .getResultList(); // 그 결과를 리스트로 반환 + } +} diff --git "a/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/RdsService.java" "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/RdsService.java" new file mode 100644 index 0000000..8961227 --- /dev/null +++ "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/RdsService.java" @@ -0,0 +1,28 @@ +package ServerStudy6Cloud.ServerStudy6Cloud.Service; + +import ServerStudy6Cloud.ServerStudy6Cloud.Domain.Book; +import ServerStudy6Cloud.ServerStudy6Cloud.Repository.RdsRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service // 서비스 명시 +@Transactional // JPA의 중요한 로직은 이 안에서 실행 +@RequiredArgsConstructor // final이 있는 argument의 생성자를 자동 생성(롬복 제공) +public class RdsService { + //RdsRepository를 사용해 DB에 저장하는 로직 + private final RdsRepository rdsRepository; // final로 해야 repo가 변경되는 문제를 막을 수 있음 + + @Transactional(readOnly = true) + public List findBooks(){ + return rdsRepository.findAll(); + } + + public Long saveBook(Book book){ + rdsRepository.save(book); + return book.getId(); // 값이 저장되었는지 확인하는 용도 + } + +} diff --git "a/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/application.yml" "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/application.yml" new file mode 100644 index 0000000..77c8cf7 --- /dev/null +++ "b/6\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/application.yml" @@ -0,0 +1,13 @@ +# RDS +spring: + datasource: + url: jdbc:mysql://gdsc-rds.cn2mak2qq5zj.us-east-1.rds.amazonaws.com:3306/study6 + username: admin + password: gdsccloud + driver-class-name: com.mysql.cj.jdbc.Driver + # hibernate + jpa: + show-sql: true + hibernate: + ddl-auto: update #rds에 없는 부분은 업데이트 되고, 아닌 부분은 그냥 남게 된다. + dialect: org.hibernate.dialect.MySQL8Dialect \ No newline at end of file From b62d8b41aefa173baf79839b902f5f8dc2eaa4d4 Mon Sep 17 00:00:00 2001 From: Dongseon Yang <78548833+dongseon0@users.noreply.github.com> Date: Sun, 24 Dec 2023 17:38:37 +0900 Subject: [PATCH 6/6] =?UTF-8?q?5=EC=A3=BC=EC=B0=A8=20=EA=B3=BC=EC=A0=9C=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../S3Controller_Dongseon.java" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller.java" => "5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller_Dongseon.java" (100%) diff --git "a/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller.java" "b/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller_Dongseon.java" similarity index 100% rename from "5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller.java" rename to "5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller_Dongseon.java"