-
Notifications
You must be signed in to change notification settings - Fork 170
[Spring JDBC] 민지인 미션제출합니다 #514
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
Open
jxxxxxn
wants to merge
11
commits into
next-step:jxxxxxn
Choose a base branch
from
jxxxxxn:step567
base: jxxxxxn
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d00ac48
[Feat] 홈 화면, 예약 조회 화면
jxxxxxn 5fddb60
[Feat] return값에서 .html 삭제
jxxxxxn a120bd3
[Feat] reservation() Model 추가, 예약 초기 데이터 추가
jxxxxxn b3cf994
[Feat] reservations() @ResponseBody로 수정
jxxxxxn 81259f3
[Feat] dto 멤버변수 private으로 변경
jxxxxxn 0ea5bfe
[Fix] dto 멤버변수 private으로 변경했을 시 오류 수정 (@Getter 사용), @AllArgsConstruct…
jxxxxxn 41ff18e
[Feat] 3단계 - 예약 추가 / 취소
jxxxxxn a4a9f6b
[Feat] 4단계 - 예외 처리
jxxxxxn 56b2a8e
[Feat] 5단계 - 데이터베이스 적용하기
jxxxxxn d364a3c
[Feat] 6단계 - 데이터 조회하기
jxxxxxn a7cb419
[Feat] 7단계 - 데이터 추가/삭제하기
jxxxxxn 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 hidden or 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
91 changes: 91 additions & 0 deletions
91
src/main/java/roomescape/controller/ReservationController.java
This file contains hidden or 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,91 @@ | ||
| package roomescape.controller; | ||
|
|
||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.ui.Model; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import roomescape.dto.ReservationAddReq; | ||
| import roomescape.dto.ReservationReq; | ||
| import roomescape.exception.InvalidRequestReservationException; | ||
| import roomescape.exception.NotFoundReservationException; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| @Controller | ||
| public class ReservationController { | ||
|
|
||
| //AtomicLong: Long 자료형 가지는 Wrapping 클래스. incrementAndGet(): ++x | ||
| private AtomicLong index = new AtomicLong(0); | ||
| private List<ReservationReq> reservations1 = new ArrayList<>(); | ||
| private JdbcTemplate jdbcTemplate; | ||
|
|
||
| public ReservationController(JdbcTemplate jdbcTemplate) { | ||
| this.jdbcTemplate = jdbcTemplate; | ||
| } | ||
|
|
||
| @GetMapping("/reservation") | ||
| public String reservation(Model model){ | ||
|
|
||
| model.addAttribute(reservations1); | ||
| return "reservation"; | ||
| } | ||
|
|
||
| //예약 조회 | ||
| @GetMapping("/reservations") | ||
| @ResponseBody | ||
| public List<ReservationReq> reservations(){ | ||
| String sql="SELECT * from reservation"; | ||
| List<ReservationReq> reservations=jdbcTemplate.query( | ||
| sql, | ||
| (resultSet,rowNum)->{ | ||
| ReservationReq reservation = new ReservationReq( | ||
| resultSet.getLong("id"), | ||
| resultSet.getString("name"), | ||
| resultSet.getString("date"), | ||
| resultSet.getString("time") | ||
| ); | ||
| return reservation; | ||
| }); | ||
| return reservations; | ||
| } | ||
|
|
||
| //예약 추가 | ||
| @PostMapping("/reservations") | ||
| @ResponseBody | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| public ReservationReq addReservation(@RequestBody ReservationAddReq reservation, HttpServletResponse response){ | ||
| if(reservation.getName().isEmpty()||reservation.getDate().isEmpty()||reservation.getTime().isEmpty()){ | ||
| throw new InvalidRequestReservationException("필요한 인자가 없습니다."); | ||
| } | ||
|
|
||
| String sql="INSERT INTO reservation(name, date, time) VALUES (?,?,?)"; | ||
| jdbcTemplate.update(sql,reservation.getName(),reservation.getDate(),reservation.getTime()); | ||
| String getIdSql="SELECT LAST_INSERT_ID()"; //해당 구문 사용 위해 application.properties에 MODE=MySQL 추가함 | ||
| Long id=jdbcTemplate.queryForObject(getIdSql, Long.class); | ||
| ReservationReq newReservation =new ReservationReq(id,reservation.getName(),reservation.getDate(),reservation.getTime()); | ||
|
|
||
| // ResponseEntity 사용하면 header 명시적으로 지정하지 않아도 된다고 한다. | ||
| response.setHeader("Location", "/reservations/" + id); | ||
| return newReservation; | ||
| } | ||
|
|
||
| //예약 삭제 | ||
| @DeleteMapping("/reservations/{id}") | ||
| @ResponseBody | ||
| @ResponseStatus(HttpStatus.NO_CONTENT) | ||
| public void deleteReservation(@PathVariable Long id){ | ||
|
|
||
| String sql="DELETE FROM reservation WHERE id=?"; | ||
| int rowCount=jdbcTemplate.update(sql,id); | ||
| if(rowCount==0){ | ||
| throw new NotFoundReservationException("삭제할 예약이 없습니다"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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 roomescape.controller; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
|
|
||
| @Controller | ||
| public class StartController { | ||
|
|
||
| @GetMapping("/") | ||
| public String home(){ | ||
| return "home"; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } |
This file contains hidden or 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 roomescape.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public class ReservationAddReq { | ||
| private String name; | ||
| private String date; //우선 String으로.. 차후 Date로 형변환 필요하면 교체.. | ||
| private String time; //동일!! | ||
| } |
This file contains hidden or 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,17 @@ | ||
| package roomescape.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor //기본 생성자 자동 생성 | ||
| public class ReservationReq { | ||
| //캡슐화를 위해서는 private으로 해야 하지만, step12의 코드에서 private으로 하면 JSON이 접근을 못 해서 직렬화 못 하는 에러 발생 | ||
| //해결을 위해 lombok의 getter 추가 | ||
| private Long id; | ||
| private String name; | ||
| private String date; //우선 String으로.. 차후 Date로 형변환 필요하면 교체.. | ||
| private String time; //동일!! | ||
|
|
||
|
|
||
| } |
This file contains hidden or 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,21 @@ | ||
| package roomescape.exception; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
|
||
|
|
||
| @ControllerAdvice | ||
| public class ExceptionHandlers { | ||
|
|
||
| @ExceptionHandler(NotFoundReservationException.class) | ||
| public ResponseEntity NotFoundReservationException() { //딱히 메시지에 id 등 추가할 필요를 못 느껴서 매개변수 없앰. | ||
| return ResponseEntity.badRequest().build(); | ||
| } | ||
|
|
||
| @ExceptionHandler(InvalidRequestReservationException.class) | ||
| public ResponseEntity InvalidRequestReservationException(){ | ||
| return ResponseEntity.badRequest().build(); | ||
| } | ||
|
|
||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/roomescape/exception/InvalidRequestReservationException.java
This file contains hidden or 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,7 @@ | ||
| package roomescape.exception; | ||
|
|
||
| public class InvalidRequestReservationException extends RuntimeException { | ||
| public InvalidRequestReservationException(String message) { | ||
| super(message); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/roomescape/exception/NotFoundReservationException.java
This file contains hidden or 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,7 @@ | ||
| package roomescape.exception; | ||
|
|
||
| public class NotFoundReservationException extends RuntimeException { | ||
| public NotFoundReservationException(String message) { | ||
| super(message); | ||
| } | ||
| } |
This file contains hidden or 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,3 @@ | ||
| spring.h2.console.enabled=true | ||
| spring.h2.console.path=/h2-console | ||
| spring.datasource.url=jdbc:h2:mem:database;MODE=MySQL |
This file contains hidden or 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,8 @@ | ||
| CREATE TABLE reservation | ||
| ( | ||
| id BIGINT NOT NULL AUTO_INCREMENT, | ||
| name VARCHAR(255) NOT NULL, | ||
| date VARCHAR(255) NOT NULL, | ||
| time VARCHAR(255) NOT NULL, | ||
| PRIMARY KEY (id) | ||
| ); |
This file contains hidden or 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
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.
JDBC 에서 자동 생성된 키 값을 조회하기 위해 어떤 방법이 있을까요? keyholder 개념을 활용해보는 것도 좋아 보입니다. 이 글을 참고해보세요!