场景:我有一张基本表存放的是设备的id,名称,价格。现在我有一个设备id的数组。现在要将这个数组里面的设备的价格全部改成传入的price,如果有传名称,则将名称也改成传入的参数。用mybatis实现。
涉及到的知识点:
if
和set
联合使用,用于更新部分字段- 使用
foreach
可以批量更新一个数组里面元素。
<update id="updateDevice" parameterType="DeviceUpdateCommand">
update device_t
<set>
price = #{price},
<if test="name!=null">
name = #{name},
</if>
</set>
where device_id in
<foreach item="deviceId" collection="deviceIds" open="(" separator="," close=")">
#{deviceId}
</foreach>
</update>
其中 DeviceUpdateCommand 是自定义的一个java类
public DeviceUpdateCommand {
private String name;
private Integer price;
private List<String> deviceIds;
}