From 02a0c8c42f7b691431adbcc90d4497741ce18a77 Mon Sep 17 00:00:00 2001 From: drunkenhw Date: Tue, 26 Sep 2023 13:14:25 +0900 Subject: [PATCH 1/5] =?UTF-8?q?refactor:=20insert=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/dao/UserDao.java | 43 +++++-------------- .../jdbc/core/JdbcTemplate.java | 37 ++++++++++++++++ 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index d14c545f34..e92577e99a 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -17,46 +17,21 @@ public class UserDao { 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 = 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) { @@ -70,7 +45,6 @@ public List findAll() { 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; @@ -98,19 +72,22 @@ public User findById(final Long id) { if (rs != null) { rs.close(); } - } catch (SQLException ignored) {} + } catch (SQLException ignored) { + } try { if (pstmt != null) { pstmt.close(); } - } catch (SQLException ignored) {} + } catch (SQLException ignored) { + } try { if (conn != null) { conn.close(); } - } catch (SQLException ignored) {} + } catch (SQLException ignored) { + } } } diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index 52a0d30a17..b21f1ac358 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -4,6 +4,9 @@ import org.slf4j.LoggerFactory; import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; public class JdbcTemplate { @@ -14,4 +17,38 @@ public class JdbcTemplate { public JdbcTemplate(final DataSource dataSource) { this.dataSource = dataSource; } + + public void update(String sql, Object... args) { + Connection conn = null; + PreparedStatement pstmt = null; + try { + conn = dataSource.getConnection(); + pstmt = conn.prepareStatement(sql); + + log.debug("query : {}", sql); + setValues(pstmt, args); + 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) {} + } + } + + private void setValues(PreparedStatement pstmt, Object... args) throws SQLException { + for (int i = 0; i < args.length; i++) { + pstmt.setObject(i + 1, args[i]); + } + } } From 85ea23d5ab84404ed51f6b7238d496441cdd7a6b Mon Sep 17 00:00:00 2001 From: drunkenhw Date: Tue, 26 Sep 2023 15:42:20 +0900 Subject: [PATCH 2/5] =?UTF-8?q?refactor:=20row=20mapper=EB=A5=BC=20?= =?UTF-8?q?=ED=86=B5=ED=95=B4=20=EC=B0=BE=EB=8F=84=EB=A1=9D=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/dao/UserDao.java | 57 +++---------------- .../jdbc/core/JdbcTemplate.java | 50 +++++++++++++++- .../springframework/jdbc/core/RowMapper.java | 9 +++ 3 files changed, 65 insertions(+), 51 deletions(-) create mode 100644 jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index e92577e99a..5f3097a0e3 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -4,12 +4,9 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +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.List; public class UserDao { @@ -45,54 +42,16 @@ public List findAll() { 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) { - } + return jdbcTemplate.queryForObject(sql, userRowMapper(), id); + } - try { - if (pstmt != null) { - pstmt.close(); - } - } catch (SQLException ignored) { - } + public User findByAccount(final String account) { + final var sql = "select id, account, password, email from users where account = ?"; + return jdbcTemplate.queryForObject(sql, userRowMapper(), account); - try { - if (conn != null) { - conn.close(); - } - } catch (SQLException ignored) { - } - } } - public User findByAccount(final String account) { - // todo - return null; + private RowMapper userRowMapper() { + return rs -> new User(rs.getLong(1), rs.getString(2), rs.getString(3), rs.getString(4)); } } diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index b21f1ac358..34702cab34 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -6,6 +6,7 @@ import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; public class JdbcTemplate { @@ -36,13 +37,15 @@ public void update(String sql, Object... args) { if (pstmt != null) { pstmt.close(); } - } catch (SQLException ignored) {} + } catch (SQLException ignored) { + } try { if (conn != null) { conn.close(); } - } catch (SQLException ignored) {} + } catch (SQLException ignored) { + } } } @@ -51,4 +54,47 @@ private void setValues(PreparedStatement pstmt, Object... args) throws SQLExcept pstmt.setObject(i + 1, args[i]); } } + + public T queryForObject(String sql, RowMapper rowMapper, Object... args) { + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + conn = dataSource.getConnection(); + pstmt = conn.prepareStatement(sql); + setValues(pstmt, args); + rs = pstmt.executeQuery(); + + log.debug("query : {}", sql); + + if (rs.next()) { + return rowMapper.mapRow(rs); + } + 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) { + } + } + } } diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java b/jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java new file mode 100644 index 0000000000..b761868d36 --- /dev/null +++ b/jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java @@ -0,0 +1,9 @@ +package org.springframework.jdbc.core; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public interface RowMapper { + + T mapRow(ResultSet rs) throws SQLException; +} From e64d0d50cdcd55aaed0fdc058d6cacb62fe9f1de Mon Sep 17 00:00:00 2001 From: drunkenhw Date: Tue, 26 Sep 2023 16:00:59 +0900 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20=EC=97=AC=EB=9F=AC=EA=B0=9C?= =?UTF-8?q?=EB=A5=BC=20=EB=B0=98=ED=99=98=ED=95=98=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jdbc/core/JdbcTemplate.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index 34702cab34..60786b5bf1 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -8,6 +8,8 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; public class JdbcTemplate { @@ -97,4 +99,47 @@ public T queryForObject(String sql, RowMapper rowMapper, Object... args) } } } + + public List query(String sql, RowMapper rowMapper) { + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + conn = dataSource.getConnection(); + pstmt = conn.prepareStatement(sql); + setValues(pstmt); + rs = pstmt.executeQuery(); + + log.debug("query : {}", sql); + List results = new ArrayList<>(); + if (rs.next()) { + results.add(rowMapper.mapRow(rs)); + } + return results; + } 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) { + } + } + } } From 2e932471782ff5039d7eb73732ee01c62f412147 Mon Sep 17 00:00:00 2001 From: drunkenhw Date: Tue, 26 Sep 2023 16:01:29 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20update=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/dao/UserDao.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index 5f3097a0e3..0e574bea53 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,9 +1,9 @@ package com.techcourse.dao; import com.techcourse.domain.User; -import org.springframework.jdbc.core.JdbcTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import javax.sql.DataSource; @@ -12,17 +12,15 @@ public class UserDao { private static final Logger log = LoggerFactory.getLogger(UserDao.class); + private static final RowMapper USER_ROW_MAPPER = rs -> new User(rs.getLong(1), rs.getString(2), rs.getString(3), rs.getString(4)); - 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 = null; this.jdbcTemplate = jdbcTemplate; } @@ -32,26 +30,23 @@ public void insert(final User user) { } 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 findAll() { - // todo - return null; + final var sql = "select id, account, password, email from users"; + return jdbcTemplate.query(sql, USER_ROW_MAPPER); } public User findById(final Long id) { final var sql = "select id, account, password, email from users where id = ?"; - return jdbcTemplate.queryForObject(sql, userRowMapper(), id); + return jdbcTemplate.queryForObject(sql, USER_ROW_MAPPER, id); } public User findByAccount(final String account) { final var sql = "select id, account, password, email from users where account = ?"; - return jdbcTemplate.queryForObject(sql, userRowMapper(), account); - - } + return jdbcTemplate.queryForObject(sql, USER_ROW_MAPPER, account); - private RowMapper userRowMapper() { - return rs -> new User(rs.getLong(1), rs.getString(2), rs.getString(3), rs.getString(4)); } } From 45c3b52061de0ce381b7e9ab2a58e8654e172d96 Mon Sep 17 00:00:00 2001 From: drunkenhw Date: Tue, 26 Sep 2023 16:38:05 +0900 Subject: [PATCH 5/5] =?UTF-8?q?fix:=20while=20=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/springframework/jdbc/core/JdbcTemplate.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index 60786b5bf1..2565311650 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -112,7 +112,7 @@ public List query(String sql, RowMapper rowMapper) { log.debug("query : {}", sql); List results = new ArrayList<>(); - if (rs.next()) { + while (rs.next()) { results.add(rowMapper.mapRow(rs)); } return results;