-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JDBC 라이브러리 구현하기 - 1단계] 로지(윤가영) 미션 제출합니다. (#281)
* feat: UserDao 구현 * refactor: JdbcTemplate 으로 옮기기 * fix: 여러 행이 나올때 처리
- Loading branch information
Showing
5 changed files
with
157 additions
and
83 deletions.
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 |
---|---|---|
@@ -1 +1,10 @@ | ||
# JDBC 라이브러리 구현하기 | ||
|
||
개발자는 SQL 쿼리 작성, 쿼리에 전달할 인자, SELECT 구문일 경우 | ||
조회 결과를 추출하는 것만 집중할 수 있도록 라이브러리를 만들자. | ||
|
||
## 힌트 | ||
|
||
- 리팩터링은 UserDaoTest를 활용해 진행한다. | ||
- 중복을 제거하기 위한 라이브러리는 JdbcTemplate 클래스에 구현한다. | ||
- DataSource는 DataSourceConfig 클래스의 getInstance() 메서드를 호출하면 된다. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,24 +3,32 @@ | |
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class UserDaoTest { | ||
|
||
private UserDao userDao; | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
@BeforeEach | ||
void setup() { | ||
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
|
||
userDao = new UserDao(DataSourceConfig.getInstance()); | ||
jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance()); | ||
userDao = new UserDao(jdbcTemplate); | ||
final var user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(user); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
jdbcTemplate.execute("truncate table users restart identity"); | ||
} | ||
|
||
@Test | ||
void findAll() { | ||
final var users = userDao.findAll(); | ||
|
@@ -66,4 +74,5 @@ void update() { | |
|
||
assertThat(actual.getPassword()).isEqualTo(newPassword); | ||
} | ||
|
||
} |
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
60 changes: 60 additions & 0 deletions
60
jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.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,60 @@ | ||
/* | ||
* Copyright 2002-2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.jdbc.core; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* An interface used by {@link JdbcTemplate} for mapping rows of a | ||
* {@link ResultSet} on a per-row basis. Implementations of this | ||
* interface perform the actual work of mapping each row to a result object, | ||
* but don't need to worry about exception handling. | ||
* {@link SQLException SQLExceptions} will be caught and handled | ||
* by the calling JdbcTemplate. | ||
* | ||
* <p>Typically used either for {@link JdbcTemplate}'s query methods | ||
* or for out parameters of stored procedures. RowMapper objects are | ||
* typically stateless and thus reusable; they are an ideal choice for | ||
* implementing row-mapping logic in a single place. | ||
* | ||
* <p>Alternatively, consider subclassing | ||
* {@code jdbc.object} package: Instead of working with separate | ||
* JdbcTemplate and RowMapper objects, you can build executable query | ||
* objects (containing row-mapping logic) in that style. | ||
* | ||
* @author Thomas Risberg | ||
* @author Juergen Hoeller | ||
* @param <T> the result type | ||
* @see JdbcTemplate | ||
*/ | ||
@FunctionalInterface | ||
public interface RowMapper<T> { | ||
|
||
/** | ||
* Implementations must implement this method to map each row of data | ||
* in the ResultSet. This method should not call {@code next()} on | ||
* the ResultSet; it is only supposed to map values of the current row. | ||
* @param rs the ResultSet to map (pre-initialized for the current row) | ||
* @param rowNum the number of the current row | ||
* @return the result object for the current row (maybe {@code null}) | ||
* @throws SQLException if an SQLException is encountered getting | ||
* column values (that is, there's no need to catch SQLException) | ||
*/ | ||
T mapRow(ResultSet rs, int rowNum) throws SQLException; | ||
|
||
} |