diff --git a/README.md b/README.md index 82011b9ab3..5492d0f409 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,9 @@ - [ ] JdbcTemplate 구현하기 - [x] 조회기능 구현 - [x] 쓰기기능 구현 -- [ ] UserDao를 JdbcTemplate을 사용하도록 리팩터링 - - [ ] findByAccount - - [ ] findById - - [ ] findAll +- [x] UserDao를 JdbcTemplate을 사용하도록 리팩터링 + - [x] findByAccount + - [x] findById + - [x] findAll - [x] update - [x] insert diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index 52b5a81b75..78b961f7b8 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,32 +1,31 @@ package com.techcourse.dao; -import com.techcourse.config.DataSourceConfig; import com.techcourse.domain.User; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.Mapper; public class UserDao { + private static final Mapper USER_MAPPER = (rs) -> new User( + rs.getLong(1), + rs.getString(2), + rs.getString(3), + rs.getString(4) + ); private static final Logger log = LoggerFactory.getLogger(UserDao.class); - private final DataSource dataSource; private final JdbcTemplate jdbcTemplate; public UserDao(final DataSource dataSource) { - this.dataSource = dataSource; this.jdbcTemplate = new JdbcTemplate(dataSource); } public UserDao(final JdbcTemplate jdbcTemplate) { - this.dataSource = DataSourceConfig.getInstance(); this.jdbcTemplate = jdbcTemplate; } @@ -44,70 +43,24 @@ public void update(final User user) { public List findAll() { final var sql = "select id, account, password, email from users"; - - try (final Connection connection = dataSource.getConnection(); - final PreparedStatement pstmt = connection.prepareStatement(sql)) { - final ResultSet rs = pstmt.executeQuery(); - - log.debug("query : {}", sql); + return jdbcTemplate.executeQuery(sql, (rs) -> { final List users = new ArrayList<>(); - if (rs.next()) { - final User user = extractUser(rs); - users.add(user); + while (rs.next()) { + users.add(USER_MAPPER.map(rs)); } return users; - } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new RuntimeException(e); - } + }); } public User findById(final Long id) { final var sql = "select id, account, password, email from users where id = ?"; - try (final Connection connection = dataSource.getConnection(); - final PreparedStatement pstmt = connection.prepareStatement(sql)) { - pstmt.setLong(1, id); - final ResultSet rs = pstmt.executeQuery(); - - log.debug("query : {}", sql); - - if (rs.next()) { - return extractUser(rs); - } - return null; - } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new RuntimeException(e); - } + return jdbcTemplate.executeQuery(sql, USER_MAPPER, id); } public User findByAccount(final String account) { final var sql = "select id, account, password, email from users where account = ?"; - try (final Connection connection = dataSource.getConnection(); - final PreparedStatement pstmt = connection.prepareStatement(sql)) { - pstmt.setString(1, account); - final ResultSet rs = pstmt.executeQuery(); - - log.debug("query : {}", sql); - - if (rs.next()) { - return extractUser(rs); - } - return null; - } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new RuntimeException(e); - } - } - - private static User extractUser(final ResultSet rs) throws SQLException { - return new User( - rs.getLong(1), - rs.getString(2), - rs.getString(3), - rs.getString(4) - ); + return jdbcTemplate.executeQuery(sql, USER_MAPPER, account); } }