-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[이서현] 6주차 과제 - complete #57
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ServerStudy6Cloud.ServerStudy6Cloud.Domain; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Entity | ||
@Getter @Setter | ||
@Table(name= "book_info") | ||
public class Book { | ||
@Id | ||
@GeneratedValue | ||
@Column(name="book_id") | ||
private Long id; | ||
|
||
private String name; //책 이름 | ||
private String reason; //해당 책을 좋아하는 이유 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ServerStudy6Cloud.ServerStudy6Cloud.Controller; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
//BookForm: 폼에서 입력받은 내용과 entity에 넣을 내용 분리 위해 만든 클래스 | ||
@Getter @Setter | ||
public class BookForm { | ||
//책 이름, 책 좋아하는 이유 | ||
private String name; | ||
private String reason; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController //API 만들기 | ||
@RequiredArgsConstructor | ||
public class RdsController { | ||
private final RdsService rdsService; | ||
//AWS RDS에서 Book list를 가져오는 GetMapping | ||
|
||
@GetMapping("/") | ||
public ResponseEntity<List<Book>> readDB(){ | ||
List<Book> bookList = rdsService.findBooks(); | ||
return new ResponseEntity<>(bookList, HttpStatus.OK); | ||
} | ||
|
||
@PostMapping("/upload") | ||
public ResponseEntity<Void> updateDB(BookForm form){ | ||
Book book = new Book(); | ||
book.setName(form.getName()); | ||
book.setReason(form.getReason()); | ||
rdsService.saveBook(book); | ||
return new ResponseEntity<>(HttpStatus.CREATED); | ||
} | ||
|
||
|
||
//AWS RDS에 Book 객체를 저장하는 PostMapping | ||
} | ||
|
||
//@Controller에서 view에 리턴할 때의 컨트롤러 코드 | ||
/*@GetMapping("/") | ||
public String readDB(Model model){ | ||
model.addAttribute("bookForm", new BookForm()); //BookForm 객체 넘기기 | ||
model.addAttribute("books", rdsService.findBooks()); | ||
return "index"; | ||
} | ||
@PostMapping("/upload") | ||
public String updateDB(BookForm form){ | ||
Book book = new Book(); | ||
book.setName(form.getName()); | ||
book.setReason(form.getReason()); | ||
rdsService.saveBook(book); | ||
return "redirect:/"; | ||
} | ||
*/ | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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 | ||
public class RdsRepository { | ||
private final EntityManager em; | ||
//DB에 새로운 책 저장하는 메서드 | ||
public void save(Book book){ | ||
em.persist(book); | ||
} | ||
//DB에서 모든 책 리스트 가져오는 메서드 | ||
public List<Book> findAll() { | ||
return em.createQuery("select b from Book b", Book.class) //JPQL 쿼리와 조회할 class | ||
.getResultList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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 | ||
@RequiredArgsConstructor | ||
public class RdsService { | ||
//RdsRepository를 사용해 DB에 저장하는 로직 | ||
private final RdsRepository rdsRepository; //레포지토리 주입받음 | ||
|
||
@Transactional(readOnly=true) //조회에 최적화되게 Transaction 사용가능 | ||
public List<Book> findBooks() { | ||
return rdsRepository.findAll(); | ||
} | ||
|
||
public Long saveBook(Book book) { | ||
rdsRepository.save(book); | ||
return book.getId(); //값 저장되었는지 확인하는 용도 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# RDS | ||
spring: | ||
datasource: | ||
url: jdbc:mysql://gdsc-rds2.cf0i42aimwar.ap-northeast-2.rds.amazonaws.com:3306/study_6 | ||
username: admin | ||
password: [password] | ||
driver-class-name: com.mysql.cj.jdbc.Driver | ||
# hibernate | ||
jpa: | ||
show-sql: true | ||
hibernate: | ||
ddl-auto: update | ||
dialect: org.hibernate.dialect.MySQL8Dialect |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
강의의 과정을 하나하나 따라해주신 게 보여서 감동적이에요...!🥹