小結一下ibatis框架下,傳入參數為數組類型或者是List類型的sql寫法。標簽裡面都不需要表名
1.傳入字符串數組,不需要標明parameterClasss,數組和List類型對象一樣都可以用<iterate>標簽進行遍歷。
<select id="selectOrgIdByNameStr" resultClass="Integer">
SELECT id
FROM org
WHERE name IN
<iterate close=")" open="(" conjunction=",">
<![CDATA[
#[]#
]]>
</iterate>
</select>
java調用代碼(注意空指針問題):
@Override
public List<Integer> selectOrgIdByNameStr(String[] orgArchArr) {
return baseDao.queryForList("org.selectOrgIdByNameStr", orgArchArr, Integer.class);
}
2.傳入List<Integer>,這個時候parameterClass傳的是List接口的類路徑,而不是List的泛型參數的類路徑!
<select id="searchWaiters" parameterClass="java.util.List" resultClass="com.chat.domain.zone.WaiterCgExt">
SELECT w.id, pin as userName, org_id as orgId, w.name, erp, job_no as jobNo, level, max_csu, gid,cg.name chatGroupName, r.yn groupYn, cg.yn chatGroupYn
FROM waiter w left join waiter_relation r on w.id = r.wid LEFT JOIN chat_group cg on r.gid = cg.id, org o
WHERE o.id = w.org_id
AND w.yn = 1
AND o.yn = 1
AND w.id IN <iterate open="(" close=")" conjunction=",">
#wid[]#
</iterate>
</select>
java調用代碼如下,注意空指針問題。
@Override
public List<WaiterCgExt> searchWaiters(ModAccManVo vo) {
List<Integer> widList = baseDao.queryForList("zone.waiter.searchWaitersId", vo, Integer.class);
if(CollectionUtils.isEmpty(widList)) {
return new ArrayList<WaiterCgExt>();
}
return baseDao.queryForList("zone.waiter.searchWaiters", widList, WaiterCgExt.class);
}
3.傳入的List的元素是復雜的對象類型,類型類型裡面包含id屬性。同樣,parameterClass傳入的是List接口的類路徑,而不是List的泛型參數的類路徑。遍歷的時候,用#list[].id#就行了,“.”後面跟上要枚舉的屬性名稱
<select id="selectWaitersByOrgList" resultClass="com.chat.domain.zone.Waiter" parameterClass="java.util.List">
SELECT id, pin as userName
FROM waiter
WHERE org_id IN
<iterate open="(" close=")" conjunction=",">
#list[].id#
</iterate>
AND yn = 1
</select>
以上代碼在本機都正常運行。
iBatis簡單入門教程 http://www.linuxidc.com/Linux/2014-01/94709.htm