요구 사항: 같은 페이지에 표시된 내용은 2개의 다른 데이터베이스 a, b에서 조회하고 a의 결과 id를 조건으로 b에서 조회해야 한다.만약 a의 결과 데이터량이 많으면 b에 나누어 조회해야 한다.
JDK:1.6 지구층 프레임:ibatis2.0
주의 사항: 1.순환 횟수 2.메모리 사용량
public PageResult queryForPageListNew(int pageSize, int pageNum,AForm queryForm)
{
//
int i = 0;
// n
int n = 200;
// a
PageResult pr = aDAO.queryForPageResult(queryForm
.getParams(), pageSize, pageNum, A.class);
List<A> aList = pr.getResultList();
// size
int size = aList.size();
// , a id
List<Long> temp = new ArrayList<Long>();
// hashmap,size*2 hashmap resize()
Map<Long, Long> countMap = new HashMap<Long, Long>(size*2);
for (A a : aList)
{
i++;
temp.add(a.getId());
// n
if (((i + n) % n == 0 && i < size)||(i==size))
{
// DAOImpl, List
List<Map<String, Long>> countMapList = bDAO
.queryCountByOwnerForList(temp);
// countMap
for (Map<String, Long> map : countMapList)
{
Long id = map.get("owner");
Long count = map.get("count");
countMap.put(id, count);
}
temp.clear();
}
}
temp = null;
for (A a : aList)
{
a.setCount(countMap.get(a.getId()));
}
countMap.clear();
countMap = null;
return pr;
}
b의 ibatis SqlMap:
<select id="queryCountByOwnerForList" resultMap="resultMap.B" parameterClass="List">
select
t1.owner,
count(t1.owner) as count
from t_b t1
where
(
t1.status= 4
or t1.status=5
or t1.status=6
)
and t1.owner in
<iterate open= "(" close = ")" conjunction = ",">
#[]#
</iterate>
group by t1.owner
</select>
HashMap countMap 사용 설명: 두 번의 조회 결과는 id를 비교하여 조합해야 합니다.두 번 조회한 결과 집합은 모두 리스트가 없습니다. 예를 들어 직접 조합하면 for 순환의 끼워넣기를 해야 합니다. 비교 방식은 다음과 같습니다.