【已解决】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>

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

相关文章
|
11月前
|
SQL XML Java
通过MyBatis的XML配置实现灵活的动态SQL查询
总结而言,通过MyBatis的XML配置实现灵活的动态SQL查询,可以让开发者以声明式的方式构建SQL语句,既保证了SQL操作的灵活性,又简化了代码的复杂度。这种方式可以显著提高数据库操作的效率和代码的可维护性。
591 18
|
11月前
|
SQL 人工智能 JSON
Flink 2.1 SQL:解锁实时数据与AI集成,实现可扩展流处理
本文整理自阿里云的高级技术专家、Apache Flink PMC 成员李麟老师在 Flink Forward Asia 2025 新加坡[1]站 —— 实时 AI 专场中的分享。将带来关于 Flink 2.1 版本中 SQL 在实时数据处理和 AI 方面进展的话题。
635 0
Flink 2.1 SQL:解锁实时数据与AI集成,实现可扩展流处理
|
11月前
|
SQL Java 数据库连接
SSM相关问题-1--#{}和${}有什么区别吗?--Mybatis都有哪些动态sql?能简述一下动 态sql的执行原理吗?--Spring支持的几种bean的作用域 Scope
在MyBatis中,`#{}`是预处理占位符,可防止SQL注入,适用于大多数参数传递场景;而`${}`是直接字符串替换,不安全,仅用于动态表名、列名等特殊场景。二者在安全性、性能及使用场景上有显著区别。
499 0
|
11月前
|
SQL 人工智能 JSON
Flink 2.1 SQL:解锁实时数据与AI集成,实现可扩展流处理
简介:本文整理自阿里云高级技术专家李麟在Flink Forward Asia 2025新加坡站的分享,介绍了Flink 2.1 SQL在实时数据处理与AI融合方面的关键进展,包括AI函数集成、Join优化及未来发展方向,助力构建高效实时AI管道。
1264 43
|
12月前
|
Java Shell Maven
【Azure Container App】构建Java应用镜像时候遇无法编译错误:ERROR [build 10/10] RUN ./mvnw.cmd dependency:go-offline -B -Dproduction package
在部署Java应用到Azure Container App时,构建镜像过程中出现错误:“./mvnw.cmd: No such file or directory”。尽管项目根目录包含mvnw和mvnw.cmd文件,但依然报错。问题出现在Dockerfile构建阶段执行`./mvnw dependency:go-offline`命令时,系统提示找不到可执行文件。经过排查,确认是mvnw文件内容异常所致。最终通过重新生成mvnw文件解决该问题,镜像成功构建。
683 1
|
SQL 关系型数据库 MySQL
SQL如何对不同表的数据进行更新
本文介绍了如何将表A的Col1数据更新到表B的Col1中,分别提供了Microsoft SQL和MySQL的实现方法,并探讨了多表合并后更新的优化方式,如使用MERGE语句提升效率。适用于数据库数据同步与批量更新场景。
|
SQL DataWorks 数据管理
SQL血缘分析实战!数据人必会的3大救命场景
1. 开源工具:Apache Atlas(元数据管理)、Spline(血缘追踪) 2. 企业级方案:阿里DataWorks血缘分析、腾讯云CDW血缘引擎 3. 自研技巧:在ETL脚本中植入版本水印,用注释记录业务逻辑变更 📌 重点总结:
|
9月前
|
JSON 网络协议 安全
【Java】(10)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
428 1