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

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

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

<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>

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

相关文章
|
1月前
|
Java 开发者 UED
【实战宝典】Java异常处理大师级教程:throws关键字,让异常声明成为你的专属标签!
【实战宝典】Java异常处理大师级教程:throws关键字,让异常声明成为你的专属标签!
40 3
|
11天前
|
Java 编译器 数据库连接
Java——异常
在 Java 中,程序执行过程中的不正常行为被称为异常。异常分为 Error 和 Exception。Error 表示系统级错误,而 Exception 则封装程序可能出现的问题。异常包括编译时异常和运行时异常(如数组越界)。异常可用于查找 bug 信息和作为方法内部的特殊返回值。处理异常的方式有默认处理和捕获异常,后者通过 try-catch 结构实现。此外,还可以自定义异常类来更灵活地处理特定情况。
25 9
Java——异常
|
14天前
|
安全 Java API
【Java面试题汇总】Java基础篇——String+集合+泛型+IO+异常+反射(2023版)
String常量池、String、StringBuffer、Stringbuilder有什么区别、List与Set的区别、ArrayList和LinkedList的区别、HashMap底层原理、ConcurrentHashMap、HashMap和Hashtable的区别、泛型擦除、ABA问题、IO多路复用、BIO、NIO、O、异常处理机制、反射
【Java面试题汇总】Java基础篇——String+集合+泛型+IO+异常+反射(2023版)
|
22小时前
|
Java 编译器 索引
|
1天前
|
IDE Java 开发工具
java自定义异常20
java自定义异常20
|
1天前
|
IDE Java 开发工具
java捕获异常19
java捕获异常19
|
1月前
|
人工智能 小程序 Java
【Java】throw异常后代码还执行吗?80%小伙伴竟然不知道
本文通过具体的Java代码示例,探讨了Java异常处理机制下的程序流程变化,包括未使用try-catch时异常导致流程中断、使用try-catch捕获异常后的不同执行路径、循环中的异常处理以及throw抛出异常后的代码执行情况。总结了异常处理的关键点,强调了finally块的重要性。
46 4
【Java】throw异常后代码还执行吗?80%小伙伴竟然不知道
|
22天前
|
SQL Java 数据库连接
Mybatis的Cursor如何避免OOM异常
在 Mybatis 中,`Cursor` 是一个特殊对象,用于避免大量数据查询时导致的 OOM 错误。它通过懒加载和迭代器实现内存友好型数据处理,尤其适用于大规模数据查询。使用时只需将 Mapper 文件中的方法返回值设为 `Cursor&lt;T&gt;`。其原理在于操作原生 `Statement` 并按需获取数据,而非一次性加载所有数据,从而避免内存溢出。
|
26天前
|
Java API 开发者
代码小妙招:用Java轻松获取List交集数据
在Java中获取两个 `List`的交集可以通过 `retainAll`方法和Java 8引入的流操作来实现。使用 `retainAll`方法更为直接,但会修改原始 `List`的内容。而使用流则提供了不修改原始 `List`、更为灵活的处理方式。开发者可以根据具体的需求和场景,选择最适合的方法来实现。了解和掌握这些方法,能够帮助开发者在实际开发中更高效地处理集合相关的问题。
18 1
|
29天前
|
Java 数据库连接 测试技术
SpringBoot 3.3.2 + ShardingSphere 5.5 + Mybatis-plus:轻松搞定数据加解密,支持字段级!
【8月更文挑战第30天】在数据驱动的时代,数据的安全性显得尤为重要。特别是在涉及用户隐私或敏感信息的应用中,如何确保数据在存储和传输过程中的安全性成为了开发者必须面对的问题。今天,我们将围绕SpringBoot 3.3.2、ShardingSphere 5.5以及Mybatis-plus的组合,探讨如何轻松实现数据的字段级加解密,为数据安全保驾护航。
81 1