Mybatis批量更新出现BadSqlGrammarException异常解决

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 解决mybatis批量更新出现异常解决方案

1.xml写法 批量更新sql如下

   <update id="updateDemo">
        <foreach collection="demos" item="demo" open="" separator=";" close="" index="index">
            update demo
            <set>
                <if test="null != demo.name">name = #{demo.name},</if>
                <if test="null != demo.age">age= #{demo.age}</if>
            </set>
            <where>id=#{demo.id} </where>
        </foreach>
    </update>

2.执行出现异常

  JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@1500edf3] will be managed by Spring
==>  Preparing: update demo SET name = ?, age= ? WHERE id=? ; update demo SET name = ?, age= ? WHERE id=? 
==> Parameters: 2(Integer), 2(Integer), 1(Integer), 3(Integer), 3(Integer), 2(Integer)
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@11d474a]
Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@11d474a]
Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@11d474a]
org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: 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 demo
             SET name = 3,
                age' at line 8
### The error may exist in file [D:\Demo-server\target\classes\mapper\DemoMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: update demo              SET name = ?,                 age= ?               WHERE id=?                               ;              update demo              SET name = ?,                 age= ?               WHERE id=?
### Cause: 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 app_user
             SET name = 3,
                age' at line 8
; bad SQL grammar []; nested exception is 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 demo
             SET name = 3,
                age' at line 8

3.配置连接数据库的url中后缀中添加以下参数

allowMultiQueries=true //开启批量更新

4.配置结果

jdbc:mysql://xxx:3306/xxx?useSSL=false&characterEncoding=utf8&allowMultiQueries=true
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
XML Java 数据库连接
解决在mybatis中使用class属性绑定映射文件出现的异常问题~
解决在mybatis中使用class属性绑定映射文件出现的异常问题~
|
SQL XML 安全
mybatis批量更新数据三种方法效率对比【Mysql】
mybatis批量更新数据三种方法效率对比【Mysql】
3024 0
mybatis批量更新数据三种方法效率对比【Mysql】
|
1月前
|
SQL Java 数据库连接
Mybatis的Cursor如何避免OOM异常
在 Mybatis 中,`Cursor` 是一个特殊对象,用于避免大量数据查询时导致的 OOM 错误。它通过懒加载和迭代器实现内存友好型数据处理,尤其适用于大规模数据查询。使用时只需将 Mapper 文件中的方法返回值设为 `Cursor&lt;T&gt;`。其原理在于操作原生 `Statement` 并按需获取数据,而非一次性加载所有数据,从而避免内存溢出。
|
关系型数据库 Java Go
解决 MyBatis-Plus + PostgreSQL 中的 org.postgresql.util.PSQLException 异常
解决 MyBatis-Plus + PostgreSQL 中的 org.postgresql.util.PSQLException 异常
1147 0
|
SQL Java Spring
【MybatisPlus异常】The SQL execution time is too large, please optimize
【MybatisPlus异常】The SQL execution time is too large, please optimize
298 0
【MybatisPlus异常】The SQL execution time is too large, please optimize
|
4月前
|
SQL Java 数据库连接
MyBatis SQL 批量更新(代码➕案例)
MyBatis SQL 批量更新(代码➕案例)
807 0
|
SQL Java 数据库
【Mybatis-plus异常】mybatis-plus updateById方法更新不了空字符串和null的解决方法
【Mybatis-plus异常】mybatis-plus updateById方法更新不了空字符串和null的解决方法
550 0
|
5月前
|
Java 关系型数据库 MySQL
整合SpringBoot与MyBatis时报错时区异常
整合SpringBoot与MyBatis时报错时区异常
48 0
|
存储 SQL 缓存
【问题处理】—— Mybatis缓存可改导致的异常
【问题处理】—— Mybatis缓存可改导致的异常
130 0
|
5月前
|
Java 测试技术
使用MybatisPlus时出现的java.lang.NullPointerException异常~
使用MybatisPlus时出现的java.lang.NullPointerException异常~
164 0
使用MybatisPlus时出现的java.lang.NullPointerException异常~