mybatis 批量更新元素的部分字段

场景:我有一张基本表存放的是设备的id,名称,价格。现在我有一个设备id的数组。现在要将这个数组里面的设备的价格全部改成传入的price,如果有传名称,则将名称也改成传入的参数。用mybatis实现。

涉及到的知识点:

  • ifset联合使用,用于更新部分字段
  • 使用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;
}

猜你喜欢

转载自blog.csdn.net/qq_43720551/article/details/131386586