이 예제에서 콜렉션은 java.util.List 오브젝트이며 java.lang.Iterable 인터페이스의 구현입니다. Customer 오브젝트의 구체화된 List를 리턴하는 경우, 연결된 데이터 소스에 다시 액세스하지 않고 해당 데이터에 대해 여러 가지 질문을 할 수 있습니다.
또한, San Francisco Bay Area 영업 지역의 고객 수를 알아보려 한다고 가정하십시오. 코드는 다음과 같을 수 있습니다.
Connection con = DriverManager.getConnection(...); 1 Data db = DataFactory.getData(con); 2 Data qocdata = DataFactory.getData(); 3 List<Customer> customers = db.queryList( 4 "SELECT * from SALES.CUSTOMER", Customer.class); db.close(); 5 int sanFranCode = ... 6 Integer countCity = qocdata.queryFirst( 7 "SELECT COUNT(*) FROM ?1.com.company.Customer AS cr WHERE cr.storeRegion = ?2", Integer.class, customers, sanFranCode ); qocdata.close(); 8
위 코드에서 수행하는 단계는 다음과 같습니다.
qocdata를 닫는 대신에 고객에 대한 추가 분석을 수행할 수 있습니다(다음 예제에서 설명).
관리 보고서에 San Francisco Bay Area 영업 지역의 독립된 도시 수 및 해당 지역의 가장 큰 5개 도시(서로 다른 주소의 수로 환산)가 필요하다고 가정하십시오. 그런 다음, 제공된 관리의 적극적인 메일링 캠페인이 해당 도시의 고객을 목표로 설정합니다.
연결된 데이터 소스에 대한 하나의 쿼리와 어노테이션이 있는 메소드를 사용하는 얼마간의 응용프로그램 논리를 사용하여 콜렉션에 대한 세 가지 쿼리를 묶을 수 있습니다.
먼저 호출 응용프로그램과 어노테이션이 있는 메소드 둘 모두에 사용되는 스테이징 클래스가 필요합니다.
public class CitySize { public String city; public Integer size; }
구현 클래스에서 콜렉션에 대한 쿼리와 데이터베이스에 대한 쿼리 둘 다에 대해 메소드를 사용할 수 없으므로 두 개의 독립된 인터페이스를 작성해야 합니다.
CustomerQuery 인터페이스는 데이터 소스 쿼리를 위한 메소드를 정의합니다. getCustomersInRegion() 메소드는 지정된 영업 지역에 있는 고객을 표시하는 Customer 오브젝트의 목록을 리턴합니다.
import com.company.Customer; public interface CustomerQuery { @Select(sql= "SELECT custId, name, ... FROM Customer WHERE region=?1") List<Customer> getCustomersInRegion(int r); }
RegionReport 인터페이스는 콜렉션 오브젝트 쿼리를 위한 메소드를 정의합니다.
import com.company.Customer; import com.company.Campaign.CitySize; public interface RegionReport { @Select(sql="SELECT COUNT(DISTINCT city) FROM ?1") public Integer countCities (List<Customer> curRegion); @Select(sql= "SELECT city, COUNT(DISTINCT addressL1) AS size " + " FROM ?1 GROUP BY city ORDER BY size DESC") public CitySize[] getCitySize(List<Customer> curRegion); @Select(sql="SELECT cr.this FROM ?1 AS cr, ?2 AS t5 " + " WHERE cr.city = t5.city ") List<Customer> getMailingList(List<Customer> curRegion, CitySize[] topFive); }
인터페이스에 대한 구현 클래스를 생성한 후, 다음과 같은 응용프로그램 논리를 사용하여 관리 보고서 및 목표로 지정된 고객의 목록을 작성할 수 있습니다.
Connection con = DriverManager.getConnection(...); 1 CustomerQuery cQuery = DataFactory.getData( CustomerQuery.class, con ); 2 RegionReport inMem = DataFactory.getData( RegionReport.class ); 3 int sanFranCode = ...; 4 List<Customer> customers = cQuery.getCustomersInRegion(sanFranCode); 5 cQuery.close(); 6 Integer cityCount = inMem.countCities(customers); 7 System.out.println ( 8 " There are " + cityCount + " cities in region " + sanFranCode ); System.out.println ( " The largest 5, and their number of addresses are: " ); CitySize[] allCityList = inMem.getCitySize(customers); 9 CitySize[] topFive = new CitySize[5]; 10 for (int i=0; i<5; i++) 11 { topFive[i] = allCityList[i]; System.out.println ( " " + topFive[i].city + " " + topFive[i].size); } List<Customer> mailingCust = 12 inMem.getMailingList(customers, topFive); inMem.close(); 13
위 코드에서 수행하는 단계는 다음과 같습니다.
관리가 목표 메일링이 너무 작거나 너무 크다고 판별하는 경우, CitySize 배열에 할당된 크기 및 배열을 채우는 루프를 약간만 변경해도 결과적으로 얻은 mailingCust 목록에서 Customer 오브젝트를 추가하거나 제거하는 데 충분하다는 점을 지적할 만한 가치가 있습니다. getMailingList() 메소드는 입력 배열의 크기와 무관합니다.