Sunteți pe pagina 1din 11

Spring 프레임워크의 이해

5. Spring Abstract API


Spring – Abstract API 2007 Grow up to be NHN

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
Spring – Abstract API 2007 Grow up to be NHN

Template Method
Pattern
Spring – Abstract API 2007 Grow up to be NHN

Spring – Abstract API 2007 Grow up to be NHN

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


Spring – Abstract API 2007 Grow up to be NHN

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 = <code to get connection>
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();

S-ar putea să vă placă și