Skip to content
Merged
3 changes: 1 addition & 2 deletions spring-toby/spring-project/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework:spring-jdbc'
implementation 'org.springframework.data:spring-data-jpa'

implementation 'com.h2database:h2'
implementation 'com.h2database:h2:1.4.197'

testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.javabom.toby.chapter3.term.전략_패턴;

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

public class DeleteAllStatement implements StatementStrategy {
@Override
public PreparedStatement makePreparedStatement(Connection connection) throws SQLException {
return connection.prepareStatement("DELETE FROM USERS");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.javabom.toby.chapter3.term.전략_패턴;

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

public class GetCountStatement implements StatementStrategy {
@Override
public PreparedStatement makePreparedStatement(Connection connection) throws SQLException {
return connection.prepareStatement("SELECT COUNT(*) FROM USERS");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.javabom.toby.chapter3.term.전략_패턴;

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

public interface StatementStrategy {
PreparedStatement makePreparedStatement(Connection connection) throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.javabom.toby.chapter3.term.전략_패턴;

import com.javabom.toby.user.connectionmaker.ConnectionMaker;

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

public class UserDao {
private ConnectionMaker connectionMaker;
private DataSource dataSource;

public void setDataSource(final DataSource dataSource) {
this.dataSource = dataSource;
}

public void setConnectionMaker(final ConnectionMaker connectionMaker) {
this.connectionMaker = connectionMaker;
}

//중복되는 코드와 로직에 따라 자꾸 확장되고 자주 변하는 코드를 잘 분리해내는 작업이다.
public void deleteAll() throws SQLException {
Connection connection = null;
PreparedStatement ps = null;

try {
connection = dataSource.getConnection();
StatementStrategy statementStrategy = new DeleteAllStatement();
ps = statementStrategy.makePreparedStatement(connection);
ps.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
}
}
}
}


public int getCount() throws SQLException {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;

try {
connection = dataSource.getConnection();

StatementStrategy statementStrategy = new GetCountStatement();
ps = statementStrategy.makePreparedStatement(connection);

rs = ps.executeQuery();
rs.next();
return rs.getInt(1);
} catch (SQLException e) {
throw e;
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.javabom.toby.chapter3.term.중첩_클래스;


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

public class JdbcContext {
private DataSource dataSource;

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

public void workWithStatementStrategy(StatementStrategy statementStrategy) throws SQLException {
Connection connection = null;
PreparedStatement ps = null;

try {
connection = dataSource.getConnection();
ps = statementStrategy.makePreparedStatement(connection);
ps.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.javabom.toby.chapter3.term.중첩_클래스;

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

public interface StatementStrategy {
PreparedStatement makePreparedStatement(Connection connection) throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.javabom.toby.chapter3.term.중첩_클래스.로컬_클래스;

import com.javabom.toby.chapter3.term.중첩_클래스.JdbcContext;
import com.javabom.toby.chapter3.term.중첩_클래스.StatementStrategy;

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

public class UserDao {
private JdbcContext jdbcContext;

public void setJdbcContext(JdbcContext jdbcContext) {
this.jdbcContext = jdbcContext;
}

public void deleteAll() throws SQLException {
class DeleteAllStatement implements StatementStrategy {
@Override
public PreparedStatement makePreparedStatement(Connection connection) throws SQLException {
return connection.prepareStatement("DELETE FROM USERS");
}
}
StatementStrategy statementStrategy = new DeleteAllStatement();
jdbcContext.workWithStatementStrategy(statementStrategy);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.javabom.toby.chapter3.term.중첩_클래스.멤버_내부_클래스;

import com.javabom.toby.chapter3.term.중첩_클래스.JdbcContext;
import com.javabom.toby.chapter3.term.중첩_클래스.StatementStrategy;

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

public class UserDao {
private JdbcContext jdbcContext;

public void setJdbcContext(JdbcContext jdbcContext) {
this.jdbcContext = jdbcContext;
}

public void deleteAll() throws SQLException {

StatementStrategy statementStrategy = new DeleteAllStatement();
jdbcContext.workWithStatementStrategy(statementStrategy);
}

private static class DeleteAllStatement implements StatementStrategy {
@Override
public PreparedStatement makePreparedStatement(Connection connection) throws SQLException {
return connection.prepareStatement("DELETE FROM USERS");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.javabom.toby.chapter3.term.중첩_클래스.익명_클래스;

import com.javabom.toby.chapter3.term.중첩_클래스.JdbcContext;

import java.sql.SQLException;

public class UserDao {
private JdbcContext jdbcContext;

public void setJdbcContext(JdbcContext jdbcContext) {
this.jdbcContext = jdbcContext;
}

public void deleteAll() throws SQLException {
jdbcContext.workWithStatementStrategy(
connection -> connection.prepareStatement("DELETE FROM USERS")
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.javabom.toby.chapter3.term.템플릿_메서드_패턴;

import com.javabom.toby.user.connectionmaker.ConnectionMaker;

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

public abstract class UserDao {
private ConnectionMaker connectionMaker;
private DataSource dataSource;

public void setDataSource(final DataSource dataSource) {
this.dataSource = dataSource;
}

public void setConnectionMaker(final ConnectionMaker connectionMaker) {
this.connectionMaker = connectionMaker;
}

//중복되는 코드와 로직에 따라 자꾸 확장되고 자주 변하는 코드를 잘 분리해내는 작업이다.
public void deleteAll() throws SQLException {
Connection connection = null;
PreparedStatement ps = null;

try {
connection = dataSource.getConnection();
ps = makeStatement(connection);
ps.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
}
}
}
}

protected abstract PreparedStatement makeStatement(Connection connection) throws SQLException;

public int getCount() throws SQLException {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;

try {
connection = dataSource.getConnection();

ps = makeStatement(connection);

rs = ps.executeQuery();
rs.next();
return rs.getInt(1);
} catch (SQLException e) {
throw e;
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.javabom.toby.chapter3.term.템플릿_메서드_패턴;

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

public class UserDaoDeleteAll extends UserDao {
@Override
protected PreparedStatement makeStatement(Connection connection) throws SQLException {
return connection.prepareStatement("DELETE FROM USERS");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.javabom.toby.chapter3.term.템플릿_메서드_패턴;

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

public class UserDaoGetCount extends UserDao {
@Override
protected PreparedStatement makeStatement(Connection connection) throws SQLException {
return connection.prepareStatement("SELECT COUNT(*) FROM USERS");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.javabom.toby.userdao;
package com.javabom.toby.user;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.javabom.toby.userdao;
package com.javabom.toby.user;

import com.javabom.toby.user.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.javabom.toby.user.connectionmaker;

public interface ConnectionMaker {
void makeConnection();
}
Loading