1.主要用到了一个foreach标签实现sql条件的循环,完成类似批量的sql。

foreach是对一个集合进行遍历,通常是在构建 IN 条件语句的时候。 foreach元素的属性主要有 item,index,collection,open,separator,close。 item:表示集合中每一个元素进行迭代时的别名 index:用于表示在迭代过程中,每次迭代到的位置 collection:指定传入参数的类型 open:开始时拼接的字符串 separator:表示在每次进行迭代之间以什么符号作为分隔符 close:结束时拼接的字符串

1、所传参数为List和普通参数

  • 注意使用@param()注解

mapper层:

void disableUsers(@Param("userIds") List<Integer> userIds,@Param("disable") String disable);

xml:

<update id="disableUsers">
    UPDATE t_user
    SET disable_flag = #{disable}
    WHERE 1 = 1
    <if test="userIds != null and userIds.size > 0">
        AND id IN
        <foreach collection="userIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </if>
</update>

2、所传参数为数组

  • List<FshEstate> getEstateByEstateId(String[] estateIds);

<select id="getEstateByEstateId" parameterType="java.util.List" resultType="com.model.mpublic.FshEstate">
        SELECT *
        FROM fsh_estate est
        WHERE est.estate_id in
        <foreach collection="array" item="estateId" index="index"
                 open="(" close=")" separator=",">
            #{estateId}
        </foreach>
</select>

注意:

(1)如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .

(2)如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .

我遇到的问题,MyBatis传入参数有三个,(list+另外的两个普通参数)

将三个参数封装进实体类里,传参实体类,因为一开始使用了@param(“xxxx”)注解,未对应上xml文件

  • 修改collection=“xxxx.userIds"

或者不使用@param(”xxxx“)注解

正确写法

mapper层:

封装实体类:

 xml层:

参考链接:

MyBatis传入参数为数组、list的写法_秋风吹过的天空的博客-CSDN博客_mybatis入参是list数组

@param(”xxx“)使用方法下篇写

更多推荐

MyBatis传入参数为数组、list的写法