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

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

 

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

 

1. 直接使用 List 作为入参

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * 查询出目前系统已有的渠道信息
 * @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>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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 类型参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * 查询出目前系统已有的渠道信息
 * @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>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<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>