【已解决】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 Windows
【Azure Function】部署Java Function失败:报错deploy [ERROR] Status code 401和警告 'China North 3' may not be a valid region
1:deploy [ERROR] Status code 401, (empty body). 2: China North 3 may not be a valid region,please refer to https://aka.ms/maven_function_configuration#supported-regions for values. 3:  <azure.functions.maven.plugin.version>1.36.0</azure.functions.maven.plugin.version>
38 11
|
3月前
|
SQL Java 数据库连接
canal-starter 监听解析 storeValue 不一样,同样的sql 一个在mybatis执行 一个在数据库操作,导致解析不出正确对象
canal-starter 监听解析 storeValue 不一样,同样的sql 一个在mybatis执行 一个在数据库操作,导致解析不出正确对象
|
3月前
|
SQL 缓存 Java
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
本文详细介绍了MyBatis的各种常见用法MyBatis多级缓存、逆向工程、分页插件 包括获取参数值和结果的各种情况、自定义映射resultMap、动态SQL
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
|
4月前
|
Java
让星星⭐月亮告诉你,Java异常分类[Throwable(Error/Exception(RuntimeException/其他异常)) 检查时异常 非检查时异常]
本文深入解析了Java异常处理机制,重点介绍了`Throwable`类及其子类`Error`和`Exception`,并通过实例代码、流程图和表格详细解释了异常的分类、区别及处理方法,帮助读者掌握异常处理的关键技巧,提升程序的稳定性和健壮性。
117 1
|
4月前
|
SQL Java 数据库连接
mybatis使用四:dao接口参数与mapper 接口中SQL的对应和对应方式的总结,MyBatis的parameterType传入参数类型
这篇文章是关于MyBatis中DAO接口参数与Mapper接口中SQL的对应关系,以及如何使用parameterType传入参数类型的详细总结。
87 10
|
4月前
|
Java
Error:java: 无效的目标发行版: 11解决方案
Error:java: 无效的目标发行版: 11解决方案
127 0
|
5月前
|
SQL 数据库
SQL error : “No query“问题参考
本文介绍了解决SQL中"No query"错误的步骤,包括错误提示、正确的SQL语句写法,以及更多相关参考信息。错误的原因是在构建更新语句时字段赋值之间缺少逗号,导致SQL解析失败。文章还提供了正确格式的SQL语句和相关错误处理的参考链接。
SQL error : “No query“问题参考
|
5月前
|
关系型数据库 MySQL Nacos
nacos启动报错 load derby-schema.sql error
这篇文章描述了作者在使用Nacos时遇到的启动错误,错误提示为加载derby-schema.sql失败,作者通过将数据库从Derby更换为MySQL解决了问题。
nacos启动报错 load derby-schema.sql error
|
7月前
|
SQL Java 数据库连接
mybatis动态SQL常用语法总结
MyBatis 使用 OGNL 表达式语言处理动态SQL,如 `if` 标签进行条件判断,`choose`、`when`、`otherwise` 实现多条件选择,`where`、`set` 管理SQL关键字,`trim` 提供通用修剪功能,`foreach` 遍历集合数据。`sql` 和 `include` 用于代码重用,`selectKey` 处理插入后的返回值。参数传递支持匿名、具名、列表、Map、Java Bean和JSON方式。注意SQL转义及使用合适的jdbcType映射Java类型。
138 7
|
8月前
|
SQL Java 数据库连接
MyBatis动态SQL
MyBatis动态SQL
78 0