3. 템플릿

템플릿

1 다시보는 초난감 DAO

공유리소스 반환에 대한 문제 -> 예외 발생시점에서 리소스 반환이 안되는 경우 DB풀이 차기 때문에 문제가 발생할 수 있다. -> try/catch/finally 사용이 필수 -> 1.7이후 AutoClose 인터페이스 경우 : try~with~catch 구문을 활용하는게 이득 -> 리소스 반환은 역순으로 close()

2 변하는 것과 변하지 않는 것

변하는 부분과 변하지 않는 부분을 분리 (기능단위 메서드 분리) -> 템플릿메서드 패턴 활용 / 전략 패턴

  • 전략패턴

    public interface StatementStrategy {
      PreparedStatement makePreparedStatement(Connection connection) throws SQLException;
    }
  • 서브클래스1 - DeleteAllStatement

    public class DeleteAllStatement implements StatementStrategy {
    
      @Override
      public PreparedStatement makePreparedStatement(Connection connection) throws SQLException {
        PreparedStatement ps = connection.prepareStatement("delete from test");
        return ps;
      }
    }
  • 서브클래스2 - AddStatement

    public class AddStatement implements StatementStrategy {
    
      private User user;
    
      public AddStatement(User user) {
        this.user = user;
      }
    
      @Override
      public PreparedStatement makePreparedStatement(Connection connection) throws SQLException {
        PreparedStatement ps = connection.prepareStatement("insert into test values (?,?,?)");
        ps.setString(1, user.getId());
        ps.setString(2, user.getName());
        ps.setString(3, user.getPassword());
        return ps;
      }
    }
  • 마이크로 DI 스프링컨테이너가 DI하는 것이 아니라 코드내에서 DI

3 JDBC 전략 패턴의 최적화

  • 익맹내부 클래스 사용 / 로컬 클래스 사용

    클래스파일을 줄일 수 있다

4 컨텍스트와 DI

JdbcContext 분리

  • JdbcContext

  • .xml 설정

    5 템플릿과 콜백

    템플릿 제공 -> 콜백으로 처리후 반환 된 데이터 재처리

  • 템플릿/콜백 응용

  • 제네릭스를 이용한 방법

6 스프링의 JdbcTemplate

위에서 만든 JdbcContext 클래스 대신 사용

  • queryForInt -> deprecated

  • queryForObject

    RowMapper

  • getAll

    query() 같은 경우 데이터가 없으면 빈값을 리턴해줌

    queryForObject() 같은 경우는 데이터가 없는 경우 예외발생

  • 리팩토링 (중복되는 부분 제거)

    ```java // 중복되는 부분 메서드 추출 private RowMapper getRowMapper() { return (ResultSet rs, int rowNum) -> { User user = new User(); user.setId(rs.getString("id")); user.setName(rs.getString("name")); user.setPassword(rs.getString("password")); return user; }; }

    public List getAllRefactoring() { return jdbcTemplate.query("select * from test order by id", getRowMapper() // private 사용 ); }

    @Test public void test_3_56() { System.out.println(userDao.getAllRefactoring()); // 리팩토링 후 맞는지 확인 Assert.assertThat(userDao.getAll().toString(), CoreMatchers.is(userDao.getAllRefactoring().toString())); }

```

Last updated

Was this helpful?