mybatis批量更新报错解决办法

一、问题描述

前段时间主要再用spring data jpa,最近又回到了mybatis的使用上,发现好多东西都忘了。
然后编写批量更新语句时候,发现老是更新失败,但是从控制台获取的sql语句又能够在数据库中正常执行,这个小坑让我花了些时间,为了避免以后再犯错,在此这里来记录下解决办法。

二、报错信息

注:在使用本方案解决问题之前,你首先要确定你的批量更新sql是否是正确的,如何确认呢?很简单把控制台打印的语句参数替换后,放到数据库执行一下,看是否能成功执行。
如果你使用的是开发工具是IDEA的话,顺便推荐一款查看执行sql的插件:MyBatis Log Plugin。
可以使用IDEA直接安装使用。

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  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 mall_product

三、项目代码

ProductMapper:

public interface ProductMapper {
    
    int batchUpdate(@Param("products") List<Product> products);
    
}

Mybatis Sql:

<update id="batchUpdate" parameterType="list">
        <foreach collection="products" item="item" index="index" open="" close="" separator=";">
            update mall_product
            <set>
                <if test="item.categoryId != null">
                    category_id = #{item.categoryId,jdbcType=INTEGER},
                </if>
                <if test="item.name != null">
                    name = #{item.name,jdbcType=VARCHAR},
                </if>
                <if test="item.subtitle != null">
                    subtitle = #{item.subtitle,jdbcType=VARCHAR},
                </if>
                <if test="item.mainImage != null">
                    main_image = #{item.mainImage,jdbcType=VARCHAR},
                </if>
                <if test="item.subImages != null">
                    sub_images = #{item.subImages,jdbcType=VARCHAR},
                </if>
                <if test="item.detail != null">
                    detail = #{item.detail,jdbcType=VARCHAR},
                </if>
                <if test="item.price != null">
                    price = #{item.price,jdbcType=DECIMAL},
                </if>
                <if test="item.stock != null">
                    stock = #{item.stock,jdbcType=INTEGER},
                </if>
                <if test="item.status != null">
                    status = #{item.status,jdbcType=INTEGER},
                </if>
            </set>
            where id = #{item.id,jdbcType=INTEGER}
        </foreach>
    </update>

四、解决方案

其实这个错误就是源于mybatis默认是不支持批量更新的,

如果想要进行批量更新,需要在连接数据库的地址上加一个配置,内容如下:

allowMultiQueries=true

我的application.yml中关于数据库的配置。

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    #MySQL配置
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mall?allowMultiQueries=true&useUnicode=yes&characterEncoding=UTF-8&useInformationSchema=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
    username: root
    password: 123456

改好了之后,就可以重新尝试了,小伙伴一定要保证你的sql本身没有错误啊。

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值