-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f804ba
commit b77af74
Showing
4 changed files
with
164 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,54 @@ | ||
package com.techcourse.dao; | ||
|
||
import java.util.List; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.RowMapper; | ||
|
||
import com.techcourse.domain.User; | ||
import javax.sql.DataSource; | ||
import java.util.List; | ||
|
||
public class UserDao { | ||
|
||
private static final RowMapper<User> USER_ROW_MAPPER = resultSet -> | ||
new User( | ||
resultSet.getLong("id"), | ||
resultSet.getString("account"), | ||
resultSet.getString("password"), | ||
resultSet.getString("email") | ||
); | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
|
||
public UserDao(final DataSource dataSource) { | ||
this.jdbcTemplate = new JdbcTemplate(dataSource); | ||
} | ||
|
||
public UserDao(final JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public void insert(final User user) { | ||
final var sql = "insert into users (account, password, email) values (?, ?, ?)"; | ||
jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail()); | ||
} | ||
|
||
public void update(final User user) { | ||
final var sql = "update users set account = ?, password = ?, email = ? where id = ?"; | ||
jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail(), user.getId()); | ||
} | ||
|
||
public List<User> findAll() { | ||
final var sql = "select id, account, password, email from users"; | ||
return jdbcTemplate.query(sql, USER_ROW_MAPPER); | ||
} | ||
|
||
public User findById(final Long id) { | ||
final var sql = "select id, account, password, email from users where id = ?"; | ||
return jdbcTemplate.queryForObject(sql, USER_ROW_MAPPER, id); | ||
} | ||
|
||
public User findByAccount(final String account) { | ||
final var sql = "select id, account, password, email from users where account = ?"; | ||
return jdbcTemplate.queryForObject(sql, USER_ROW_MAPPER, account); | ||
} | ||
private static final RowMapper<User> USER_ROW_MAPPER = resultSet -> | ||
new User( | ||
resultSet.getLong("id"), | ||
resultSet.getString("account"), | ||
resultSet.getString("password"), | ||
resultSet.getString("email") | ||
); | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
|
||
public UserDao(final DataSource dataSource) { | ||
this.jdbcTemplate = new JdbcTemplate(dataSource); | ||
} | ||
|
||
public UserDao(final JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public void insert(final User user) { | ||
final var sql = "insert into users (account, password, email) values (?, ?, ?)"; | ||
jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail()); | ||
} | ||
|
||
public void update(final User user) { | ||
final var sql = "update users set account = ?, password = ?, email = ? where id = ?"; | ||
jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail(), user.getId()); | ||
} | ||
|
||
public List<User> findAll() { | ||
final var sql = "select id, account, password, email from users"; | ||
return jdbcTemplate.query(sql, USER_ROW_MAPPER); | ||
} | ||
|
||
public User findById(final Long id) { | ||
final var sql = "select id, account, password, email from users where id = ?"; | ||
return jdbcTemplate.queryForObject(sql, USER_ROW_MAPPER, id); | ||
} | ||
|
||
public User findByAccount(final String account) { | ||
final var sql = "select id, account, password, email from users where account = ?"; | ||
return jdbcTemplate.queryForObject(sql, USER_ROW_MAPPER, account); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,72 @@ | ||
package com.techcourse.dao; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class UserDaoTest { | ||
|
||
private UserDao userDao; | ||
private UserDao userDao; | ||
|
||
@BeforeEach | ||
void setup() { | ||
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
@BeforeEach | ||
void setup() { | ||
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
|
||
userDao = new UserDao(DataSourceConfig.getInstance()); | ||
final var user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(user); | ||
} | ||
userDao = new UserDao(DataSourceConfig.getInstance()); | ||
final var user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(user); | ||
} | ||
|
||
@Test | ||
void findAll() { | ||
final var users = userDao.findAll(); | ||
@Test | ||
void findAll() { | ||
final var users = userDao.findAll(); | ||
|
||
assertThat(users).isNotEmpty(); | ||
} | ||
assertThat(users).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
void findById() { | ||
final var user = userDao.findById(1L); | ||
@Test | ||
void findById() { | ||
final var user = userDao.findById(1L); | ||
|
||
assertThat(user.getAccount()).isEqualTo("gugu"); | ||
} | ||
assertThat(user.getAccount()).isEqualTo("gugu"); | ||
} | ||
|
||
@Test | ||
void findByAccount() { | ||
final var juno = new User("juno", "password", "[email protected]"); | ||
userDao.insert(juno); | ||
@Test | ||
void findByAccount() { | ||
final var juno = new User("juno", "password", "[email protected]"); | ||
userDao.insert(juno); | ||
|
||
final var account = "juno"; | ||
final var user = userDao.findByAccount(account); | ||
final var account = "juno"; | ||
final var user = userDao.findByAccount(account); | ||
|
||
assertThat(user.getAccount()).isEqualTo(account); | ||
} | ||
assertThat(user.getAccount()).isEqualTo(account); | ||
} | ||
|
||
@Test | ||
void insert() { | ||
final var account = "insert-gugu"; | ||
final var user = new User(account, "password", "[email protected]"); | ||
userDao.insert(user); | ||
@Test | ||
void insert() { | ||
final var account = "insert-gugu"; | ||
final var user = new User(account, "password", "[email protected]"); | ||
userDao.insert(user); | ||
|
||
final var actual = userDao.findById(2L); | ||
final var actual = userDao.findById(2L); | ||
|
||
assertThat(actual.getAccount()).isEqualTo(account); | ||
} | ||
assertThat(actual.getAccount()).isEqualTo(account); | ||
} | ||
|
||
@Test | ||
void update() { | ||
final var newPassword = "password99"; | ||
final var user = userDao.findById(1L); | ||
user.changePassword(newPassword); | ||
@Test | ||
void update() { | ||
final var newPassword = "password99"; | ||
final var user = userDao.findById(1L); | ||
user.changePassword(newPassword); | ||
|
||
userDao.update(user); | ||
userDao.update(user); | ||
|
||
final var actual = userDao.findById(1L); | ||
final var actual = userDao.findById(1L); | ||
|
||
assertThat(actual.getPassword()).isEqualTo(newPassword); | ||
} | ||
assertThat(actual.getPassword()).isEqualTo(newPassword); | ||
} | ||
} |
143 changes: 71 additions & 72 deletions
143
jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,83 @@ | ||
package org.springframework.jdbc.core; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.sql.DataSource; | ||
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; | ||
|
||
public class JdbcTemplate { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(JdbcTemplate.class); | ||
|
||
private final DataSource dataSource; | ||
|
||
public JdbcTemplate(final DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public void update(String sql, Object... args) { | ||
try (Connection connection = dataSource.getConnection(); | ||
PreparedStatement preparedStatement = getPreparedStatement(sql, connection, args)) { | ||
preparedStatement.executeUpdate(); | ||
} catch (Exception e) { | ||
log.error(e.getMessage(), e); | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
|
||
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) { | ||
try (Connection connection = dataSource.getConnection(); | ||
PreparedStatement preparedStatement = getPreparedStatement(sql, connection, args)) { | ||
|
||
ResultSet resultSet = preparedStatement.executeQuery(); | ||
List<T> result = new ArrayList<>(); | ||
|
||
while (resultSet.next()) { | ||
result.add(rowMapper.mapRow(resultSet)); | ||
} | ||
|
||
return result; | ||
} catch (Exception e) { | ||
log.error(e.getMessage(), e); | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
|
||
public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) { | ||
try (Connection connection = dataSource.getConnection(); | ||
PreparedStatement preparedStatement = getPreparedStatement(sql, connection, args)) { | ||
ResultSet resultSet = preparedStatement.executeQuery(); | ||
T result = null; | ||
|
||
while (resultSet.next()) { | ||
if (result != null) { | ||
throw new IllegalArgumentException("1개 이상의 결과가 존재합니다."); | ||
} | ||
result = rowMapper.mapRow(resultSet); | ||
} | ||
|
||
if (result == null) { | ||
throw new IllegalArgumentException("결과가 존재하지 않습니다."); | ||
} | ||
return result; | ||
} catch (Exception e) { | ||
log.error(e.getMessage(), e); | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
|
||
private PreparedStatement getPreparedStatement(String sql, Connection connection, Object... args) throws | ||
SQLException { | ||
PreparedStatement preparedStatement = connection.prepareStatement(sql); | ||
for (int i = 0; i < args.length; i++) { | ||
preparedStatement.setObject(i + 1, args[i]); | ||
} | ||
return preparedStatement; | ||
} | ||
private static final Logger log = LoggerFactory.getLogger(JdbcTemplate.class); | ||
|
||
private final DataSource dataSource; | ||
|
||
public JdbcTemplate(final DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public void update(String sql, Object... args) { | ||
try (Connection connection = dataSource.getConnection(); | ||
PreparedStatement preparedStatement = getPreparedStatement(sql, connection, args)) { | ||
preparedStatement.executeUpdate(); | ||
} catch (Exception e) { | ||
log.error(e.getMessage(), e); | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
|
||
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) { | ||
try (Connection connection = dataSource.getConnection(); | ||
PreparedStatement preparedStatement = getPreparedStatement(sql, connection, args)) { | ||
|
||
ResultSet resultSet = preparedStatement.executeQuery(); | ||
List<T> result = new ArrayList<>(); | ||
|
||
while (resultSet.next()) { | ||
result.add(rowMapper.mapRow(resultSet)); | ||
} | ||
|
||
return result; | ||
} catch (Exception e) { | ||
log.error(e.getMessage(), e); | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
|
||
public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) { | ||
try (Connection connection = dataSource.getConnection(); | ||
PreparedStatement preparedStatement = getPreparedStatement(sql, connection, args)) { | ||
ResultSet resultSet = preparedStatement.executeQuery(); | ||
T result = null; | ||
|
||
while (resultSet.next()) { | ||
if (result != null) { | ||
throw new IllegalArgumentException("1개 이상의 결과가 존재합니다."); | ||
} | ||
result = rowMapper.mapRow(resultSet); | ||
} | ||
|
||
if (result == null) { | ||
throw new IllegalArgumentException("결과가 존재하지 않습니다."); | ||
} | ||
return result; | ||
} catch (Exception e) { | ||
log.error(e.getMessage(), e); | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
|
||
private PreparedStatement getPreparedStatement(String sql, Connection connection, Object... args) throws | ||
SQLException { | ||
PreparedStatement preparedStatement = connection.prepareStatement(sql); | ||
for (int i = 0; i < args.length; i++) { | ||
preparedStatement.setObject(i + 1, args[i]); | ||
} | ||
return preparedStatement; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters