mybatis传入List实现批量更新

通过在jdbc:url设置allowMultiQueries=true,允许Mybatis批量执行多条更新语句,避免SQL语法错误。详细解释了Mapper接口内代码的使用,并提供了关于Mybatis中foreach用法的参考链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果要在一个update里面执行多条更新语句,只需要在jdbc:url后面跟上allowMultiQueries=true的参数,比如:

spring.datasource.url=jdbc:mysql://localhost:3306/wang_last?allowMultiQueries=true

不加这一句,即使mybatis里面的查询语句没有问题,也会报错:

Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update
viscosity_fitting set
viscosity_id = 2,
’ at line 12

下面附上xml里面的代码

  <update id="updateViscosityFittings" parameterType="com.cup.wang.airport.model.ViscosityFitting" useGeneratedKeys="true" keyProperty="id">
       <foreach collection="viscosityFittings" item="item" index="index" open="" close="" separator=";">
           update
           viscosity_fitting set
               <trim prefix="" suffix=" " suffixOverrides=",">
                   <if test="item.viscosityId != null">
                       viscosity_id = #{item.viscosityId,jdbcType=INTEGER},
                   </if>
                   <if test="item.viscosity != null">
                       viscosity = #{item.viscosity,jdbcType=DOUBLE},
                   </if>
                   <if test="item.temperature != null">
                       temperature = #{item.temperature,jdbcType=DOUBLE},
                   </if>
               </trim>
           where id = #{item.id}
       </foreach>
   </update>

Mapper接口内的代码如下:

int updateViscosityFittings(List<ViscosityFitting> viscosityFittings);

Mybatis中foreach的具体用法见:

https://blog.csdn.net/weixin_30321449/article/details/95711964?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.channel_param

以及:

https://blog.csdn.net/weixin_43564923/article/details/94390954?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值