Skip to content

Commit

Permalink
feat: queryForObject의 where 절 다중 조건 대응 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
junpakPark committed Sep 26, 2023
1 parent c9f4461 commit c8fabc9
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,26 @@ public JdbcTemplate(final DataSource dataSource) {
this.dataSource = dataSource;
}

public void update(final String sql, final Object... parameters) {
public void update(final String sql, final Object... args) {
try (
Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)
) {
log.debug(QUERY_FORMAT, sql);

for (int i = 1; i <= parameters.length; i++) {
preparedStatement.setObject(i, parameters[i - 1]);
}
bindStatementWithArgs(args, preparedStatement);
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new DataAccessException(e);
}
}

public <T> Optional<T> queryForObject(final String sql, final RowMapper<T> rowMapper, final Object id) {
public <T> Optional<T> queryForObject(final String sql, final RowMapper<T> rowMapper, final Object... args) {
try (
Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)
) {
preparedStatement.setObject(1, id);
bindStatementWithArgs(args, preparedStatement);
ResultSet resultSet = preparedStatement.executeQuery();

log.debug(QUERY_FORMAT, sql);
Expand All @@ -64,6 +62,12 @@ public <T> Optional<T> queryForObject(final String sql, final RowMapper<T> rowMa
}
}

private void bindStatementWithArgs(Object[] args, PreparedStatement preparedStatement) throws SQLException {
for (int i = 1; i <= args.length; i++) {
preparedStatement.setObject(i, args[i - 1]);
}
}

public <T> List<T> query(final String sql, final RowMapper<T> rowMapper) {
try (
Connection connection = dataSource.getConnection();
Expand Down

0 comments on commit c8fabc9

Please sign in to comment.