mysql 批量更新

    技术2025-07-21  14

    方式一:

    <update id="updateBatchByIds"> update test set state=#{state},name=#{name} where id in( <foreach collection="ids.split(',')" separator="," item="id" open="" close="" index=""> #{id} </foreach> ) </update>

    对应dao层接口

    void updateBatchByIds(@Param(value = "state")Integer state,@Param(value = "name")String name,@Param(value = "ids")String ids);

    方式二:

    <update id="updateBatchByIds"> <foreach collection="ids.split(',')" separator=";" item="id" open="" close="" index=""> Update test <set> state=state ,name=name </set> <where> id=#{id} </where> </foreach> </update>

    但是方式二 可能会出现 这种错误,原因是因为配置的 mysql jdbc 链接字符串 默认不支持一次性执行多个sql 语句

    如果是springboot项目 需要更改druid配置

    把wall参数去掉 然后在 数据库连接后面添加 allowMultiQueries=true

    jdbc:mysql://127.0.0.1/test?characterEncoding=UTF-8&useUnicode=true&useSSL=false&allowMultiQueries=true

    如果不是springboot+druid项目 直接修改数据库连接即可!

    Processed: 0.011, SQL: 9