-
Notifications
You must be signed in to change notification settings - Fork 300
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
[JDBC 라이브러리 구현하기 - 1단계] 로이스(원태연) 미션 제출합니다. #277
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dc70d1d
패키지 위치 변경 및 코드 정리
kang-hyungu 7182a49
패키지 위치 변경 및 코드 정리
kang-hyungu 06379e5
패키지 위치 변경 및 코드 정리
kang-hyungu 484e0e1
feat: JdbcTemplate update 메서드 추가
TaeyeonRoyce 2d57cd9
test: UserHistory log 테스트 추가
TaeyeonRoyce 8552735
refactor: 이전 버전 Insert 제거
TaeyeonRoyce 46f0bae
feat: RowMapper 추가
TaeyeonRoyce 4befeda
feat: 단일 객체 조회(queryForObject) 추가
TaeyeonRoyce 4e39529
feat: findByAccount 추가
TaeyeonRoyce 88ac245
feat: findAll 추가
TaeyeonRoyce d69fed7
feat: update 추가
TaeyeonRoyce d44cd7e
style: 불필요한 import 제거
TaeyeonRoyce 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 |
---|---|---|
@@ -1,121 +1,47 @@ | ||
package com.techcourse.dao; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
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 org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.RowMapper; | ||
|
||
public class UserDao { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(UserDao.class); | ||
private static final RowMapper<User> userRowMapper = (resultSet) -> new User( | ||
resultSet.getLong("id"), | ||
resultSet.getString("account"), | ||
resultSet.getString("password"), | ||
resultSet.getString("email") | ||
); | ||
|
||
private final DataSource dataSource; | ||
|
||
public UserDao(final DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
private final JdbcTemplate jdbcTemplate; | ||
|
||
public UserDao(final JdbcTemplate jdbcTemplate) { | ||
this.dataSource = null; | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public void insert(final User user) { | ||
final var 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(final User user) { | ||
// todo | ||
final var 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 var sql = "select id, account, password, email from users"; | ||
return jdbcTemplate.query(sql, userRowMapper); | ||
} | ||
|
||
public User findById(final Long id) { | ||
final var 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, userRowMapper, id); | ||
} | ||
|
||
public User findByAccount(final String account) { | ||
// todo | ||
return null; | ||
final var sql = "select id, account, password, email from users where account = ?"; | ||
return jdbcTemplate.queryForObject(sql, userRowMapper, account); | ||
} | ||
} |
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,62 +1,48 @@ | ||
package com.techcourse.dao; | ||
|
||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.RowMapper; | ||
|
||
public class UserHistoryDao { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(UserHistoryDao.class); | ||
private static final RowMapper<UserHistory> userHistoryRowMapper = (resultSet) -> new UserHistory( | ||
resultSet.getLong("id"), | ||
resultSet.getLong("user_id"), | ||
resultSet.getString("account"), | ||
resultSet.getString("password"), | ||
resultSet.getString("email"), | ||
resultSet.getString("created_by") | ||
); | ||
|
||
private final DataSource dataSource; | ||
|
||
public UserHistoryDao(final DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
private final JdbcTemplate jdbcTemplate; | ||
|
||
public UserHistoryDao(final JdbcTemplate jdbcTemplate) { | ||
this.dataSource = null; | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public void log(final UserHistory userHistory) { | ||
final var sql = "insert into user_history (user_id, account, password, email, created_at, created_by) values (?, ?, ?, ?, ?, ?)"; | ||
|
||
Connection conn = null; | ||
PreparedStatement pstmt = null; | ||
try { | ||
conn = dataSource.getConnection(); | ||
pstmt = conn.prepareStatement(sql); | ||
|
||
log.debug("query : {}", sql); | ||
|
||
pstmt.setLong(1, userHistory.getUserId()); | ||
pstmt.setString(2, userHistory.getAccount()); | ||
pstmt.setString(3, userHistory.getPassword()); | ||
pstmt.setString(4, userHistory.getEmail()); | ||
pstmt.setObject(5, userHistory.getCreatedAt()); | ||
pstmt.setString(6, userHistory.getCreateBy()); | ||
pstmt.executeUpdate(); | ||
} catch (SQLException e) { | ||
log.error(e.getMessage(), e); | ||
throw new RuntimeException(e); | ||
} finally { | ||
try { | ||
if (pstmt != null) { | ||
pstmt.close(); | ||
} | ||
} catch (SQLException ignored) {} | ||
log.debug("User history id : {}", userHistory.getUserId()); | ||
final var sql = "insert into user_history " | ||
+ "(user_id, account, password, email, created_at, created_by) " | ||
+ "values (?, ?, ?, ?, ?, ?)"; | ||
jdbcTemplate.update(sql, | ||
userHistory.getUserId(), | ||
userHistory.getAccount(), | ||
userHistory.getPassword(), | ||
userHistory.getEmail(), | ||
userHistory.getCreatedAt(), | ||
userHistory.getCreateBy() | ||
); | ||
} | ||
|
||
try { | ||
if (conn != null) { | ||
conn.close(); | ||
} | ||
} catch (SQLException ignored) {} | ||
} | ||
public UserHistory findLogByUser(final User user) { | ||
log.debug("User history id : {}", user.getId()); | ||
final var sql = "select * from user_history where user_id = ?"; | ||
return jdbcTemplate.queryForObject(sql, userHistoryRowMapper, user.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
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
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; | ||
|
||
|
@@ -15,17 +16,20 @@ class UserDaoTest { | |
@BeforeEach | ||
void setup() { | ||
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
|
||
userDao = new UserDao(DataSourceConfig.getInstance()); | ||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance()); | ||
userDao = new UserDao(jdbcTemplate); | ||
final var user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(user); | ||
} | ||
|
||
@Test | ||
void findAll() { | ||
userDao.insert(new User("gugu2", "password2", "[email protected]")); | ||
userDao.insert(new User("gugu3", "password3", "[email protected]")); | ||
final var users = userDao.findAll(); | ||
|
||
assertThat(users).isNotEmpty(); | ||
assertThat(users).hasSize(3); | ||
} | ||
|
||
@Test | ||
|
@@ -35,6 +39,13 @@ void findById() { | |
assertThat(user.getAccount()).isEqualTo("gugu"); | ||
} | ||
|
||
@Test | ||
void findByIdShouldBeNull() { | ||
final var nullableUser = userDao.findById(3L); | ||
|
||
assertThat(nullableUser).isNull(); | ||
} | ||
|
||
@Test | ||
void findByAccount() { | ||
final var account = "gugu"; | ||
|
@@ -45,12 +56,15 @@ void findByAccount() { | |
|
||
@Test | ||
void insert() { | ||
// given | ||
final var account = "insert-gugu"; | ||
final var user = new User(account, "password", "[email protected]"); | ||
userDao.insert(user); | ||
|
||
// when | ||
final var actual = userDao.findById(2L); | ||
|
||
// then | ||
assertThat(actual.getAccount()).isEqualTo(account); | ||
} | ||
|
||
|
38 changes: 38 additions & 0 deletions
38
app/src/test/java/com/techcourse/dao/UserHistoryDaoTest.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,38 @@ | ||
package com.techcourse.dao; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
class UserHistoryDaoTest { | ||
|
||
private UserHistoryDao userHistoryDao; | ||
|
||
@BeforeEach | ||
void setup() { | ||
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance()); | ||
userHistoryDao = new UserHistoryDao(jdbcTemplate); | ||
} | ||
|
||
@Test | ||
void log() { | ||
// given | ||
final String userHistoryCreatedBy = "royce"; | ||
final var user = new User(1L, "gugu", "password", "[email protected]"); | ||
final var userHistory = new UserHistory(user, userHistoryCreatedBy); | ||
|
||
// when | ||
userHistoryDao.log(userHistory); | ||
|
||
// then | ||
final UserHistory actual = userHistoryDao.findLogByUser(user); | ||
assertThat(actual.getCreateBy()).isEqualTo(userHistoryCreatedBy); | ||
} | ||
} |
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.
var를 쓰시면서 로이스가 체감한 장단점이 있다면 어떤 것들이 있을 지 궁금합니다 :)