mybatis对于list更新sql语句写法
批量更新
<update id="updateConfigureNames" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open=""
close="" separator=";">
update configure
<set>
configure_name=#{item.configureName}
</set>
where id = #{item.id,jdbcType=VARCHAR}
</foreach>
</update>
在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可