mybatis foreach详解

mybatis foreach 参数说明:sql

属性    描述数组

  • item ,循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details.  具体说明:在list和数组中是其中的对象,在map中是value.该参数为必选。mybatis

  • collection,要作foreach的对象,做为入参时,List<?>对象默认用list代替做为键,数组对象有array代替做为键,Map对象没有默认的键。固然在做为入参时能够使用@Param("keyName")来设置键,设置keyName后,list,array将会失效。 除了入参这种状况外,还有一种做为参数对象的某个字段的时候。举个例子:若是User有属性List ids。入参是User对象,那么这个collection = "ids".若是User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id".上面只是举例,具体collection等于什么,就看你想对那个元素作循环。该参数为必选。app

  • separator,元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号致使sql错误,如in(1,2,)这样。该参数可选。spa

  • open,foreach代码的开始符号,通常是(和close=")"合用。经常使用在in(),values()时。该参数可选。.net

  • close,foreach代码的关闭符号,通常是)和open="("合用。经常使用在in(),values()时。该参数可选。xml

  • index,在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。对象


**********************************************具体操做*******************************************blog

------------------------------collection操做的是List---------------------------------------it

一、参数为Map,"callLetters":List<String>

   Mapper类:

       public List<TWgLastPos> findByParams(Map<String,Object> params);

   MapperXML:

      (1) <select id="findByParams" parameterType="Map" resultType="TWgLastPos">

    SELECT * FROM t_wg_last_pos

    <where>

       call_letter in

       <foreach collection="callLetters" item="item" index="index" open="(" separator="," close=")">

            #{item}

       </foreach>

    </where>

 </select>


 结果:SELECT * FROM t_wg_last_pos WHERE call_letter in ( ? , ? , ? ) 

            Parameters: 1381254878(String), 1381254877(String), 1381254879(String)


     (2) 读取index

          <select id="findByParams" parameterType="Map" resultType="TWgLastPos">

    SELECT * FROM t_wg_last_pos

    <where>

       call_letter in

       <foreach collection="callLetters" item="item" index="index" open="(" separator="," close=")">

            #{index}

       </foreach>

    </where>

 </select>


结果:SELECT * FROM t_wg_last_pos WHERE call_letter in ( ? , ? , ? ) 

     Parameters: 0(Integer), 1(Integer), 2(Integer)


二、参数为List<String>和上面的操做基本类型:

   Mapper:

        public List<TWgLastPos> findByParams(@Param("calls")List<String> list);

   Mapper xml:

        <select id="findByParams" parameterType="Map" resultType="TWgLastPos">

    SELECT * FROM t_wg_last_pos

    

    <where>

       call_letter in

       <foreach collection="calls" item="item" index="index" open="(" separator="," close=")">

            #{index}

       </foreach>

    </where>

</select>


结果:同1,这里能够看出和parameterType没有关系,传递的参数是List,这里也能正常执行.

     默认状况下,List下面,collection="list",能够经过@Param修改key,这里为calls.


三、参数为List<Map<String,Object>> 

   Mapper: 

      public List<TWgLastPos> findByParams(List<Map<String,Object>> list);


   Mapper xml:

      <select id="findByParams" parameterType="Map" resultType="TWgLastPos">

    SELECT * FROM t_wg_last_pos

    

    <where>

       call_letter in

       <foreach collection="list" item="item" index="index" open="(" separator="," close=")">

            #{item.callLetter}

       </foreach>

    </where>

</select>

-------------------------------------------------操做的是Map---------------------------------


Mapper:

    public List<TWgLastPos> findByParams(@Param("map") Map<String,Object> params);//没有默认Key,必须这样赋值

Mapper xml:

    <select id="findByParams" parameterType="Map" resultType="TWgLastPos">

    SELECT * FROM t_wg_last_pos

    

    <where>

       call_letter in

       <foreach collection="map" item="item" index="index" open="(" separator="," close=")">

            #{item}

       </foreach>

    </where>

</select>


结论:    parameterType没有关系;

     经过@Param修改key.

参考博文:http://blog.csdn.net/isea533/article/details/21237175