刚接触mybaties 需要执行批量更新。
具体需求是,前端给我一个list,将list中的内容根据id在数据库中跟新数据。
要更新的list实体类为:
public class SysParamEntity { private int id; private String pcode; private String label; private String pvalue; private String remark; private String creater; private Date createdDate; private String modifier; private Date modifiedDate; }
要根据id批量跟新pvalue,modifier,modifiedDate,mapper.xml文件如下
<update id="batchUpdate"> update direct_bank_sys_param <trim prefix="set" suffixOverrides=","> <trim prefix="pvalue =case" suffix="end,"> <foreach collection="list" item="i" index="index"> <if test="i.pvalue!=null"> when id=#{i.id} then #{i.pvalue} </if> </foreach> </trim> <trim prefix=" modified_date =case" suffix="end,"> <foreach collection="list" item="i" index="index"> <if test="i.modifiedDate!=null"> when id=#{i.id} then #{i.modifiedDate} </if> </foreach> </trim> <trim prefix=" modifier =case" suffix="end,"> <foreach collection="list" item="i" index="index"> <if test="i.modifier!=null"> when id=#{i.id} then #{i.modifier} </if> </foreach> </trim> </trim> where <foreach collection="list" separator="or" item="i" index="index"> id=#{i.id} </foreach> </update>