今天遇到一个需要执行批量更新的操作,原先一直没有写过,今天实现了,记录一下:
List<TEmployee> batchUpdateEmployeeList = new ArrayList<>();
TEmployee employee = new TEmployee();
employee.setCode("1");
employee.setName("a");
TEmployee employee1 = new TEmployee();
employee1.setCode("2");
employee1.setName("b");
List<TEmployee> employeeList = new ArrayList<>();
employeeList.add(employee);
employeeList.add(employee1);
调用传值为list即可 Dao层代码 int batchUpdate(List<TEmployee> employeeList)
@Override
public int batchUpdate(List<TEmployee> employeeList) {
return employeeExtendMapper.batchUpdate(employeeList);
}
Xml代码实现:
<update id="batchUpdate" parameterType="java.util.List">
update td_employee
<trim prefix="set" suffixOverrides=",">
<trim prefix="name=case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.name!=null">
when code=#{i.code} then #{i.name}
</if>
</foreach>
</trim>
<trim prefix="face_picture_url=case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.facePictureUrl!=null">
when code=#{i.code} then #{i.facePictureUrl}
</if>
</foreach>
</trim>
<trim prefix="is_feature=case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.isFeature!=null">
when code=#{i.code} then #{i.isFeature}
</if>
</foreach>
</trim>
<trim prefix="feature=case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.feature!=null">
when code=#{i.code} then #{i.feature}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index">
code=#{i.code}
</foreach>
</update>
实体类:
@Data
public class TEmployee implements Serializable {
private static final long serialVersionUID = -23158537252788377L;
private Integer id;
private String code;
private String name;
private String facePictureUrl;
private Integer isFeature;
private Object feature;
private String algorithmVersion;
private String comment;
private String createUser;
private LocalDateTime createTime;
private String updateUser;
private LocalDateTime updateTime;
private String deviceName;
}
本文分享了使用MyBatis实现批量更新操作的方法,通过示例代码详细介绍了如何构造批量更新的List参数,并展示了Dao层及XML映射文件的具体实现。
2万+





