diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index e7603b5451..7817ef7587 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -4,252 +4,50 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; public class UserDao { private static final Logger log = LoggerFactory.getLogger(UserDao.class); - private final DataSource dataSource; - - public UserDao(final DataSource dataSource) { - this.dataSource = dataSource; - } + private final JdbcTemplate jdbcTemplate; public UserDao(final JdbcTemplate jdbcTemplate) { - this.dataSource = (DataSource) jdbcTemplate; + this.jdbcTemplate = jdbcTemplate; } public void insert(final User user) { - final var sql = "insert into users (account, password, email) values (?, ?, ?)"; - - Connection connection = null; - PreparedStatement preparedStatement = null; - try { - connection = dataSource.getConnection(); - preparedStatement = connection.prepareStatement(sql); - - log.debug("query : {}", sql); - - preparedStatement.setString(1, user.getAccount()); - preparedStatement.setString(2, user.getPassword()); - preparedStatement.setString(3, user.getEmail()); - preparedStatement.executeUpdate(); - } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new RuntimeException(e); - } finally { - try { - if (preparedStatement != null) { - preparedStatement.close(); - } - } catch (SQLException ignored) { - } - - try { - if (connection != null) { - connection.close(); - } - } catch (SQLException ignored) { - } - } + String sql = "insert into users (account, password, email) values (?, ?, ?)"; + jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail()); } public void update(final User user) { - // todo - - final String sql = "UPDATE users SET account = ?, password = ? WHERE email = ?"; - - 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) { - } - } + String sql = "UPDATE users SET account = ?, password = ? WHERE email = ?"; + jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail()); } public List findAll() { - // todo - - final String sql = "SELECT id, account, password, email FROM users"; - - Connection conn = null; - PreparedStatement pstmt = null; - ResultSet rs = null; - List userList = new ArrayList<>(); - - try { - conn = dataSource.getConnection(); - pstmt = conn.prepareStatement(sql); - rs = pstmt.executeQuery(); - - log.debug("query : {}", sql); - - while (rs.next()) { - User user = new User( - rs.getLong("id"), - rs.getString("account"), - rs.getString("password"), - rs.getString("email") - ); - userList.add(user); - } - return userList; - } 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) { - } - } + String sql = "SELECT id, account, password, email FROM users"; + return jdbcTemplate.query(sql, rowMapper()); } 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) { - } - } + String sql = "select id, account, password, email from users where id = ?"; + return jdbcTemplate.queryForObject(sql, rowMapper(), id); } public User findByAccount(final String account) { - // todo - - final var sql = "select id, account, password, email from users where account = ?"; - - Connection conn = null; - PreparedStatement pstmt = null; - ResultSet rs = null; - try { - conn = dataSource.getConnection(); - pstmt = conn.prepareStatement(sql); - pstmt.setString(1, account); - 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) { - } + String sql = "select id, account, password, email from users where account = ?"; + return jdbcTemplate.queryForObject(sql, rowMapper(), account); + } - try { - if (conn != null) { - conn.close(); - } - } catch (SQLException ignored) { - } - } + private RowMapper rowMapper() { + return (resultSet) -> new User( + resultSet.getLong("id"), + resultSet.getString("account"), + resultSet.getString("password"), + resultSet.getString("email")); } } diff --git a/app/src/test/java/com/techcourse/dao/UserDaoTest.java b/app/src/test/java/com/techcourse/dao/UserDaoTest.java index 773d7faf82..7e9cc2b01a 100644 --- a/app/src/test/java/com/techcourse/dao/UserDaoTest.java +++ b/app/src/test/java/com/techcourse/dao/UserDaoTest.java @@ -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; @@ -16,7 +17,7 @@ class UserDaoTest { void setup() { DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); - userDao = new UserDao(DataSourceConfig.getInstance()); + userDao = new UserDao(new JdbcTemplate(DataSourceConfig.getInstance())); final var user = new User("gugu", "password", "hkkang@woowahan.com"); userDao.insert(user); }