MyBatis_foreach_list_array_map

MyBatis中foreach传入参数为list、数组、map的不同写法

list

<!--List:forech中的collection属性类型是List,collection的值必须是:list,item的值可以随意,Dao接口中参数名字随意 -->  
    <select id="getIteamsList" resultType="Employees">  
        select *  
        from EMPLOYEES e  
        where e.EMPLOYEE_ID in  
        <foreach collection="list" item="employeeId" index="index"  
            open="(" close=")" separator=",">  
            #{employeeId}  
        </foreach>  
    </select>  
  

array

<!--Array:forech中的collection属性类型是array,collection的值必须是:array,item的值可以随意,
Dao接口中参数名字随意 -->  
    <select id="getItemsArray" resultType="Employees">  
        select *  
        from EMPLOYEES e  
        where e.EMPLOYEE_ID in  
        <foreach collection="array" item="employeeId" index="index"  
            open="(" close=")" separator=",">  
            #{employeeId}  
        </foreach>  
    </select>  
  

map

 <!--Map:forech中的collection属性是map.key-->  
    <select id="getItemsMap" resultType="Employees">  
        select *  
        from EMPLOYEES e  
        <where>  
            <if test="departmentId!=null and departmentId!=''">  
                e.DEPARTMENT_ID=#{departmentId}  
            </if>  
            <if test="employeeIdsArray!=null and employeeIdsArray.length!=0">  
                AND e.EMPLOYEE_ID in  
                <foreach collection="employeeIdsArray" item="employeeId"  
                    index="index" open="(" close=")" separator=",">  
                    #{employeeId}  
                </foreach>  
            </if>  
        </where>  
    </select> 

foreach标签内list为空 判断
方法一

<if test="list != null and list.size() > 0">
  _claimreview.id in
  <foreach collection="list" index="index" item="item"  open="(" separator="," close=")">  
    #{item}
  </foreach>
</if>

方法二(推荐)

<if test="list != null and list.size() > 0">
  _claimreview.id in
  <foreach collection="list" index="index" item="item"  open="(" separator="," close=")">  
    #{item}
  </foreach>
</if>

https://blog.csdn.net/m0_37692562/article/details/106378384
https://icecoola.blog.csdn.net/article/details/78036304

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页