Spring 프레임워크의 이해
5. Spring Abstract API
2007 Grow up to be NHN
Spring – Abstract API
Spring JDBC 를 이용할 때 개발 자들이 구현 할 부분 작업
Spring JDBC
개발자
Connection 관리
O
X
SQL
X
O
Statement 관리
O
X
ResultSet 관리
O
X
Row 데이터 추출
X
O
패러미터 선언
X
O
패러미터 Setting
O
X
트랜잭션 관리
O
X
人
2007 Grow up to be NHN
Spring – Abstract API
Template Method Pattern
人
Spring – Abstract API
2007 Grow up to be NHN
人
2007 Grow up to be NHN
Spring – Abstract API
public abstract class AbstractClass { public void templateMethod() { // .. 비지니스 로직 구현 operation1(); // .. 비지니스 로직 구현 operation2(); } protected abstract void operation1(); protected abstract void operation2(); }
人
Spring – Abstract API
2007 Grow up to be NHN
public class ConcreteClassA extends AbstractClass { protected void operation1() { // TODO Auto-generated method stub } protected void operation2() { // TODO Auto-generated method stub } }
人
Spring – Abstract API
2007 Grow up to be NHN
Template Method = IoC
人
2007 Grow up to be NHN
Spring – Abstract API
Callback Class
Callback Method
人
Spring – Abstract API
2007 Grow up to be NHN
public interface RowCallbackHandler { void processRow(ResultSet rs) throws SQLException; }
人
Spring – Abstract API
2007 Grow up to be NHN
public void query(String sql, RowCallbackHandler callbackHandler) throws JdbcSqlException { Connection con = null; PreparedStatement ps = null; ResultSet rs = null; try { con = ps = con.prepareStatement (sql); rs = ps.executeQuery(); while (rs.next()) { callbackHandler.processRow(rs); } rs.close(); ps.close(); } catch (SQLException ex) { throw new JdbcSqlException("Couldn't run query [" + sql + "]", ex); } finally { DataSourceUtils.closeConnectionIfNecessary(this.dataSource, con); } }
人
Spring – Abstract API
2007 Grow up to be NHN
class StringHandler implements JdbcTemplate.RowCallbackHandler { private List 1 = new LinkedList(); public void processRow(ResultSet rs)throws SQLException { 1.add(rs.getString(1)); } public String[] getStrings() { return (String[]) 1.toArray(new String[1.size()]); } }
StringHandler sh = new StringHandler(); jdbcTemplate.query("SELECT FORENAME FROM CUSTMR", sh); String[] forenames = sh.getStrings();
人