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>
	

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。