2008. 8. 26. 22:01
이전글(~2009)
JdbcTemplate 클래스는 SpringJDBC 패키지에서 가장 기본이되는 클래스로 SQL를 질의하거나, update, 스토어드프로시져 콜, Resultset 을 실행한다. 이 클래스는 단순히 콜백 인터페이스만 구현하면 사용할수 있는데 보통 DAO의 구현체에서 사용된다.
이 클래스를 사용하기 위해서는 Datasource를 Spring IoC로 참조하고 해당 DAO 구현체에서 확장하거나 참조로 사용할수 있다.
1. SELECT 쿼리 예제
2. INSERT, UPDATE, DELETE 쿼리 예제
SQL문만 다를뿐 모두 update 메서드를 사용하면 된다.
또한 간단한 스토어드프로시져도 update메서드를 이용할수 있다.
3.기타
그밖에 DDL 같은 특수한 쿼리를 실행할때는 execute()메서드를 사용하기도 한다.
JdbcTemplate를 사용하기 위해서 위와 같은 기본기능이 있고, 사용하기 위한 Spring 환경 설정은 다음과 같다.
환경설정에서...
기억할것은 Spring은 다양한 방법을 제공하고 또 만들수 있다. 따라서 이것은 Best가 아닌 선택이라는것을 앞으로 다른 JDBC 관련 클래스를 보면서 알수 있다.
(계속...)
이 클래스를 사용하기 위해서는 Datasource를 Spring IoC로 참조하고 해당 DAO 구현체에서 확장하거나 참조로 사용할수 있다.
1. SELECT 쿼리 예제
// 숫자형을 리턴할때
int countOfActorsNamedJoe = this.jdbcTemplate.queryForInt(
"select count(0) from t_actors where first_name = ?", new Object[]{"Joe"});
int countOfActorsNamedJoe = this.jdbcTemplate.queryForInt(
"select count(0) from t_actors where first_name = ?", new Object[]{"Joe"});
// 도메인 객체와 매핑할때
public Collection findAllActors() {
return this.jdbcTemplate.query( "select first_name, surname from t_actor", new ActorMapper());
}
private static final class ActorMapper implements RowMapper {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Actor actor = new Actor();
actor.setFirstName(rs.getString("first_name"));
actor.setSurname(rs.getString("surname"));
return actor;
}
}
public Collection findAllActors() {
return this.jdbcTemplate.query( "select first_name, surname from t_actor", new ActorMapper());
}
private static final class ActorMapper implements RowMapper {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Actor actor = new Actor();
actor.setFirstName(rs.getString("first_name"));
actor.setSurname(rs.getString("surname"));
return actor;
}
}
2. INSERT, UPDATE, DELETE 쿼리 예제
this.jdbcTemplate.update(
"insert into t_actor (first_name, surname) values (?, ?)",
new Object[] {"Leonor", "Watling"});
"insert into t_actor (first_name, surname) values (?, ?)",
new Object[] {"Leonor", "Watling"});
SQL문만 다를뿐 모두 update 메서드를 사용하면 된다.
또한 간단한 스토어드프로시져도 update메서드를 이용할수 있다.
this.jdbcTemplate.update(
"call SUPPORT.REFRESH_ACTORS_SUMMARY(?)",
new Object[]{Long.valueOf(unionId)});
"call SUPPORT.REFRESH_ACTORS_SUMMARY(?)",
new Object[]{Long.valueOf(unionId)});
3.기타
그밖에 DDL 같은 특수한 쿼리를 실행할때는 execute()메서드를 사용하기도 한다.
this.jdbcTemplate.execute("create table mytable (id integer, name varchar(100))");
JdbcTemplate를 사용하기 위해서 위와 같은 기본기능이 있고, 사용하기 위한 Spring 환경 설정은 다음과 같다.
환경설정에서...
<bean id="corporateEventDao" class="com.example.JdbcCorporateEventDao">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
Class에서...<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
public class JdbcCorporateEventDao implements CorporateEventDao {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
// JDBC-backed implementations of the methods on the CorporateEventDao follow...
}
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
// JDBC-backed implementations of the methods on the CorporateEventDao follow...
}
기억할것은 Spring은 다양한 방법을 제공하고 또 만들수 있다. 따라서 이것은 Best가 아닌 선택이라는것을 앞으로 다른 JDBC 관련 클래스를 보면서 알수 있다.
(계속...)
'이전글(~2009)' 카테고리의 다른 글
[SpringJDBC] Batch (0) | 2008.08.26 |
---|---|
[SpringJDBC] NamedParameterJdbcTemplate (0) | 2008.08.26 |
[SpringJDBC] JdbcTemplate (0) | 2008.08.26 |
[SpringJDBC] 개요 (0) | 2008.08.26 |
가고싶은 일주일간의 독서여행 (2) | 2008.08.26 |
책을 사서(buy) 봐야 하는 이유 (0) | 2008.08.25 |
댓글을 달아 주세요