【已解决】Mybatis 批量动态更新数据时出现异常:java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax

🎉记录一个在应用场景下遇到的问题,给出的需求是批量对用户表进行更新操作。需要说明的是,使用的数据库为 MySQL,ORM 框架为 Mybatis.

需求本身并不难实现,很容易地便可以写出批量更新语句,代码如下:

<update id="updateUserBatch" parameterType="java.util.List">
    <foreach collection="list" item="user" separator=";">
        update sys_user
        <set>
            <if test="user.userCode != null and user.userCode != ''">
                user_code = #{user.userCode},
            </if>
            <if test="user.userAccount != null and user.userAccount != ''">
                user_account = #{user.userAccount},
            </if>
            <if test="user.userName != null and user.userName != ''">
                user_name = #{user.userName},
            </if>
            <if test="user.email != null and user.email != ''">
                email = #{user.email},
            </if>
            <if test="user.deptCode != null and user.deptCode != ''">
                dept_code = #{user.deptCode},
            </if>
            <if test="user.status != null">
                status = #{user.status},
            </if>
            <if test="user.createTime != null and user.createTime != ''">
                create_time = #{user.createTime},
            </if>
            <if test="user.updateTime != null and user.updateTime != ''">
                update_time = #{user.updateTime},
            </if>
        </set>
        where user_id = #{user.userId}
    </foreach>
</update>

从上面的代码可以看出,这里使用了 <foreach collection="list" item="item" separator=";">...</foreach> 对传入的列表进行遍历更新。

其中,collection="list" 表示传入的参数是列表类型,item="user" 其中的 user 是自定义的名称(你也可以指定item="item" 这样的形式),表示单个更新的形参对象(Java 对象 User 有很多属性和表的字段对应),separator=";" 表示指定分隔符,对于批量更新操作而言分隔符参数必须是;,固定格式不能改变。如果是批量插入,分隔符参数则为, ,这也是固定的格式。

但是,在项目运行并执行更新用户操作代码时出现了异常,异常信息如下所示:

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 sys_user ...‘

在反复确认 SQL 语句没有写错的情况下,经过一番折腾才知道:如果使用 Mybatis 框架对 MySQL 数据库进行批量更新或者插入操作,需要在连接数据库的 URL 加上 allowMultiQueries=true,这样便可以执行批处理操作了。

示例 URL 如下所示

 jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true

最后,顺便再补充一下批量插入的语句:

<insert id="insertUserBatch" parameterType="java.util.List">
    insert into sys_user (
        user_code,
        user_account,
        user_name,
        email,
        dept_code,
        status,
        create_time,
        update_time
    )
    values
    <foreach collection="list" item="user" separator=",">
        (
            <if test="userCode != null and userCode != ''">
                user_code = #{user.userCode},
            </if>
            <if test="user.userAccount != null and user.userAccount != ''">
                user_account = #{user.userAccount},
            </if>
            <if test="user.userName != null and user.userName != ''">
                user_name = #{user.userName},
            </if>
            <if test="user.email != null and user.email != ''">
                email = #{user.email},
            </if>
            <if test="user.deptCode != null and user.deptCode != ''">
                dept_code = #{user.deptCode},
            </if>
            <if test="user.status != null">
                status = #{user.status},
            </if>
            <if test="user.createTime != null and user.createTime != ''">
                create_time = #{user.createTime},
            </if>
            <if test="user.updateTime != null and user.updateTime != ''">
                update_time = #{user.updateTime},
            </if>
        )
    </foreach>
</insert>

至此,问题得到解决,可以早点干饭啦!

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReadThroughLife

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值