From 2a605431e13ad70fb2c44fb2068979da2b8434c2 Mon Sep 17 00:00:00 2001 From: Rosie Date: Wed, 27 Sep 2023 14:01:47 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20UserDao=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +++ .../main/java/com/techcourse/dao/UserDao.java | 59 ++++++++++++++----- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 721b81d472..83b92f3e3f 100644 --- a/README.md +++ b/README.md @@ -1 +1,10 @@ # JDBC 라이브러리 구현하기 + +개발자는 SQL 쿼리 작성, 쿼리에 전달할 인자, SELECT 구문일 경우 +조회 결과를 추출하는 것만 집중할 수 있도록 라이브러리를 만들자. + +## 힌트 + +- 리팩터링은 UserDaoTest를 활용해 진행한다. +- 중복을 제거하기 위한 라이브러리는 JdbcTemplate 클래스에 구현한다. +- DataSource는 DataSourceConfig 클래스의 getInstance() 메서드를 호출하면 된다. diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index d14c545f34..2c1bbe33a0 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,6 +1,7 @@ package com.techcourse.dao; import com.techcourse.domain.User; +import java.util.ArrayList; import org.springframework.jdbc.core.JdbcTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,6 +12,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; +import org.springframework.jdbc.core.RowMapper; public class UserDao { @@ -28,7 +30,10 @@ public UserDao(final JdbcTemplate jdbcTemplate) { public void insert(final User user) { final var sql = "insert into users (account, password, email) values (?, ?, ?)"; + execute(sql, user.getAccount(), user.getPassword(), user.getEmail()); + } + private void execute(String sql, Object... parameters) { Connection conn = null; PreparedStatement pstmt = null; try { @@ -37,9 +42,10 @@ public void insert(final User user) { log.debug("query : {}", sql); - pstmt.setString(1, user.getAccount()); - pstmt.setString(2, user.getPassword()); - pstmt.setString(3, user.getEmail()); + for (int i = 0; i < parameters.length; i++) { + pstmt.setObject(i + 1, parameters[i]); + } + pstmt.executeUpdate(); } catch (SQLException e) { log.error(e.getMessage(), e); @@ -60,36 +66,56 @@ public void insert(final User user) { } public void update(final User user) { - // todo + final var sql = "update users set password = ?, email = ?, account = ? where id = ?"; + execute(sql, user.getPassword(), user.getEmail(), user.getAccount(), user.getId()); } public List findAll() { - // todo - return null; + final var sql = "select id, account, password, email from users"; + return query(sql, (rs, rowNum) -> new User(rs.getLong("id"), + rs.getString("account"), + rs.getString("password"), + rs.getString("email"))); } public User findById(final Long id) { final var sql = "select id, account, password, email from users where id = ?"; + return queryForObject(sql, (rs, rowNum) -> new User(rs.getLong("id"), + rs.getString("account"), + rs.getString("password"), + rs.getString("email")), id); + } + private T queryForObject(String sql, RowMapper rowMapper, Object... params) { + List query = query(sql, rowMapper, params); + if (query.size() > 1) { + throw new RuntimeException("too many result"); + } + if (query.isEmpty()) { + return null; + } + return query.get(0); + } + + private List query(String sql, RowMapper rowMapper, Object... parameters) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = dataSource.getConnection(); pstmt = conn.prepareStatement(sql); - pstmt.setLong(1, id); + for (int i = 0; i < parameters.length; i++) { + pstmt.setObject(i + 1, parameters[i]); + } rs = pstmt.executeQuery(); log.debug("query : {}", sql); + final List results = new ArrayList<>(); if (rs.next()) { - return new User( - rs.getLong(1), - rs.getString(2), - rs.getString(3), - rs.getString(4)); + results.add(rowMapper.mapRow(rs, 0)); } - return null; + return results; } catch (SQLException e) { log.error(e.getMessage(), e); throw new RuntimeException(e); @@ -115,7 +141,10 @@ public User findById(final Long id) { } public User findByAccount(final String account) { - // todo - return null; + final var sql = "select id, account, password, email from users where account = ?"; + return queryForObject(sql, (rs, rowNum) -> new User(rs.getLong("id"), + rs.getString("account"), + rs.getString("password"), + rs.getString("email")), account); } }