日期:2014-05-16  浏览次数:20464 次

ibatis传入数组或List类型参数小结

    小结一下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>
   

    以上代码在本机都正常运行。