zoukankan      html  css  js  c++  java
  • 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>
    	
    

      

  • 相关阅读:
    Objective-C 【self的用法】
    Objective-C 学习笔记(Day 3,下)
    Objective-C 学习笔记(Day 3,上)
    Objective-C 学习笔记(Day 2)
    牵扯较多属性和方法的类题目,很简单的题目本来不想发的,如果有同学学到这个题目感觉太长不愿敲代码,copy走我的即可~不过还是建议自己打一打
    Objective-C 学习笔记(Day 1)
    输出一个等边三角形的字母阵,等边三角形的两腰为字母A,往里靠依次字母大一个(详细题目文章中描述)
    分类或者叫类别 oc
    自定义构造方法 initWithName: oc
    self = [super init] 拿出来单独讲讲
  • 原文地址:https://www.cnblogs.com/DFX339/p/12084719.html
Copyright © 2011-2022 走看看