-
Notifications
You must be signed in to change notification settings - Fork 379
[JDBC 라이브러리 구현하기 - 1, 2단계] 에어(김준서) 미션 제출합니다. #7
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
Merged
Merged
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
39413ad
refactor: 한글 경로 인식문제 해결
KJunseo 3cafb13
feat: findAll() 구현
KJunseo 26a31be
feat: findByAccount 구현
KJunseo ce54a46
feat: update 구현
KJunseo 161a287
docs: 초기 todo 문서화
KJunseo 7256776
feat: property 파일로 db 연결 정보 관리
KJunseo c642a8a
refactor: update, insert 메서드 분리
KJunseo 1ab140d
refactor: update, insert 클래스 분리
KJunseo 355eef2
refactor: update, insert 클래스 추상 클래스로 중복 추출
KJunseo 2978eb9
refactor: select 클래스 분리
KJunseo 759e854
refactor: JdbcTemplate에 SelectJdbcTemplate 합치기
KJunseo d3e886f
refactor: 사용하지 않는 클래스 제거 & 패키지 구조 정리
KJunseo 547ecfa
refactor: SQLException을 Unchecked Exception으로 관리
KJunseo 5fb5494
refactor: RowMapper 제네릭 사용
KJunseo ba0b062
refactor: PreparedStatementSetter 가변인자 사용
KJunseo 1c6dd47
test: findAll 검증 추가
KJunseo 0f35ef3
feat: 이메일로 조회 추가
KJunseo 8d9e6e8
refactor: 빌더 패턴 적용
KJunseo 583ce68
refactor: 리스트 반환 메서드에 set value 로직 추가
KJunseo 324df84
refactor: JdbcTemplate 중복 리팩토링
KJunseo 89764bf
refactor: DatabasePopulatorUtils 리팩토링
KJunseo 4baf362
refactor: InMemoryRepository 대신 UserDao 사용
KJunseo 41544f7
refactor: 어색한 테스트 제거
KJunseo 7727116
refactor: 업데이트 시 executeUpdate() 값 반환
KJunseo 635aab0
refactor: 불필요한 query 메서드 줄이기
KJunseo 242709c
test: jdbcTemplate 테스트 추가
KJunseo 3f5d0f2
refactor: 특정 id값 주입 제거
KJunseo 45e4a7e
test: SQLException 발생시 commit 되지 않음
KJunseo 08434e1
refactor: PreparedStatementSetter 구현체 제거
KJunseo b69f10b
refactor: 간단하게 userDao 생성자 주입받기
KJunseo 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,13 @@ | ||
| # jwp-dashboard-jdbc | ||
|
|
||
| ## TODO | ||
| - [x] property 파일로 db 연결 정보 관리 | ||
| - [x] update, insert 메서드 분리 | ||
| - [x] UpdateJdbcTemplate, InsertJdbcTemplate 클래스 분리 | ||
| - [x] UpdateJdbcTemplate, InsertJdbcTemplate 중복 추상클래스(JdbcTemplate)로 제거 | ||
| - [x] SelectJdbcTemplate 클래스 분리 | ||
| - [x] JdbcTemplate에 SelectJdbcTemplate 합치기 | ||
| - [x] mapRow, setValues 인터페이스 분리 | ||
| - [x] SQLException Unchecked Exception으로 관리 | ||
| - [x] RowMapper 제네릭 사용 | ||
| - [x] PreparedStatementSetter 가변인자 사용 |
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
27 changes: 0 additions & 27 deletions
27
app/src/main/java/com/techcourse/config/DataSourceConfig.java
This file was deleted.
Oops, something went wrong.
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
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
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
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 |
|---|---|---|
| @@ -1,116 +1,56 @@ | ||
| package com.techcourse.dao; | ||
|
|
||
| import java.util.List; | ||
| import javax.sql.DataSource; | ||
|
|
||
| import com.techcourse.domain.User; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import javax.sql.DataSource; | ||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.util.List; | ||
| import nextstep.jdbc.JdbcTemplate; | ||
| import nextstep.jdbc.RowMapper; | ||
|
|
||
| public class UserDao { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(UserDao.class); | ||
| private static final RowMapper<User> USER_ROW_MAPPER = | ||
| resultSet -> new User( | ||
| resultSet.getLong("id"), | ||
| resultSet.getString("account"), | ||
| resultSet.getString("password"), | ||
| resultSet.getString("email") | ||
| ); | ||
|
|
||
| private final DataSource dataSource; | ||
| private final JdbcTemplate jdbcTemplate; | ||
|
|
||
| public UserDao(DataSource dataSource) { | ||
| this.dataSource = dataSource; | ||
| this.jdbcTemplate = new JdbcTemplate(dataSource); | ||
| } | ||
|
|
||
| public void insert(User user) { | ||
| final String sql = "insert into users (account, password, email) values (?, ?, ?)"; | ||
|
|
||
| Connection conn = null; | ||
| PreparedStatement pstmt = null; | ||
| try { | ||
| conn = dataSource.getConnection(); | ||
| pstmt = conn.prepareStatement(sql); | ||
|
|
||
| log.debug("query : {}", sql); | ||
|
|
||
| pstmt.setString(1, user.getAccount()); | ||
| pstmt.setString(2, user.getPassword()); | ||
| pstmt.setString(3, user.getEmail()); | ||
| pstmt.executeUpdate(); | ||
| } catch (SQLException e) { | ||
| log.error(e.getMessage(), e); | ||
| throw new RuntimeException(e); | ||
| } finally { | ||
| try { | ||
| if (pstmt != null) { | ||
| pstmt.close(); | ||
| } | ||
| } catch (SQLException ignored) {} | ||
|
|
||
| try { | ||
| if (conn != null) { | ||
| conn.close(); | ||
| } | ||
| } catch (SQLException ignored) {} | ||
| } | ||
| jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail()); | ||
| } | ||
|
|
||
| public void update(User user) { | ||
| // todo | ||
| final String sql = "update users set account = ?, password = ?, email = ? where id = ?"; | ||
| jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail(), user.getId()); | ||
| } | ||
|
|
||
| public List<User> findAll() { | ||
| // todo | ||
| return null; | ||
| final String sql = "select * from users"; | ||
| return jdbcTemplate.query(sql, USER_ROW_MAPPER); | ||
| } | ||
|
|
||
| public User findById(Long id) { | ||
| final String sql = "select id, account, password, email from users where id = ?"; | ||
|
|
||
| Connection conn = null; | ||
| PreparedStatement pstmt = null; | ||
| ResultSet rs = null; | ||
| try { | ||
| conn = dataSource.getConnection(); | ||
| pstmt = conn.prepareStatement(sql); | ||
| pstmt.setLong(1, id); | ||
| rs = pstmt.executeQuery(); | ||
|
|
||
| log.debug("query : {}", sql); | ||
|
|
||
| if (rs.next()) { | ||
| return new User( | ||
| rs.getLong(1), | ||
| rs.getString(2), | ||
| rs.getString(3), | ||
| rs.getString(4)); | ||
| } | ||
| return null; | ||
| } catch (SQLException e) { | ||
| log.error(e.getMessage(), e); | ||
| throw new RuntimeException(e); | ||
| } finally { | ||
| try { | ||
| if (rs != null) { | ||
| rs.close(); | ||
| } | ||
| } catch (SQLException ignored) {} | ||
|
|
||
| try { | ||
| if (pstmt != null) { | ||
| pstmt.close(); | ||
| } | ||
| } catch (SQLException ignored) {} | ||
|
|
||
| try { | ||
| if (conn != null) { | ||
| conn.close(); | ||
| } | ||
| } catch (SQLException ignored) {} | ||
| } | ||
| return jdbcTemplate.queryForObject(sql, USER_ROW_MAPPER, id); | ||
| } | ||
|
|
||
| public User findByAccount(String account) { | ||
| // todo | ||
| return null; | ||
| final String sql = "select id, account, password, email from users where account = ?"; | ||
| return jdbcTemplate.queryForObject(sql, USER_ROW_MAPPER, account); | ||
| } | ||
|
|
||
| public User findByEmail(String email) { | ||
| final String sql = "select id, account, password, email from users where email = ?"; | ||
| return jdbcTemplate.queryForObject(sql, USER_ROW_MAPPER, email); | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
app/src/main/java/com/techcourse/support/context/ContextLoaderListener.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
44 changes: 19 additions & 25 deletions
44
app/src/main/java/com/techcourse/support/jdbc/init/DatabasePopulatorUtils.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 |
|---|---|---|
| @@ -1,47 +1,41 @@ | ||
| package com.techcourse.support.jdbc.init; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import javax.sql.DataSource; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.net.URISyntaxException; | ||
| import java.net.URL; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| import javax.sql.DataSource; | ||
|
|
||
| import nextstep.jdbc.exception.DataAccessException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class DatabasePopulatorUtils { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(DatabasePopulatorUtils.class); | ||
|
|
||
| public static void execute(DataSource dataSource) { | ||
| Connection connection = null; | ||
| Statement statement = null; | ||
| try { | ||
| final URL url = DatabasePopulatorUtils.class.getClassLoader().getResource("schema.sql"); | ||
| final File file = new File(url.getFile()); | ||
| final String sql = Files.readString(file.toPath()); | ||
| connection = dataSource.getConnection(); | ||
| statement = connection.createStatement(); | ||
| try (Connection conn = dataSource.getConnection(); | ||
| Statement statement = conn.createStatement()) { | ||
| final String sql = getInitSchema(); | ||
| statement.execute(sql); | ||
| } catch (NullPointerException | IOException | SQLException e) { | ||
| } catch (SQLException e) { | ||
| throw new DataAccessException(e.getMessage()); | ||
| } catch (URISyntaxException | IOException e) { | ||
|
Comment on lines
+23
to
+29
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. 기본적으로 제공되는 코드도 리팩토링 하셨군요 👍 |
||
| log.error(e.getMessage(), e); | ||
| } finally { | ||
| try { | ||
| if (statement != null) { | ||
| statement.close(); | ||
| } | ||
| } catch (SQLException ignored) {} | ||
|
|
||
| try { | ||
| if (connection != null) { | ||
| connection.close(); | ||
| } | ||
| } catch (SQLException ignored) {} | ||
| } | ||
| } | ||
|
|
||
| private static String getInitSchema() throws URISyntaxException, IOException { | ||
| final URL url = DatabasePopulatorUtils.class.getClassLoader().getResource("schema.sql"); | ||
| final File file = Paths.get(url.toURI()).toFile(); | ||
| return Files.readString(file.toPath()); | ||
| } | ||
|
|
||
| private DatabasePopulatorUtils() {} | ||
| } | ||
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 @@ | ||
| datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1; | ||
| datasource.username= | ||
| datasource.password= | ||
|
Comment on lines
+1
to
+3
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. 오 application.properties로 설정할 수 있도록 구현하셨네요. 좋은 아이디어인것 같습니다 |
||
Oops, something went wrong.
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.
필드 주입도 문제가 없겠지만.
@Inject를 통한 생성자 주입도 고민해보면 좋을 것 같습니다.저도 이부분은 명확하지는 않지만 저는 ControllerScanner에서
@Inject와@Repository를 활용하여 주입하도록 하였습니다. 한 번 고민해보시고 이부분도 의견 나누면 좋을 것 같아여