From 79347b41bfb50e756399cf6885713662dc445abc Mon Sep 17 00:00:00 2001 From: jjongwa Date: Sun, 1 Oct 2023 10:49:58 +0900 Subject: [PATCH 1/7] =?UTF-8?q?refactor:=20`@FunctionalInterface`=20?= =?UTF-8?q?=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java | 1 + 1 file changed, 1 insertion(+) diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java b/jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java index 39b9b9751a..3afd9b9dea 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java @@ -3,6 +3,7 @@ import java.sql.ResultSet; import java.sql.SQLException; +@FunctionalInterface public interface RowMapper { T mapRow(final ResultSet rs) throws SQLException; From d86a650973cdf6b3934c0c09f35edec2cfb308e9 Mon Sep 17 00:00:00 2001 From: jjongwa Date: Sun, 1 Oct 2023 10:51:03 +0900 Subject: [PATCH 2/7] =?UTF-8?q?refactor:=20=EC=A4=91=EB=B3=B5=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EC=A0=9C=EA=B1=B0=20=EB=B0=8F=20ResultSet?= =?UTF-8?q?=20=EB=A6=AC=EC=86=8C=EC=8A=A4=20=EB=B0=98=EB=82=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jdbc/core/JdbcTemplate.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) 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 3ee08eeb83..a714868a88 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -23,10 +23,7 @@ public JdbcTemplate(final DataSource dataSource) { public void execute(final String sql, final Object... objects) { try (final Connection conn = dataSource.getConnection(); - final PreparedStatement pstmt = conn.prepareStatement(sql) - ) { - log.debug("query : {}", sql); - setValues(pstmt, objects); + final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects)) { pstmt.executeUpdate(); } catch (SQLException e) { log.error(e.getMessage(), e); @@ -36,11 +33,8 @@ public void execute(final String sql, final Object... objects) { public List query(final String sql, final RowMapper rowMapper, final Object... objects) { try (final Connection conn = dataSource.getConnection(); - final PreparedStatement pstmt = conn.prepareStatement(sql) + final ResultSet rs = preparedStatementAndSetValue(conn, sql, objects).executeQuery() ) { - log.debug("query : {}", sql); - setValues(pstmt, objects); - final ResultSet rs = pstmt.executeQuery(); final List list = new ArrayList<>(); while (rs.next()) { list.add(rowMapper.mapRow(rs)); @@ -55,11 +49,8 @@ public List query(final String sql, final RowMapper rowMapper, final O @Nullable public T queryForObject(final String sql, final RowMapper rowMapper, final Object... objects) { try (final Connection conn = dataSource.getConnection(); - final PreparedStatement pstmt = conn.prepareStatement(sql) + final ResultSet rs = preparedStatementAndSetValue(conn, sql, objects).executeQuery() ) { - log.debug("query : {}", sql); - setValues(pstmt, objects); - final ResultSet rs = pstmt.executeQuery(); if (rs.next()) { return rowMapper.mapRow(rs); } @@ -70,6 +61,17 @@ public T queryForObject(final String sql, final RowMapper rowMapper, fina } } + private PreparedStatement preparedStatementAndSetValue( + final Connection conn, + final String sql, + final Object... objects + ) throws SQLException { + final PreparedStatement pstmt = conn.prepareStatement(sql); + log.debug("query : {}", sql); + setValues(pstmt, objects); + return pstmt; + } + private void setValues(final PreparedStatement pstmt, Object... objects) throws SQLException { for (int i = 0; i < objects.length; i++) { pstmt.setObject(i + 1, objects[i]); From 8e850d8cc6091c8c5f6a70987065a8a2ab12008f Mon Sep 17 00:00:00 2001 From: jjongwa Date: Sun, 1 Oct 2023 13:36:01 +0900 Subject: [PATCH 3/7] =?UTF-8?q?refactor:=20final=20=EC=B6=94=EA=B0=80?= 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 a714868a88..9dc6934831 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -72,7 +72,7 @@ private PreparedStatement preparedStatementAndSetValue( return pstmt; } - private void setValues(final PreparedStatement pstmt, Object... objects) throws SQLException { + private void setValues(final PreparedStatement pstmt, final Object... objects) throws SQLException { for (int i = 0; i < objects.length; i++) { pstmt.setObject(i + 1, objects[i]); } From d02986ba355d75e7527dc96c37e503ecf4e9ff64 Mon Sep 17 00:00:00 2001 From: jjongwa Date: Sun, 1 Oct 2023 13:44:49 +0900 Subject: [PATCH 4/7] =?UTF-8?q?refactor:=20exception=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springframework/jdbc/core/JdbcTemplate.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) 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 9dc6934831..829d83fc01 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -10,6 +10,7 @@ import javax.sql.DataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.dao.DataAccessException; public class JdbcTemplate { @@ -23,26 +24,24 @@ public JdbcTemplate(final DataSource dataSource) { public void execute(final String sql, final Object... objects) { try (final Connection conn = dataSource.getConnection(); - final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects)) { + final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects) + ) { pstmt.executeUpdate(); } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new RuntimeException(e); + throw new DataAccessException(e.getMessage()); } } public List query(final String sql, final RowMapper rowMapper, final Object... objects) { try (final Connection conn = dataSource.getConnection(); - final ResultSet rs = preparedStatementAndSetValue(conn, sql, objects).executeQuery() - ) { + final ResultSet rs = preparedStatementAndSetValue(conn, sql, objects).executeQuery()) { final List list = new ArrayList<>(); while (rs.next()) { list.add(rowMapper.mapRow(rs)); } return list; } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new RuntimeException(e); + throw new DataAccessException(e.getMessage()); } } @@ -56,8 +55,7 @@ public T queryForObject(final String sql, final RowMapper rowMapper, fina } return null; } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new RuntimeException(e); + throw new DataAccessException(e.getMessage()); } } From cf28768dbdaedcdc1feea2d534bb16ee0a5d1e70 Mon Sep 17 00:00:00 2001 From: jjongwa Date: Sun, 1 Oct 2023 13:47:48 +0900 Subject: [PATCH 5/7] =?UTF-8?q?refactor:=20=EC=A4=84=EB=B0=94=EA=BF=88=20?= =?UTF-8?q?=EB=8F=99=EC=9D=BC=ED=95=98=EA=B2=8C=20=EB=B3=80=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 | 3 ++- 1 file changed, 2 insertions(+), 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 829d83fc01..862c043245 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -34,7 +34,8 @@ public void execute(final String sql, final Object... objects) { public List query(final String sql, final RowMapper rowMapper, final Object... objects) { try (final Connection conn = dataSource.getConnection(); - final ResultSet rs = preparedStatementAndSetValue(conn, sql, objects).executeQuery()) { + final ResultSet rs = preparedStatementAndSetValue(conn, sql, objects).executeQuery() + ) { final List list = new ArrayList<>(); while (rs.next()) { list.add(rowMapper.mapRow(rs)); From c62edf6b3eb584483873c5a6b0d273f44d10847b Mon Sep 17 00:00:00 2001 From: jjongwa Date: Thu, 5 Oct 2023 10:50:07 +0900 Subject: [PATCH 6/7] =?UTF-8?q?refactor:=20=EB=A6=AC=EB=B7=B0=20=EB=B0=98?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/springframework/jdbc/core/JdbcTemplate.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) 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 862c043245..6c7bd21e76 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -28,35 +28,37 @@ public void execute(final String sql, final Object... objects) { ) { pstmt.executeUpdate(); } catch (SQLException e) { - throw new DataAccessException(e.getMessage()); + throw new DataAccessException(e); } } public List query(final String sql, final RowMapper rowMapper, final Object... objects) { try (final Connection conn = dataSource.getConnection(); - final ResultSet rs = preparedStatementAndSetValue(conn, sql, objects).executeQuery() + final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects) ) { + final ResultSet rs = pstmt.executeQuery(); final List list = new ArrayList<>(); while (rs.next()) { list.add(rowMapper.mapRow(rs)); } return list; } catch (SQLException e) { - throw new DataAccessException(e.getMessage()); + throw new DataAccessException(e); } } @Nullable public T queryForObject(final String sql, final RowMapper rowMapper, final Object... objects) { try (final Connection conn = dataSource.getConnection(); - final ResultSet rs = preparedStatementAndSetValue(conn, sql, objects).executeQuery() + final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects) ) { + final ResultSet rs = pstmt.executeQuery(); if (rs.next()) { return rowMapper.mapRow(rs); } return null; } catch (SQLException e) { - throw new DataAccessException(e.getMessage()); + throw new DataAccessException(e); } } From d3858bd2ceef0412a0fe21ee0c55d894f834d019 Mon Sep 17 00:00:00 2001 From: jjongwa Date: Thu, 5 Oct 2023 12:08:26 +0900 Subject: [PATCH 7/7] =?UTF-8?q?refactor:=20=ED=85=9C=ED=94=8C=EB=A6=BF=20?= =?UTF-8?q?=EC=BD=9C=EB=B0=B1=20=ED=8C=A8=ED=84=B4=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jdbc/core/AutoClosableTemplate.java | 81 +++++++++++++++++++ .../jdbc/core/JdbcTemplate.java | 74 ++++------------- 2 files changed, 97 insertions(+), 58 deletions(-) create mode 100644 jdbc/src/main/java/org/springframework/jdbc/core/AutoClosableTemplate.java diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/AutoClosableTemplate.java b/jdbc/src/main/java/org/springframework/jdbc/core/AutoClosableTemplate.java new file mode 100644 index 0000000000..c963fd58f0 --- /dev/null +++ b/jdbc/src/main/java/org/springframework/jdbc/core/AutoClosableTemplate.java @@ -0,0 +1,81 @@ +package org.springframework.jdbc.core; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; +import javax.annotation.Nullable; +import javax.sql.DataSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.dao.DataAccessException; + +abstract class AutoClosableTemplate { + + private static final Logger log = LoggerFactory.getLogger(AutoClosableTemplate.class); + + private final DataSource dataSource; + + AutoClosableTemplate(final DataSource dataSource) { + this.dataSource = dataSource; + } + + public void execute(final String sql, final Object... objects) { + try (final Connection conn = dataSource.getConnection(); + final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects) + ) { + commandQuery(pstmt); + } catch (SQLException e) { + throw new DataAccessException(e); + } + } + + public List query(final String sql, final RowMapper rowMapper, final Object... objects) { + try (final Connection conn = dataSource.getConnection(); + final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects) + ) { + final ResultSet rs = pstmt.executeQuery(); + return queryAll(rs, rowMapper); + } catch (SQLException e) { + throw new DataAccessException(e); + } + } + + @Nullable + public T queryForObject(final String sql, final RowMapper rowMapper, final Object... objects) { + try (final Connection conn = dataSource.getConnection(); + final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects) + ) { + final ResultSet rs = pstmt.executeQuery(); + return queryForOne(rs, rowMapper); + } catch (SQLException e) { + throw new DataAccessException(e); + } + } + + private PreparedStatement preparedStatementAndSetValue( + final Connection conn, + final String sql, + final Object... objects + ) throws SQLException { + final PreparedStatement pstmt = conn.prepareStatement(sql); + log.debug("query : {}", sql); + setValues(pstmt, objects); + return pstmt; + } + + private void setValues(final PreparedStatement pstmt, final Object... objects) throws SQLException { + for (int i = 0; i < objects.length; i++) { + pstmt.setObject(i + 1, objects[i]); + } + } + + protected abstract void commandQuery(final PreparedStatement pstmt) throws SQLException; + + protected abstract List queryAll(final ResultSet rs, final RowMapper rowMapper) throws SQLException; + + @Nullable + protected abstract T queryForOne(final ResultSet rs, final RowMapper rowMapper) + throws SQLException; +} 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 6c7bd21e76..4932bbdac7 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -1,6 +1,5 @@ package org.springframework.jdbc.core; -import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -8,74 +7,33 @@ import java.util.List; import javax.annotation.Nullable; import javax.sql.DataSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.dao.DataAccessException; -public class JdbcTemplate { - - private static final Logger log = LoggerFactory.getLogger(JdbcTemplate.class); - - private final DataSource dataSource; +public class JdbcTemplate extends AutoClosableTemplate { public JdbcTemplate(final DataSource dataSource) { - this.dataSource = dataSource; + super(dataSource); } - public void execute(final String sql, final Object... objects) { - try (final Connection conn = dataSource.getConnection(); - final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects) - ) { - pstmt.executeUpdate(); - } catch (SQLException e) { - throw new DataAccessException(e); - } + @Override + protected void commandQuery(final PreparedStatement pstmt) throws SQLException { + pstmt.executeUpdate(); } - public List query(final String sql, final RowMapper rowMapper, final Object... objects) { - try (final Connection conn = dataSource.getConnection(); - final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects) - ) { - final ResultSet rs = pstmt.executeQuery(); - final List list = new ArrayList<>(); - while (rs.next()) { - list.add(rowMapper.mapRow(rs)); - } - return list; - } catch (SQLException e) { - throw new DataAccessException(e); + @Override + protected List queryAll(final ResultSet rs, final RowMapper rowMapper) throws SQLException { + final List list = new ArrayList<>(); + while (rs.next()) { + list.add(rowMapper.mapRow(rs)); } + return list; } @Nullable - public T queryForObject(final String sql, final RowMapper rowMapper, final Object... objects) { - try (final Connection conn = dataSource.getConnection(); - final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects) - ) { - final ResultSet rs = pstmt.executeQuery(); - if (rs.next()) { - return rowMapper.mapRow(rs); - } - return null; - } catch (SQLException e) { - throw new DataAccessException(e); - } - } - - private PreparedStatement preparedStatementAndSetValue( - final Connection conn, - final String sql, - final Object... objects - ) throws SQLException { - final PreparedStatement pstmt = conn.prepareStatement(sql); - log.debug("query : {}", sql); - setValues(pstmt, objects); - return pstmt; - } - - private void setValues(final PreparedStatement pstmt, final Object... objects) throws SQLException { - for (int i = 0; i < objects.length; i++) { - pstmt.setObject(i + 1, objects[i]); + @Override + protected T queryForOne(final ResultSet rs, final RowMapper rowMapper) throws SQLException { + if (rs.next()) { + return rowMapper.mapRow(rs); } + return null; } }