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

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

相关文章
|
9月前
|
SQL Java 数据库连接
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
|
7月前
|
SQL XML Java
菜鸟之路Day35一一Mybatis之XML映射与动态SQL
本文介绍了MyBatis框架中XML映射与动态SQL的使用方法,作者通过实例详细解析了XML映射文件的配置规范,包括namespace、id和resultType的设置。文章还对比了注解与XML映射的优缺点,强调复杂SQL更适合XML方式。在动态SQL部分,重点讲解了`&lt;if&gt;`、`&lt;where&gt;`、`&lt;set&gt;`、`&lt;foreach&gt;`等标签的应用场景,如条件查询、动态更新和批量删除,并通过代码示例展示了其灵活性与实用性。最后,通过`&lt;sql&gt;`和`&lt;include&gt;`实现代码复用,优化维护效率。
654 5
|
7月前
|
SQL Java 数据库连接
Java中实现SQL分页的方法
无论何种情况,选择适合自己的,理解了背后的工作原理,并能根据实际需求灵活变通的方式才是最重要的。
207 9
|
9月前
|
SQL Java 数据库连接
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
|
9月前
|
SQL 缓存 Java
框架源码私享笔记(02)Mybatis核心框架原理 | 一条SQL透析核心组件功能特性
本文详细解构了MyBatis的工作机制,包括解析配置、创建连接、执行SQL、结果封装和关闭连接等步骤。文章还介绍了MyBatis的五大核心功能特性:支持动态SQL、缓存机制(一级和二级缓存)、插件扩展、延迟加载和SQL注解,帮助读者深入了解其高效灵活的设计理念。
|
11月前
|
SQL NoSQL Java
Java使用sql查询mongodb
通过MongoDB Atlas Data Lake或Apache Drill,可以在Java中使用SQL语法查询MongoDB数据。这两种方法都需要适当的配置和依赖库的支持。希望本文提供的示例和说明能够帮助开发者实现这一目标。
457 17
|
11月前
|
SQL Java 数据库连接
如何在 Java 代码中使用 JSqlParser 解析复杂的 SQL 语句?
大家好,我是 V 哥。JSqlParser 是一个用于解析 SQL 语句的 Java 库,可将 SQL 解析为 Java 对象树,支持多种 SQL 类型(如 `SELECT`、`INSERT` 等)。它适用于 SQL 分析、修改、生成和验证等场景。通过 Maven 或 Gradle 安装后,可以方便地在 Java 代码中使用。
3519 11
|
11月前
|
SQL Java 数据库连接
【潜意识Java】MyBatis中的动态SQL灵活、高效的数据库查询以及深度总结
本文详细介绍了MyBatis中的动态SQL功能,涵盖其背景、应用场景及实现方式。
1246 6
|
11月前
|
SQL Java 数据库连接
如何用 Java 校验 SQL 语句的合法性?
本文介绍了五种校验 SQL 语句合法性的方案:1) 使用 JDBC API 的 `execute()` 方法,通过捕获异常判断合法性;2) 使用 JSqlParser 库解析 SQL 语句为 Java 对象;3) 使用正则表达式检查 SQL 语句格式;4) 使用 ANTLR 生成 SQL 解析器;5) 使用 Apache Calcite 解析 SQL。每种方法各有优劣,具体选择取决于需求和个人偏好。需要注意的是,这些方法仅能校验语法合法性,无法保证语义正确性,仍需防范 SQL 注入攻击。
477 6
|
9月前
|
SQL XML Java
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
278 0