Java iBatis使用List类型参数(解决List为空的报错问题)

Java iBatis使用List类型参数(解决List为空的报错问题)

在查询的时候需要使用 in  或 not in 关键字来获取相关数据信息,这里以 not in 为例(需要排除的数据项)

1.直接使用List作为入参

Dao层方法的定义: 封装List类型参数

	/**
	 * 查询出目前系统已有的渠道信息
	 * @param areaCode 预留机构号参数,用于排除不同地区的渠道信息
	 * @return
	 * @throws IMException
	 */
	public List<ChannelPojo> queryAllChannels(String areaCode) throws IMException{

		List<String> 	exceptChannelList  = new ArrayList<String>();
		if(StringUtils.isNotBlank(areaCode) && "820200".equals(areaCode)){
			exceptChannelList.add("10");
			exceptChannelList.add("20");
		}else if(StringUtils.isNotBlank(areaCode) && "830300".equals(areaCode)){
			exceptChannelList.add("21");
			exceptChannelList.add("22");
			exceptChannelList.add("23");
			exceptChannelList.add("25");
		}
		logger.info("Demo.queryAllChannels,Parameter areaCode value is:"+areaCode);
		logger.info("Demo.queryAllChannels,exceptChannelList is:"+ exceptChannelList);
		
		return st.queryForList("kpiRptum.queryAllChannels",exceptChannelList);
	}
	

 

SqlMap的定义,迭代取出参数信息

主要sql片段:

<iterate open="(" close=")" conjunction=",">
       <![CDATA[  #exceptChannelList[]# ]]>
</iterate>

<select id="queryAllChannels"  parameterClass="java.util.List" resultClass="com.imodule.report.dao.pojo.ChannelPojo">
	<![CDATA[
		select 
			codevalue as channelcode
			,name as channelCfname
			,smpname as channelCsmpname
			,trim(replace(engname,' ','')) as channelEname
		from 
			t_codedef 
		where 1=1
	]]>
		and codevalue not in 
		<iterate  open="(" close=")" conjunction=",">
		 	 <![CDATA[  
             	 	  #exceptChannelList[]#
              	       ]]>
              </iterate> 
    
         
       <![CDATA[  
		order by codevalue asc
	]]>
</select>

  

 

2使用Map作为入参,将List对象存入Map集合中  (建议使用此方式,可避免当传入的List对象为空时而报错)

Dao层方法的定义: 封装Map类型参数

    /**
	 * 查询出目前系统已有的渠道信息
	 * @param areaCode 预留机构号参数,用于排除不同地区的渠道信息
	 * @return
	 * @throws IMException
	 */
	public List<ChannelPojo> queryAllChannels(String areaCode) throws IMException{
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> 	exceptChannelList  = new ArrayList<String>();
		if(StringUtils.isNotBlank(areaCode) && "820200".equals(areaCode)){
			exceptChannelList.add("10");
			exceptChannelList.add("20");
		}else if(StringUtils.isNotBlank(areaCode) && "830300".equals(areaCode)){
			exceptChannelList.add("21");
			exceptChannelList.add("22");
			exceptChannelList.add("23");
			exceptChannelList.add("25");
		}
		map.put("exceptChannelList", exceptChannelList);
		logger.info("Demo.queryAllChannels,Parameter areaCode value is:"+areaCode);
		logger.info("Demo.queryAllChannels,exceptChannelList is:"+ exceptChannelList);
		
		return st.queryForList("kpiRptum.queryAllChannels",map);
	}

  

SqlMap的定义,迭代取出参数信息

主要sql片段:

<isPropertyAvailable property="exceptChannelList">
  <isNotEmpty property="exceptChannelList">
    and codevalue not in
    <iterate property="exceptChannelList" open="(" close=")" conjunction=",">
      <![CDATA[
        #exceptChannelList[]#
      ]]>
    </iterate>
  </isNotEmpty>

  <isEmpty property="exceptChannelList">
    <![CDATA[ and 1=1 ]]>
  </isEmpty>
</isPropertyAvailable>

<select id="queryAllChannels"  parameterClass="java.util.Map" resultClass="com.imodule.report.dao.pojo.ChannelPojo">
		<![CDATA[
			select 
				codevalue as channelcode
				,name as channelCfname
				,smpname as channelCsmpname
				,trim(replace(engname,' ','')) as channelEname
			from 
				t_codedef 
			where = 1=1
		]]>
		<isPropertyAvailable  property="exceptChannelList">
   			<isNotEmpty property="exceptChannelList">
				and codevalue not in 
			 	<iterate property="exceptChannelList"  open="(" close=")" conjunction=",">
			 		 <![CDATA[  
              	 		#exceptChannelList[]#
               		 ]]>
             	</iterate> 
        	</isNotEmpty>
        
    		<isEmpty property="exceptChannelList">
      	  		<![CDATA[ and  1=1 ]]>
    		</isEmpty>
	    </isPropertyAvailable>
          
        <![CDATA[  
			order by codevalue asc
		]]>
	</select>
	

  

原文地址:https://www.cnblogs.com/DFX339/p/12084719.html