-
Notifications
You must be signed in to change notification settings - Fork 142
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
[Spring JDBC] 정민주 미션 제출합니다. #396
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
97c9cef
step5 : jdbc 연결
JoungMinJu 65e908f
step6 : 데이터 조회하는 부분 jdbc 사용하도록 변경
JoungMinJu 6960701
step7 : 데이터 저장 및 삭제 하는 부분 jdbc 사용하도록 변경
JoungMinJu 78bcf97
refactor : 스키마 타입 변경
JoungMinJu 2d69712
refactor : 로그 지우기
JoungMinJu d14489c
refactor : simpleJdbcInsert 사용하도록 변경
JoungMinJu 1ac93f8
refactor : HH:mm 형식 유지를 위해 컬럼 타입 변경
JoungMinJu ce323b6
refactor : 기본생성자 접근제어자 변경
JoungMinJu 564daa4
refactor : find시 id도 불러오도록 수정
JoungMinJu 8fcd2ac
refactor : SimpleJdbcInsert 체이닝 메소드 사용
JoungMinJu 21e4abf
refactor : @Transactional 추가
JoungMinJu 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
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
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
47 changes: 47 additions & 0 deletions
47
src/main/java/roomescape/entity/repository/ReservationRepository.java
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,47 @@ | ||
package roomescape.entity.repository; | ||
|
||
import java.util.List; | ||
import javax.sql.DataSource; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; | ||
import org.springframework.jdbc.core.namedparam.SqlParameterSource; | ||
import org.springframework.jdbc.core.simple.SimpleJdbcInsert; | ||
import org.springframework.stereotype.Repository; | ||
import roomescape.entity.Reservation; | ||
|
||
@Repository | ||
public class ReservationRepository { | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
private final SimpleJdbcInsert simpleJdbcInsert; | ||
|
||
public ReservationRepository(JdbcTemplate jdbcTemplate, DataSource source) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
this.simpleJdbcInsert = new SimpleJdbcInsert(source); | ||
this.simpleJdbcInsert.setTableName("reservation"); | ||
this.simpleJdbcInsert.usingGeneratedKeyColumns("id"); | ||
} | ||
|
||
public List<Reservation> findAll() { | ||
String sql = "SELECT * FROM reservation"; | ||
return jdbcTemplate.query(sql, (rs, rowNum) -> | ||
new Reservation( | ||
rs.getString("name"), | ||
rs.getString("date"), | ||
rs.getString("time"))); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
public Reservation save(Reservation reservation) { | ||
SqlParameterSource params = new MapSqlParameterSource() | ||
.addValue("name", reservation.getName()) | ||
.addValue("date", reservation.getDate()) | ||
.addValue("time", reservation.getTime()); | ||
long id = simpleJdbcInsert.executeAndReturnKey(params).longValue(); | ||
return new Reservation(id, reservation.getName(), reservation.getDate(), reservation.getTime()); | ||
} | ||
|
||
public int deleteById(Long id) { | ||
String sql = "DELETE FROM reservation WHERE id = ?"; | ||
return jdbcTemplate.update(sql, id); | ||
} | ||
} |
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,2 @@ | ||
spring.h2.console.enabled=true | ||
spring.datasource.url=jdbc:h2:mem:database |
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,8 @@ | ||
CREATE TABLE reservation | ||
( | ||
id BIGINT NOT NULL AUTO_INCREMENT, | ||
name VARCHAR(255) NOT NULL, | ||
date DATE NOT NULL, | ||
time VARCHAR(5) NOT NULL, | ||
PRIMARY KEY (id) | ||
); |
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,80 @@ | ||
package roomescape; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import io.restassured.RestAssured; | ||
import io.restassured.http.ContentType; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import roomescape.entity.Reservation; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) | ||
public class JdbcStepTest { | ||
|
||
@Autowired | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
@Test | ||
void 오단계() { | ||
try (Connection connection = jdbcTemplate.getDataSource().getConnection()) { | ||
assertThat(connection).isNotNull(); | ||
assertThat(connection.getCatalog()).isEqualTo("DATABASE"); | ||
assertThat(connection.getMetaData().getTables(null, null, "RESERVATION", null).next()).isTrue(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Test | ||
void 육단계() { | ||
jdbcTemplate.update("INSERT INTO reservation (name, date, time) VALUES (?, ?, ?)", "브라운", "2023-08-05", | ||
"15:40"); | ||
|
||
List<Reservation> reservations = RestAssured.given().log().all() | ||
.when().get("/reservations") | ||
.then().log().all() | ||
.statusCode(200).extract() | ||
.jsonPath().getList(".", Reservation.class); | ||
|
||
Integer count = jdbcTemplate.queryForObject("SELECT count(1) from reservation", Integer.class); | ||
|
||
assertThat(reservations.size()).isEqualTo(count); | ||
} | ||
|
||
@Test | ||
void 칠단계() { | ||
Map<String, String> params = new HashMap<>(); | ||
params.put("name", "브라운"); | ||
params.put("date", "2023-08-05"); | ||
params.put("time", "10:00"); | ||
|
||
RestAssured.given().log().all() | ||
.contentType(ContentType.JSON) | ||
.body(params) | ||
.when().post("/reservations") | ||
.then().log().all() | ||
.statusCode(201) | ||
.header("Location", "/reservations/1"); | ||
|
||
Integer count = jdbcTemplate.queryForObject("SELECT count(1) from reservation", Integer.class); | ||
assertThat(count).isEqualTo(1); | ||
|
||
RestAssured.given().log().all() | ||
.when().delete("/reservations/1") | ||
.then().log().all() | ||
.statusCode(204); | ||
|
||
Integer countAfterDelete = jdbcTemplate.queryForObject("SELECT count(1) from reservation", Integer.class); | ||
assertThat(countAfterDelete).isEqualTo(0); | ||
} | ||
|
||
} |
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.
이 부분은 참고 사항인데 해당 부분을 이렇게도 표현이 가능하네요
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.
아하 저렇게 하는게 더 가독성이 좋아보이네요 반영해두겠습니다!!