Skip to content

Commit

Permalink
feat: JdbcTemplate query, queryForObject 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
thdwoqor committed Sep 26, 2023
1 parent 7bbad7e commit 362b083
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 143 deletions.
165 changes: 24 additions & 141 deletions app/src/main/java/com/techcourse/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
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;
Expand All @@ -17,13 +15,16 @@ public class UserDao {
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 = null;
this.jdbcTemplate = jdbcTemplate;
}

public void insert(final User user) {
Expand Down Expand Up @@ -72,11 +73,11 @@ public void update(final User user) {

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

pstmt.setLong(1,user.getId());
pstmt.setString(2,user.getAccount());
pstmt.setString(3,user.getPassword());
pstmt.setString(4,user.getEmail());
pstmt.setLong(5,user.getId());
pstmt.setLong(1, user.getId());
pstmt.setString(2, user.getAccount());
pstmt.setString(3, user.getPassword());
pstmt.setString(4, user.getEmail());
pstmt.setLong(5, user.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
log.error(e.getMessage(), e);
Expand All @@ -98,154 +99,36 @@ public void update(final User user) {
}



}

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

Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<User> users = new ArrayList<>();

try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();

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

while (rs.next()) {
users.add(new User(
rs.getLong(1),
rs.getString(2),
rs.getString(3),
rs.getString(4)));
}
return users;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException ignored) {
}

try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException ignored) {
}

try {
if (conn != null) {
conn.close();
}
} catch (SQLException ignored) {
}
}
return jdbcTemplate.query(sql, rs -> new User(
rs.getLong(1),
rs.getString(2),
rs.getString(3),
rs.getString(4)));
}

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

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

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

if (rs.next()) {
return new User(
rs.getLong(1),
rs.getString(2),
rs.getString(3),
rs.getString(4));
}
return null;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException ignored) {
}

try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException ignored) {
}

try {
if (conn != null) {
conn.close();
}
} catch (SQLException ignored) {
}
}
return jdbcTemplate.queryForObject(sql, rs -> new User(
rs.getLong(1),
rs.getString(2),
rs.getString(3),
rs.getString(4)), id);
}

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

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

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

if (rs.next()) {
return new User(
rs.getLong(1),
rs.getString(2),
rs.getString(3),
rs.getString(4));
}
return null;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException ignored) {
}

try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException ignored) {
}

try {
if (conn != null) {
conn.close();
}
} catch (SQLException ignored) {
}
}
return jdbcTemplate.queryForObject(sql, rs -> new User(
rs.getLong("id"),
rs.getString("account"),
rs.getString("password"),
rs.getString("email")
), account);
}
}
97 changes: 95 additions & 2 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package org.springframework.jdbc.core;

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 javax.sql.DataSource;

public class JdbcTemplate {

private static final Logger log = LoggerFactory.getLogger(JdbcTemplate.class);
Expand All @@ -14,4 +19,92 @@ public class JdbcTemplate {
public JdbcTemplate(final DataSource dataSource) {
this.dataSource = dataSource;
}

public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);

for (int i = 0; i < args.length; i++) {
pstmt.setObject(i + 1, args[i]);
}
rs = pstmt.executeQuery();

log.debug("query : {}", sql);
if (rs.next()) {
return rowMapper.mapRow(rs);
}
return null;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException ignored) {
}

try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException ignored) {
}

try {
if (conn != null) {
conn.close();
}
} catch (SQLException ignored) {
}
}
}

public <T> List<T> query(String sql, RowMapper<T> rowMapper) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);

rs = pstmt.executeQuery();

log.debug("query : {}", sql);
ArrayList<T> objects = new ArrayList<>();
while (rs.next()) {
objects.add(rowMapper.mapRow(rs));
}
return objects;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException ignored) {
}

try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException ignored) {
}

try {
if (conn != null) {
conn.close();
}
} catch (SQLException ignored) {
}
}
}
}
10 changes: 10 additions & 0 deletions jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.springframework.jdbc.core;

import java.sql.ResultSet;
import java.sql.SQLException;

@FunctionalInterface
public interface RowMapper<T> {

T mapRow(ResultSet rs) throws SQLException;
}

0 comments on commit 362b083

Please sign in to comment.