mybatis批量更新不同值的字段

<mapper namespace=" " >

 <update id="updateByMobile" parameterType="java.util.List">
    update pb_vip_table 
    set  integral=
    <foreach collection="list" item="item" index="index" 
        separator=" " open="CASE mobile" close="end">
        when #{item.mobile} then #{item.integral}
    </foreach>
    where mobile in
    <foreach collection="list" index="index" item="item" 
        separator="," open="(" close=")">
        #{item.mobile,jdbcType=BIGINT}
    </foreach>
 </update>

</mapper>


原生sql批量更新语句可以看我的上一篇文章,以便更好理解mybatis批量更新。
最好将原生sql语句与mybatis语句进行对比,这样就很容易理解mybatis的一些属性的含义了。

说明:

id=" "名字自己起,我这里起名为updateByMobile,这个是用于dao接口的方法名

parameterType传递的参数类型,我这里传递的参数是数据列表,所以用java.util.List

pb_vip_table是我要更新的表的表名

integral是我要更新的字段,这里我只更新一个字段,更新多个字段类似,用end连接

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。
foreach元素的属性主要有 item,index,collection,open,separator,close。
item集合中每一个元素进行迭代时的别名,
index表示在迭代过程中,每次迭代到的位置,
open该语句以什么开始,
separator在每次进行迭代之间以什么符号作为分隔 符,
close以什么结束,
collection属性,在不同情况 下,该属性的值是不一样的,
主要有一下3种情况:
1.     如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2.     如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3.     如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了



猜你喜欢

转载自blog.csdn.net/qinyf2015/article/details/78692788