springboot + mybatis实现批量更新操作

今天遇到一个需要执行批量更新的操作,原先一直没有写过,今天实现了,记录一下:

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;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值