Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spring-toby-3 [Spring] 3. 템플릿 (3.6 - refactoring) #11

Merged
merged 1 commit into from
Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion chapter2/src/main/java/com/example/chapter2/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void add(User user) throws SQLException {
public User get(String id) throws SQLException {
try (
Connection c = dataSource.getConnection();
PreparedStatement ps = c.prepareStatement("SELECT * FROM USERS WHERE id = ?")
PreparedStatement ps = makeStatement(c);
) {
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
Expand All @@ -51,6 +51,10 @@ public User get(String id) throws SQLException {
}
}

private PreparedStatement makeStatement(Connection c) throws SQLException {
return c.prepareStatement("SELECT * FROM USERS WHERE id = ?");
}

public int getCount() throws SQLException {
try (
Connection c = dataSource.getConnection();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.example.template.part2.context;

import com.example.template.part1.strategy.StatementStrategy;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Setter
@NoArgsConstructor
public class JdbcContext {
private final DataSource dataSource;

private DataSource dataSource;

public JdbcContext(DataSource dataSource) {
this.dataSource = dataSource;
Expand All @@ -22,4 +27,13 @@ public void workWithStatementStrategy(StatementStrategy strategy) throws SQLExce
s.executeUpdate();
}
}

public void executeSql(final String query) throws SQLException {
workWithStatementStrategy(new StatementStrategy() {
@Override
public PreparedStatement makePreparedStatement(Connection c) throws SQLException {
return c.prepareStatement(query);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import com.example.template.part1.User;
import com.example.template.part1.strategy.StatementStrategy;
import com.example.template.part2.context.JdbcContext;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Configuration
public class UserDaoDataSourceDI {

private final JdbcContext context;

public UserDaoDataSourceDI(DataSource dataSource) {
this.context = new JdbcContext(dataSource);
this.context = new JdbcContext();
context.setDataSource(dataSource);
}

public void add(User user) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.example.template.part1.strategy.StatementStrategy;
import com.example.template.part1.strategy.TruncateStatement;
import com.example.template.part2.context.JdbcContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.EmptyResultDataAccessException;

import javax.sql.DataSource;
Expand Down Expand Up @@ -42,13 +43,6 @@ public PreparedStatement makePreparedStatement(Connection c) throws SQLException
}

public void truncateTable() throws SQLException {
this.context.workWithStatementStrategy(
new StatementStrategy() {
@Override
public PreparedStatement makePreparedStatement(Connection c) throws SQLException {
return c.prepareStatement("TRUNCATE TABLE USERS");
}
}
);
this.context.executeSql("TRUNCATE TABLE USERS");
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.example.template.part3;

import com.example.template.part1.User;
import com.example.template.part1.strategy.StatementStrategy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
Expand Down