Skip to content

Commit

Permalink
refactor: checked 예외 변한처리 JdbcTemplate 담당
Browse files Browse the repository at this point in the history
  • Loading branch information
rawfishthelgh committed Oct 11, 2023
1 parent 940e754 commit a208a88
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 48 deletions.
37 changes: 7 additions & 30 deletions app/src/main/java/com/techcourse/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import com.techcourse.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.SQLException;
import java.util.List;
import java.util.NoSuchElementException;

Expand All @@ -23,52 +21,31 @@ public UserDao(final JdbcTemplate jdbcTemplate) {

public void insert(final User user) {
final var sql = "insert into users (account, password, email) values (?, ?, ?)";
try {
jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail());
} catch (SQLException e) {
throw new DataAccessException(e);
}
jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail());
}

public void update(final User user) {
String sql = "UPDATE users SET account = ?, password = ?, email = ? WHERE id = ?";
try {
jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail(), user.getId());
} catch (SQLException e) {
throw new DataAccessException(e) {
};
}
jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail(), user.getId());

}

public List<User> findAll() {
String sql = "SELECT id, account, password, email FROM users";
try {
return jdbcTemplate.query(sql, getUserRowMapper());
} catch (SQLException e) {
throw new DataAccessException(e);
}
return jdbcTemplate.query(sql, getUserRowMapper());
}


public User findById(final Long id) {
final var sql = "select id, account, password, email from users where id = ?";
try {
return jdbcTemplate.queryForObject(sql, getUserRowMapper(), id)
.orElseThrow(() -> new NoSuchElementException("결과가 존재하지 않습니다"));
} catch (SQLException e) {
throw new DataAccessException(e);
}
return jdbcTemplate.queryForObject(sql, getUserRowMapper(), id)
.orElseThrow(() -> new NoSuchElementException("결과가 존재하지 않습니다"));
}

public User findByAccount(final String account) {
String sql = "SELECT id, account, password, email FROM users WHERE account = ?";
try {
return jdbcTemplate.queryForObject(sql, getUserRowMapper(), account)
.orElseThrow(() -> new NoSuchElementException("결과가 존재하지 않습니다"));
} catch (SQLException e) {
throw new DataAccessException(e);
}
return jdbcTemplate.queryForObject(sql, getUserRowMapper(), account)
.orElseThrow(() -> new NoSuchElementException("결과가 존재하지 않습니다"));
}

private RowMapper<User> getUserRowMapper() {
Expand Down
23 changes: 8 additions & 15 deletions app/src/main/java/com/techcourse/dao/UserHistoryDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import com.techcourse.domain.UserHistory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.SQLException;

public class UserHistoryDao {

private static final Logger log = LoggerFactory.getLogger(UserHistoryDao.class);
Expand All @@ -20,17 +17,13 @@ public UserHistoryDao(final JdbcTemplate jdbcTemplate) {

public void log(final UserHistory userHistory) {
final var sql = "insert into user_history (user_id, account, password, email, created_at, created_by) values (?, ?, ?, ?, ?, ?)";
try {
jdbcTemplate.update(sql,
userHistory.getUserId(),
userHistory.getAccount(),
userHistory.getPassword(),
userHistory.getEmail(),
userHistory.getCreatedAt(),
userHistory.getCreateBy()
);
} catch (SQLException e) {
throw new DataAccessException(e);
}
jdbcTemplate.update(sql,
userHistory.getUserId(),
userHistory.getAccount(),
userHistory.getPassword(),
userHistory.getEmail(),
userHistory.getCreatedAt(),
userHistory.getCreateBy()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ public JdbcTemplate(DataSource dataSource) {
this.dataSource = dataSource;
}

public void update(String sql, Object... arguments) throws SQLException {
public void update(String sql, Object... arguments) {
Connection con = DataSourceUtils.getConnection(dataSource);
try (PreparedStatement pstmt = con.prepareStatement(sql)) {
log.debug("query : {}", sql);
setArguments(pstmt, arguments);
pstmt.executeUpdate();
} catch (SQLException e) {
throw new DataAccessException(e);
}
}

public <T> Optional<T> queryForObject(String sql, RowMapper<T> rowMapper, Object... arguments) throws SQLException {
public <T> Optional<T> queryForObject(String sql, RowMapper<T> rowMapper, Object... arguments) {
Connection con = DataSourceUtils.getConnection(dataSource);
try (PreparedStatement pstmt = con.prepareStatement(sql)) {
setArguments(pstmt, arguments);
Expand All @@ -50,10 +52,12 @@ public <T> Optional<T> queryForObject(String sql, RowMapper<T> rowMapper, Object
}
throw new NoSuchElementException();
}, pstmt);
} catch (SQLException e) {
throw new DataAccessException(e);
}
}

public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... arguments) throws SQLException {
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... arguments) {
Connection con = DataSourceUtils.getConnection(dataSource);
try (PreparedStatement pstmt = con.prepareStatement(sql)) {
setArguments(pstmt, arguments);
Expand All @@ -66,6 +70,8 @@ public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... arguments
}
return results;
}, pstmt);
} catch (SQLException e) {
throw new DataAccessException(e);
}
}

Expand Down

0 comments on commit a208a88

Please sign in to comment.