如果要在一个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