Skip to content

Commit

Permalink
feat: UserDao 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
kyY00n committed Sep 27, 2023
1 parent 06379e5 commit 2a60543
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# JDBC 라이브러리 구현하기

개발자는 SQL 쿼리 작성, 쿼리에 전달할 인자, SELECT 구문일 경우
조회 결과를 추출하는 것만 집중할 수 있도록 라이브러리를 만들자.

## 힌트

- 리팩터링은 UserDaoTest를 활용해 진행한다.
- 중복을 제거하기 위한 라이브러리는 JdbcTemplate 클래스에 구현한다.
- DataSource는 DataSourceConfig 클래스의 getInstance() 메서드를 호출하면 된다.
59 changes: 44 additions & 15 deletions app/src/main/java/com/techcourse/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.techcourse.dao;

import com.techcourse.domain.User;
import java.util.ArrayList;
import org.springframework.jdbc.core.JdbcTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -11,6 +12,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;

public class UserDao {

Expand All @@ -28,7 +30,10 @@ public UserDao(final JdbcTemplate jdbcTemplate) {

public void insert(final User user) {
final var sql = "insert into users (account, password, email) values (?, ?, ?)";
execute(sql, user.getAccount(), user.getPassword(), user.getEmail());
}

private void execute(String sql, Object... parameters) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Expand All @@ -37,9 +42,10 @@ public void insert(final User user) {

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

pstmt.setString(1, user.getAccount());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getEmail());
for (int i = 0; i < parameters.length; i++) {
pstmt.setObject(i + 1, parameters[i]);
}

pstmt.executeUpdate();
} catch (SQLException e) {
log.error(e.getMessage(), e);
Expand All @@ -60,36 +66,56 @@ public void insert(final User user) {
}

public void update(final User user) {
// todo
final var sql = "update users set password = ?, email = ?, account = ? where id = ?";
execute(sql, user.getPassword(), user.getEmail(), user.getAccount(), user.getId());
}

public List<User> findAll() {
// todo
return null;
final var sql = "select id, account, password, email from users";
return query(sql, (rs, rowNum) -> new User(rs.getLong("id"),
rs.getString("account"),
rs.getString("password"),
rs.getString("email")));
}

public User findById(final Long id) {
final var sql = "select id, account, password, email from users where id = ?";
return queryForObject(sql, (rs, rowNum) -> new User(rs.getLong("id"),
rs.getString("account"),
rs.getString("password"),
rs.getString("email")), id);
}

private <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... params) {
List<T> query = query(sql, rowMapper, params);
if (query.size() > 1) {
throw new RuntimeException("too many result");
}
if (query.isEmpty()) {
return null;
}
return query.get(0);
}

private <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... parameters) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setLong(1, id);
for (int i = 0; i < parameters.length; i++) {
pstmt.setObject(i + 1, parameters[i]);
}
rs = pstmt.executeQuery();

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

final List<T> results = new ArrayList<>();
if (rs.next()) {
return new User(
rs.getLong(1),
rs.getString(2),
rs.getString(3),
rs.getString(4));
results.add(rowMapper.mapRow(rs, 0));
}
return null;
return results;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
Expand All @@ -115,7 +141,10 @@ public User findById(final Long id) {
}

public User findByAccount(final String account) {
// todo
return null;
final var sql = "select id, account, password, email from users where account = ?";
return queryForObject(sql, (rs, rowNum) -> new User(rs.getLong("id"),
rs.getString("account"),
rs.getString("password"),
rs.getString("email")), account);
}
}

0 comments on commit 2a60543

Please sign in to comment.