Skip to content

Commit

Permalink
feat: UserDao 투두 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
somsom13 committed Sep 28, 2023
1 parent cd9c89a commit 8b660b8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
### 1단계
- [x] JdbcTemplate insert, update, delete, select 쿼리 메서드 구현
- [x] UserDao rowmapper 구현
- [ ] UserDao todo 쿼리 로직 구현
- [x] UserDao todo 쿼리 로직 구현
10 changes: 4 additions & 6 deletions app/src/main/java/com/techcourse/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

import com.techcourse.domain.User;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class UserDao {

private static final Logger log = LoggerFactory.getLogger(UserDao.class);
private static final RowMapper<User> ROW_MAPPER = (rs, count) ->
new User(rs.getLong("id"),
new User(
rs.getLong("id"),
rs.getString("account"),
rs.getString("password"),
rs.getString("email")
Expand All @@ -24,7 +22,7 @@ public UserDao(final JdbcTemplate jdbcTemplate) {
}

public void insert(final User user) {
final var sql = "insert into users (account, password, email) values (?, ?, ?)";
final String sql = "insert into users (account, password, email) values (?, ?, ?)";

jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail());
}
Expand All @@ -42,7 +40,7 @@ public List<User> findAll() {
}

public User findById(final Long id) {
final var sql = "select id, account, password, email from users where id = ?";
final String sql = "select id, account, password, email from users where id = ?";

return jdbcTemplate.queryForObject(sql, ROW_MAPPER, id);
}
Expand Down
31 changes: 15 additions & 16 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DataSourceUtils;

public class JdbcTemplate {

Expand All @@ -22,35 +21,38 @@ public JdbcTemplate(final DataSource dataSource) {
}

public <T> List<T> query(final String sql, final RowMapper<T> rowMapper, final Object... args) {
try (final Connection connection = DataSourceUtils.getConnection(dataSource);
try (final Connection connection = dataSource.getConnection();
final PreparedStatement pstmt = connection.prepareStatement(sql)) {
log.debug("query : {}", sql);

final Object[] parameters = args;
for (int i = 0; i < parameters.length; i++) {
pstmt.setObject(i + 1, parameters[i]);
}
setParamsToPreparedStatement(pstmt, args);

final ResultSet resultSet = pstmt.executeQuery();
final List<T> results = new ArrayList<>();
if (resultSet.next()) {
results.add(rowMapper.mapRow(resultSet, resultSet.getRow()));
}

return results;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

private void setParamsToPreparedStatement(final PreparedStatement pstmt, final Object[] args)
throws SQLException
{
for (int i = 0; i < args.length; i++) {
pstmt.setObject(i + 1, args[i]);
}
}

public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, final Object... args) {
try (final Connection connection = DataSourceUtils.getConnection(dataSource);
try (final Connection connection = dataSource.getConnection();
final PreparedStatement pstmt = connection.prepareStatement(sql)) {
log.debug("query : {}", sql);

final Object[] parameters = args;
for (int i = 0; i < parameters.length; i++) {
pstmt.setObject(i + 1, parameters[i]);
}
setParamsToPreparedStatement(pstmt, args);

final ResultSet resultSet = pstmt.executeQuery();
if (resultSet.next()) {
Expand All @@ -63,14 +65,11 @@ public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, fina
}

public int update(final String sql, final Object... args) {
try (final Connection connection = DataSourceUtils.getConnection(dataSource);
try (final Connection connection = dataSource.getConnection();
final PreparedStatement pstmt = connection.prepareStatement(sql)) {
log.debug("query : {}", sql);

final Object[] parameters = args;
for (int i = 0; i < parameters.length; i++) {
pstmt.setObject(i + 1, parameters[i]);
}
setParamsToPreparedStatement(pstmt, args);

return pstmt.executeUpdate();
} catch (SQLException e) {
Expand Down

0 comments on commit 8b660b8

Please sign in to comment.