Skip to content

Commit

Permalink
feat: userDao의 조회쿼리 부분 JdbcTemplate으로 리팩터링
Browse files Browse the repository at this point in the history
  • Loading branch information
hong-sile committed Sep 26, 2023
1 parent 7ffbb6f commit db3936e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 64 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
- [ ] JdbcTemplate 구현하기
- [x] 조회기능 구현
- [x] 쓰기기능 구현
- [ ] UserDao를 JdbcTemplate을 사용하도록 리팩터링
- [ ] findByAccount
- [ ] findById
- [ ] findAll
- [x] UserDao를 JdbcTemplate을 사용하도록 리팩터링
- [x] findByAccount
- [x] findById
- [x] findAll
- [x] update
- [x] insert
73 changes: 13 additions & 60 deletions app/src/main/java/com/techcourse/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
package com.techcourse.dao;

import com.techcourse.config.DataSourceConfig;
import com.techcourse.domain.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.Mapper;

public class UserDao {

private static final Mapper<User> USER_MAPPER = (rs) -> new User(
rs.getLong(1),
rs.getString(2),
rs.getString(3),
rs.getString(4)
);
private static final Logger log = LoggerFactory.getLogger(UserDao.class);

private final DataSource dataSource;
private final JdbcTemplate jdbcTemplate;

public UserDao(final DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

public UserDao(final JdbcTemplate jdbcTemplate) {
this.dataSource = DataSourceConfig.getInstance();
this.jdbcTemplate = jdbcTemplate;
}

Expand All @@ -44,70 +43,24 @@ public void update(final User user) {

public List<User> findAll() {
final var sql = "select id, account, password, email from users";

try (final Connection connection = dataSource.getConnection();
final PreparedStatement pstmt = connection.prepareStatement(sql)) {
final ResultSet rs = pstmt.executeQuery();

log.debug("query : {}", sql);
return jdbcTemplate.executeQuery(sql, (rs) -> {
final List<User> users = new ArrayList<>();
if (rs.next()) {
final User user = extractUser(rs);
users.add(user);
while (rs.next()) {
users.add(USER_MAPPER.map(rs));
}
return users;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
});
}

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

try (final Connection connection = dataSource.getConnection();
final PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setLong(1, id);
final ResultSet rs = pstmt.executeQuery();

log.debug("query : {}", sql);

if (rs.next()) {
return extractUser(rs);
}
return null;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
return jdbcTemplate.executeQuery(sql, USER_MAPPER, id);
}

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

try (final Connection connection = dataSource.getConnection();
final PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, account);
final ResultSet rs = pstmt.executeQuery();

log.debug("query : {}", sql);

if (rs.next()) {
return extractUser(rs);
}
return null;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}

private static User extractUser(final ResultSet rs) throws SQLException {
return new User(
rs.getLong(1),
rs.getString(2),
rs.getString(3),
rs.getString(4)
);
return jdbcTemplate.executeQuery(sql, USER_MAPPER, account);
}
}

0 comments on commit db3936e

Please sign in to comment.