From 46d3655e6c4789d3f377e92d1dda59bcf0a41ad0 Mon Sep 17 00:00:00 2001 From: Kong-hana01 Date: Fri, 29 Sep 2023 23:30:05 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20final=20=ED=82=A4=EC=9B=8C=EB=93=9C?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++--- .../jdbc/core/JdbcTemplate.java | 23 +++++++++---------- .../jdbc/core/PreparedStatementSetter.java | 2 +- .../jdbc/core/ResultSetGetter.java | 10 ++++---- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 866be39652..05d7431c46 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # JDBC 라이브러리 구현하기 ## 1. JDBC 라이브러리 구현하기 -- [ ] SQL 쿼리 작성, 쿼리에 전달할 인자, 조회 결과를 추출하는 것에만 집중할 수 있도록 라이브러리 만들기 - - [ ] 중복제거를 위해 JdbcTemplate을 구현한다. - - [ ] 기존 Dao에서 활용하던 DataSource 대신 JdbcTemplate을 사용하도록 변경한다. +- [x] SQL 쿼리 작성, 쿼리에 전달할 인자, 조회 결과를 추출하는 것에만 집중할 수 있도록 라이브러리 만들기 + - [x] 중복제거를 위해 JdbcTemplate을 구현한다. + - [x] 기존 Dao에서 활용하던 DataSource 대신 JdbcTemplate을 사용하도록 변경한다. 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 e492d62bb8..ad398188fc 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -24,8 +24,8 @@ public JdbcTemplate(final DataSource dataSource) { public void execute(final String sql, final PreparedStatementSetter preparedStatementSetter) { log.debug("query : {}", sql); - try (Connection conn = dataSource.getConnection(); - PreparedStatement ps = conn.prepareStatement(sql)) { + try (final Connection conn = dataSource.getConnection(); + final PreparedStatement ps = conn.prepareStatement(sql)) { preparedStatementSetter.set(ps); ps.executeUpdate(); @@ -40,8 +40,8 @@ public T find(final String sql, final ResultSetGetter rsg) { log.debug("query : {}", sql); - try (Connection conn = dataSource.getConnection(); - PreparedStatement ps = conn.prepareStatement(sql)) { + try (final Connection conn = dataSource.getConnection(); + final PreparedStatement ps = conn.prepareStatement(sql)) { preparedStatementSetter.set(ps); return getObject(rsg, ps); @@ -52,7 +52,7 @@ public T find(final String sql, } private T getObject(ResultSetGetter rsg, PreparedStatement ps) throws SQLException { - try (ResultSet rs = ps.executeQuery()) { + try (final ResultSet rs = ps.executeQuery()) { if (rs.next()) { return rsg.getObject(rs); } @@ -60,12 +60,11 @@ private T getObject(ResultSetGetter rsg, PreparedStatement ps) throws SQL } } - public List findAll(final String sql, - final ResultSetGetter rsg) { + public List findAll(final String sql, final ResultSetGetter rsg) { log.debug("query : {}", sql); - try (Connection conn = dataSource.getConnection(); - PreparedStatement ps = conn.prepareStatement(sql)) { + try (final Connection conn = dataSource.getConnection(); + final PreparedStatement ps = conn.prepareStatement(sql)) { return getObjects(rsg, ps); } catch (SQLException e) { @@ -74,9 +73,9 @@ public List findAll(final String sql, } } - private List getObjects(ResultSetGetter rsg, PreparedStatement ps) throws SQLException { - try (ResultSet rs = ps.executeQuery()) { - List objects = new ArrayList<>(); + private List getObjects(final ResultSetGetter rsg, final PreparedStatement ps) throws SQLException { + try (final ResultSet rs = ps.executeQuery()) { + final List objects = new ArrayList<>(); while (rs.next()) { objects.add(rsg.getObject(rs)); } diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementSetter.java b/jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementSetter.java index 7e88526420..4fb23c1fd3 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementSetter.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementSetter.java @@ -17,7 +17,7 @@ public PreparedStatementSetter(final List values) { public void set(final PreparedStatement ps) throws SQLException { for (int i = 0; i < values.size(); i++) { - PreparedStatementTypeValue pstv = values.get(i); + final PreparedStatementTypeValue pstv = values.get(i); pstv.setPreparedStatement(ps, i+1); } } diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/ResultSetGetter.java b/jdbc/src/main/java/org/springframework/jdbc/core/ResultSetGetter.java index 30a0589df4..b8772d84e7 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/ResultSetGetter.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/ResultSetGetter.java @@ -22,7 +22,7 @@ public ResultSetGetter(final Map> values, final Class classF public T getObject(final ResultSet rs) throws SQLException { try { - T instance = classForObject.getConstructor().newInstance(); + final T instance = classForObject.getConstructor().newInstance(); setField(rs, instance); return instance; } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException | @@ -31,10 +31,10 @@ public T getObject(final ResultSet rs) throws SQLException { } } - private void setField(ResultSet rs, T instance) throws SQLException, NoSuchFieldException, IllegalAccessException { - for (Entry entry : values.entrySet()) { - Object value = getValue(rs, entry.getValue(), entry.getKey()); - Field field = instance.getClass().getDeclaredField(entry.getKey()); + private void setField(final ResultSet rs, final T instance) throws SQLException, NoSuchFieldException, IllegalAccessException { + for (final Entry entry : values.entrySet()) { + final Object value = getValue(rs, entry.getValue(), entry.getKey()); + final Field field = instance.getClass().getDeclaredField(entry.getKey()); field.setAccessible(true); field.set(instance, value); }