• 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>
    	
    

      

  • 相关阅读:
    信号的调制
    是否产生latch
    带通采样定理
    傅里叶变换
    信号与傅里叶(下)
    滤波器的相位和信号的时延
    信号与傅里叶级数
    阅读应该是主动的
    Matlab笔记—函数
    网络搭建---IP地址的设置及ping的使用
  • 原文地址:https://www.cnblogs.com/DFX339/p/12084719.html
Copyright © 2020-2023  润新知