Skip to content

Commit

Permalink
refactor: JdbcTemplate의 반복을 줄이고,dao에서 커넥션을 보내지 않도록 수정함
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeost committed Oct 11, 2023
1 parent 2347c91 commit 7ff7f19
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 43 deletions.
4 changes: 1 addition & 3 deletions app/src/main/java/com/techcourse/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.techcourse.dao;

import com.techcourse.config.DataSourceConfig;
import com.techcourse.domain.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.DataSourceUtils;

import java.util.List;

Expand Down Expand Up @@ -35,7 +33,7 @@ public void update(final User user) {
final var sql = "update users set account = ? , password = ? , email = ? where id = ?";
final Object[] parameters = {user.getAccount(), user.getPassword(), user.getEmail(), user.getId()};

jdbcTemplate.update(DataSourceUtils.getConnection(DataSourceConfig.getInstance()), sql, parameters);
jdbcTemplate.update(sql, parameters);
}

public List<User> findAll() {
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/java/com/techcourse/dao/UserHistoryDao.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.techcourse.dao;

import com.techcourse.config.DataSourceConfig;
import com.techcourse.domain.UserHistory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;

public class UserHistoryDao {

Expand All @@ -17,6 +15,6 @@ public void log(final UserHistory userHistory) {
final var sql = "insert into user_history (user_id, account, password, email, created_at, created_by) values (?, ?, ?, ?, ?, ?)";
final Object[] parameters = {userHistory.getUserId(), userHistory.getAccount(), userHistory.getPassword(), userHistory.getEmail(), userHistory.getCreatedAt(), userHistory.getCreateBy()};

jdbcTemplate.update(DataSourceUtils.getConnection(DataSourceConfig.getInstance()), sql, parameters);
jdbcTemplate.update(sql, parameters);
}
}
55 changes: 18 additions & 37 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceUtils;

import javax.sql.DataSource;
import java.sql.Connection;
Expand All @@ -23,57 +24,37 @@ public JdbcTemplate(final DataSource dataSource) {
}

public int update(final String sql, final Object... parameters) {
try (Connection conn = dataSource.getConnection();
PreparedStatement pstmt = PreparedStateUtil.makeStatement(conn, sql, parameters)
) {
log.debug("Execute Update - query : {}", sql);
return pstmt.executeUpdate();
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new DataAccessException(e);
}
return executeQuery(sql, PreparedStatement::executeUpdate, parameters);
}

public int update(final Connection conn, final String sql, final Object[] parameters) {
try (PreparedStatement pstmt = PreparedStateUtil.makeStatement(conn, sql, parameters)){
log.debug("Execute Update - query : {}", sql);
return pstmt.executeUpdate();
private <T> T executeQuery(final String sql, final PreparedStateExecutor<T> executor, final Object... parameters) {
final Connection conn = DataSourceUtils.getConnection(dataSource);
try (PreparedStatement pstmt = PreparedStateUtil.makeStatement(conn, sql, parameters)) {
log.debug("Execute - query : {}", sql);
return executor.execute(pstmt);
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new DataAccessException(e);
} finally {
DataSourceUtils.releaseConnection(conn, dataSource);
}
}

public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, final Object... parameters) {
try (Connection conn = dataSource.getConnection();
PreparedStatement pstmt = PreparedStateUtil.makeStatement(conn, sql, parameters);
ResultSet rs = pstmt.executeQuery()
) {
log.debug("Execute Query -query : {}", sql);
return SingleResult.makeSingleResultFrom(makeResults(rowMapper, rs));
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new DataAccessException(e);
}
return executeQuery(sql, preparedStatement -> SingleResult.makeSingleResultFrom(makeResults(rowMapper, preparedStatement)), parameters);
}

private <T> List<T> makeResults(final RowMapper<T> rowMapper, final ResultSet rs) throws SQLException {
private <T> List<T> makeResults(final RowMapper<T> rowMapper, final PreparedStatement preparedStatement) throws SQLException {
List<T> list = new ArrayList<>();
while (rs.next()) {
list.add(rowMapper.mapping(rs));
try (final ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
list.add(rowMapper.mapping(resultSet));
}
}
return list;
}

public <T> List<T> queryForList(final String sql, final RowMapper<T> rowMapper) {
try (Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery()
) {
log.debug("Execute Query - query : {}", sql);
return makeResults(rowMapper, rs);
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new DataAccessException(e);
}
public <T> List<T> queryForList(final String sql, final RowMapper<T> rowMapper, final Object... parameters) {
return executeQuery(sql, preparedStatement -> makeResults(rowMapper, preparedStatement), parameters);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.springframework.jdbc.core;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public interface PreparedStateExecutor<T> {

T execute(final PreparedStatement preparedStatement) throws SQLException;
}

0 comments on commit 7ff7f19

Please sign in to comment.