From 7bbad7ef440d81e3629ea408a79c5b904133a2c1 Mon Sep 17 00:00:00 2001 From: thdwoqor Date: Tue, 26 Sep 2023 20:29:02 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20UserDao=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/dao/UserDao.java | 160 ++++++++++++++++-- 1 file changed, 145 insertions(+), 15 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..c63aa0e660 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,16 +1,16 @@ 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.ArrayList; import java.util.List; +import javax.sql.DataSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.jdbc.core.JdbcTemplate; public class UserDao { @@ -49,23 +49,106 @@ public void insert(final User user) { if (pstmt != null) { pstmt.close(); } - } catch (SQLException ignored) {} + } catch (SQLException ignored) { + } try { if (conn != null) { conn.close(); } - } catch (SQLException ignored) {} + } catch (SQLException ignored) { + } } } public void update(final User user) { - // todo + String sql = "update users set id = ?, account = ?, password = ?, email = ? where id = ?"; + Connection conn = null; + PreparedStatement pstmt = null; + + try { + conn = dataSource.getConnection(); + pstmt = conn.prepareStatement(sql); + + log.debug("query : {}", sql); + + pstmt.setLong(1,user.getId()); + pstmt.setString(2,user.getAccount()); + pstmt.setString(3,user.getPassword()); + pstmt.setString(4,user.getEmail()); + pstmt.setLong(5,user.getId()); + 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) { + } + } + + + } public List findAll() { - // todo - return null; + String sql = "select id, account, password, email from users"; + + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + List users = new ArrayList<>(); + + try { + conn = dataSource.getConnection(); + pstmt = conn.prepareStatement(sql); + rs = pstmt.executeQuery(); + + log.debug("query : {}", sql); + + while (rs.next()) { + users.add(new User( + rs.getLong(1), + rs.getString(2), + rs.getString(3), + rs.getString(4))); + } + return users; + } 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) { + } + } } public User findById(final Long id) { @@ -98,24 +181,71 @@ 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) { + } } } public User findByAccount(final String account) { - // todo - return null; + String 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) { + } + + try { + if (conn != null) { + conn.close(); + } + } catch (SQLException ignored) { + } + } } } From 362b083e01f0728c81fd2e5034a0ad69d8ead2a2 Mon Sep 17 00:00:00 2001 From: thdwoqor Date: Tue, 26 Sep 2023 22:12:59 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20JdbcTemplate=20query,=20queryForObj?= =?UTF-8?q?ect=20=EA=B5=AC=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 | 165 +++--------------- .../jdbc/core/JdbcTemplate.java | 97 +++++++++- .../springframework/jdbc/core/RowMapper.java | 10 ++ 3 files changed, 129 insertions(+), 143 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 c63aa0e660..827d889166 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -3,9 +3,7 @@ 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; @@ -17,13 +15,16 @@ 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) { @@ -72,11 +73,11 @@ public void update(final User user) { log.debug("query : {}", sql); - pstmt.setLong(1,user.getId()); - pstmt.setString(2,user.getAccount()); - pstmt.setString(3,user.getPassword()); - pstmt.setString(4,user.getEmail()); - pstmt.setLong(5,user.getId()); + pstmt.setLong(1, user.getId()); + pstmt.setString(2, user.getAccount()); + pstmt.setString(3, user.getPassword()); + pstmt.setString(4, user.getEmail()); + pstmt.setLong(5, user.getId()); pstmt.executeUpdate(); } catch (SQLException e) { log.error(e.getMessage(), e); @@ -98,154 +99,36 @@ public void update(final User user) { } - } public List findAll() { String sql = "select id, account, password, email from users"; - Connection conn = null; - PreparedStatement pstmt = null; - ResultSet rs = null; - List users = new ArrayList<>(); - - try { - conn = dataSource.getConnection(); - pstmt = conn.prepareStatement(sql); - rs = pstmt.executeQuery(); - - log.debug("query : {}", sql); - - while (rs.next()) { - users.add(new User( - rs.getLong(1), - rs.getString(2), - rs.getString(3), - rs.getString(4))); - } - return users; - } 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.query(sql, rs -> new User( + rs.getLong(1), + rs.getString(2), + rs.getString(3), + rs.getString(4))); } 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, rs -> new User( + rs.getLong(1), + rs.getString(2), + rs.getString(3), + rs.getString(4)), id); } public User findByAccount(final String account) { String 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) { - } - - try { - if (conn != null) { - conn.close(); - } - } catch (SQLException ignored) { - } - } + return jdbcTemplate.queryForObject(sql, rs -> new User( + rs.getLong("id"), + rs.getString("account"), + rs.getString("password"), + rs.getString("email") + ), account); } } 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..5e69340468 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -1,10 +1,15 @@ package org.springframework.jdbc.core; +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 javax.sql.DataSource; - public class JdbcTemplate { private static final Logger log = LoggerFactory.getLogger(JdbcTemplate.class); @@ -14,4 +19,92 @@ public class JdbcTemplate { public JdbcTemplate(final DataSource dataSource) { this.dataSource = dataSource; } + + 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); + + for (int i = 0; i < args.length; i++) { + pstmt.setObject(i + 1, args[i]); + } + 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) { + } + } + } + + public List query(String sql, RowMapper rowMapper) { + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + conn = dataSource.getConnection(); + pstmt = conn.prepareStatement(sql); + + rs = pstmt.executeQuery(); + + log.debug("query : {}", sql); + ArrayList objects = new ArrayList<>(); + while (rs.next()) { + objects.add(rowMapper.mapRow(rs)); + } + return objects; + } 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..a264f57230 --- /dev/null +++ b/jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java @@ -0,0 +1,10 @@ +package org.springframework.jdbc.core; + +import java.sql.ResultSet; +import java.sql.SQLException; + +@FunctionalInterface +public interface RowMapper { + + T mapRow(ResultSet rs) throws SQLException; +} From 8b0e7576a41654da0ca1c5ff0dfd605f9a919982 Mon Sep 17 00:00:00 2001 From: thdwoqor Date: Tue, 26 Sep 2023 23:31:49 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20JdbcTemplate=20update=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 | 79 +++---------------- .../jdbc/core/JdbcTemplate.java | 35 ++++++++ 2 files changed, 45 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index 827d889166..b31501fd1b 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,9 +1,6 @@ package com.techcourse.dao; import com.techcourse.domain.User; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; import java.util.List; import javax.sql.DataSource; import org.slf4j.Logger; @@ -28,77 +25,21 @@ public UserDao(final 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) { - } - } + String sql = "insert into users (account, password, email) values (?, ?, ?)"; + + jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail()); } public void update(final User user) { String sql = "update users set id = ?, account = ?, password = ?, email = ? where id = ?"; - Connection conn = null; - PreparedStatement pstmt = null; - - try { - conn = dataSource.getConnection(); - pstmt = conn.prepareStatement(sql); - - log.debug("query : {}", sql); - - pstmt.setLong(1, user.getId()); - pstmt.setString(2, user.getAccount()); - pstmt.setString(3, user.getPassword()); - pstmt.setString(4, user.getEmail()); - pstmt.setLong(5, user.getId()); - 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.getId(), + user.getAccount(), + user.getPassword(), + user.getEmail(), + user.getId() + ); } public List findAll() { 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 5e69340468..86efd404cf 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -107,4 +107,39 @@ public List query(String sql, RowMapper rowMapper) { } } } + + public int update(String sql, Object... args) { + Connection conn = null; + PreparedStatement pstmt = null; + + try { + conn = dataSource.getConnection(); + pstmt = conn.prepareStatement(sql); + + log.debug("query : {}", sql); + + for (int i = 0; i < args.length; i++) { + pstmt.setObject(i + 1, args[i]); + } + + return 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) { + } + } + } }