Query Service - Pagination
페이징 처리를 수행하는 예제로 해당 페이지에 속한 data만 전달해 주기 때문에 효율적이고 page 처리를 위한 별도 로직을 필요로 하지 않는다.
display하고자 하는 페이지 번호와 한 페이지에 보여지는 data의 개수만 prameter로 입력하면 된다.
Query Service의 Configuration file
다음은 Query Service를 정의한
applicationContext-query-pagination.xml이다.
<bean name="queryService" class="anyframe.core.query.impl.QueryServiceImpl">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
<config:configuration>
<filename>classpath:services/query/mappings/mapping-paging-query.xml</filename>
</config:configuration>
</bean>
<!-- The original JdbcTemplate definition -->
<bean id="jdbcTemplate" class="anyframe.core.query.impl.util.PagingJdbcTemplate">
<property name="dataSource" ref="common_datasource" />
</bean>
Mapping xml file 정의 예제
다음은 위에 정의한 Query Service가 로드시킨
mapping-pagination-query.xml로,
Table 매핑 정보와 query를 포함하고 있다.
<query id="selectUsingPagination">
<statement>
SELECT NAME FROM TBL_CUSTOMER WHERE SSNO like ?
</statement>
<param type="VARCHAR" />
<result class="integration.anyframe.core.query.vo.CustomerVO"/>
</query>
Paging support를 사용하는 Query Service의 샘플 코드
아래는 Query Service의 pagination을 사용하여 DB의 data를 SELECT하는 소스 코드
QueryServiceTestPagination.java이다.
샘플 코드는 TBL_CUSTOMER 테이블에 여러 데이터가 입력되었을 때, 한 페이지에 display하는 data의 개수(5)와
display하고자 하는 페이지수(3)를 입력하여 특정 page에 속한 데이터만 조회하는 예제이다.
/**
* Pagination으로 많은 데이터를 page별로 SELECT하는 테스트 코드
*/
public void testSelectQuery() throws Exception {
IQueryService queryService = (IQueryService) context
.getBean("queryService");
/** findWithRowCount() : XML mapping파일에 정의되어 있는 SQL query를 이용하여 SELECT를 실행한다.
* find()와 다른 점은 findWithRowCount()는 한번의 method실행으로 record의
* 전체 개수와 display하고자 하는 page의 record를 얻어 올 수 있다.
*/
HashMap resultMap = queryService.findWithRowCount("selectUsingPagination",
new Object[] { "%1234%" }, 3, 5);
CustomerVO customerVO = new CustomerVO();
Map rsMap = new HashMap();
List resultList = (List) resultMap.get(IQueryService.LIST);
for(int i = 0 ; i < resultList.size() ; i ++){
customerVO = (CustomerVO)resultList.get(i);
customerVO.getNm();
}
int totalSize = ((Long) resultMap.get(IQueryService.COUNT)).intValue();
if (resultList.size() != 5 || totalSize != 15) {
throw new Exception("Select query failed");
}
}
다음은 paging support를 사용하는 일반적인 유형이다.
public Collection find...(..., int page number to display, int row count per page);- page number이 1보다 작으면 첫번째 page가 display된다.
- row count per page는 0보다 커야 한다.
- page number와 row count를 사용하여 계산한 값이 전체 record의 개수보다 크면 마지막 페이지가 출력된다.
row count per page parameter가 생략될 경우 다음과 매핑 파일의 result mapping tag에 별도로 정의해 주어야 한다.
<result length="10"/>
public Collection find...(..., int page number to display);
앞서 소개된 샘플 테스트 코드를 포함하여 QueryService 소개 페이지에서 제공하는 모든 샘플 테스트 코드는 HSQL DB를
기반으로 실행된다. ( 단, ※ CallableStatement, LOB의 경우는 Oracle 9i, 10g를 기반으로 함.)
Resources
다운로드
샘플 테스트 코드를 포함하고 있는 anyframe-querytest-src.zip 파일을 다운받은 후, 테스트 환경 설정을 참조하여
위에서 제시한 예제 코드를 실행해 볼 수 있다.
| Name |
Download |
| anyframe-querytest-src.zip |
Download |