Query Service - Embedded SQL
다음은 Embedded SQL을 사용하는 경우로, 매핑 XML 파일에 별도로 쿼리문을 정의해 두지 않고도 특정 쿼리문을 직접 입력하여
실행할 수 있다.
Query 서비스 속성 정의 파일 Sample
다음은 Query 서비스를 정의한
applicationContext-query-common.xml
파일의 일부이다.
<bean id="queryService" class="anyframe.core.query.impl.QueryServiceImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
<property name="sqlRepository" ref="sqlLoader"/>
<!-- 중략 -->
</bean>
<bean id="jdbcTemplate" class="anyframe.core.query.impl.jdbc.PagingJdbcTemplate">
<property name="dataSource" ref="common_datasource" />
</bean>
<!-- 중략 -->
Query 서비스 테스트 코드 Sample
다음은 매핑 XML 파일에 별도 쿼리문을 정의하지 않고 쿼리문을 직접 입력하여 실행하는 소스 코드
QueryServiceTestEmbeddedSQL.java
이다.
다음에서 볼 수 있듯이 Query 서비스는 매핑 XML 파일 파일 없이도 INSERT, SELECT, UPDATE, DELETE를 실행할 수 있도록 지원한다.
/**
* 테스트 코드를 샐행하기 위한 data table 생성
*/
public void testInsertQuery() throws Exception{
IQueryService queryService = (IQueryService) context.getBean("queryService");
//createBySQL() : 매핑 XML 파일에 쿼리문이 정의되어 있지 않을 때 INSERT를 실행한다.
int result = queryService.createBySQL(
"insert into TBL_CUSTOMER values (?, ?, ?)",
new String[] {"VARCHAR", "VARCHAR", "VARCHAR" },
new Object[] {"1234567890123", "GilDongHong", "Seoul" });
if ( result == -1 ){
throw new Exception("Insert Query failed");
}
}
/**
* Query 서비스를 통해 DB에 입력된 데이터를 조회하는 테스트 코드
*/
public void testSelectQuery() throws Exception{
IQueryService queryService = (IQueryService) context.getBean("queryService");
//findBySQL() : 매핑 XML 파일에 쿼리문이 정의되어 있지 않을 때 SELECT를 실행한다.
Collection result = queryService.findBySQL(
"select NAME, ADDRESS from TBL_CUSTOMER where SSNO like ?",
new String[] { "VARCHAR" },
new Object[] { "%4567890123" });
Iterator resultItr = result.iterator();
while( resultItr.hasNext() ){
Map resultMap = (Map) resultItr.next();
resultMap.get("name");
}
if(result.size() != 1){
throw new Exception("Select Query failed");
}
}
/**
* Query 서비스를 통해 DB에 입력된 데이터를 수정하는 테스트 코드
*/
public void testUpdateQuery() throws Exception{
IQueryService queryService = (IQueryService) context.getBean("queryService");
//updateBySQL() : 매핑 XML 파일에 쿼리문이 정의되어 있지 않을 때 UPDATE를 실행한다.
int result = queryService.updateBySQL(
"update TBL_CUSTOMER set NAME=? where SSNO=?",
new String[] {"VARCHAR", "VARCHAR" },
new Object[] { "Anonymous","1234567890123" });
if(result == -1 ){
throw new Exception("Update Query failed");
}
}
/**
* Query 서비스를 통해 DB에 입력된 데이터를 삭제하는 테스트 코드
*/
public void testDeleteQuery() throws Exception{
IQueryService queryService = (IQueryService) context.getBean("queryService");
//removeBySQL() : 매핑 XML 파일에 쿼리문이 정의되어 있지 않을 때 DELETE를 실행한다.
int result = queryService.removeBySQL(
"delete from TBL_CUSTOMER where SSNO=?",
new String[] { "VARCHAR" },
new Object[] { "1234567890123" });
if(result == -1 ){
throw new Exception("Delete Query failed");
}
}
앞서 소개된 샘플 테스트 코드를 포함하여 Query 서비스 소개 페이지에서 제공하는 모든 샘플 테스트 코드는 HSQL DB를
기반으로 실행된다. ( 단, ※ CallableStatement, LOB의 경우는 Oracle 9i, 10g를 기반으로 함. )
Resources
다운로드
샘플 테스트 코드를 포함하고 있는 anyframe-querytest-src.zip 파일을 다운받은 후, 테스트 환경 설정
을 참조하여
위에서 제시한 예제 코드를 실행해 볼 수 있다.
| Name
|
Download
|
| anyframe-querytest-src.zip |
Download
|