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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 解决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
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
6月前
|
XML Java 数据库连接
解决在mybatis中使用class属性绑定映射文件出现的异常问题~
解决在mybatis中使用class属性绑定映射文件出现的异常问题~
|
SQL XML 安全
mybatis批量更新数据三种方法效率对比【Mysql】
mybatis批量更新数据三种方法效率对比【Mysql】
1612 0
mybatis批量更新数据三种方法效率对比【Mysql】
|
6月前
|
关系型数据库 Java Go
解决 MyBatis-Plus + PostgreSQL 中的 org.postgresql.util.PSQLException 异常
解决 MyBatis-Plus + PostgreSQL 中的 org.postgresql.util.PSQLException 异常
364 0
|
6月前
|
SQL Java 数据库
【Mybatis-plus异常】mybatis-plus updateById方法更新不了空字符串和null的解决方法
【Mybatis-plus异常】mybatis-plus updateById方法更新不了空字符串和null的解决方法
137 0
|
4月前
|
SQL 存储 Java
MyBatis【付诸实践 02】 mapper文件未编译+statementType使用+返回结果字段顺序不一致+获取自增ID+一个update标签批量更新记录
MyBatis【付诸实践 02】 mapper文件未编译+statementType使用+返回结果字段顺序不一致+获取自增ID+一个update标签批量更新记录
34 0
|
9月前
|
存储 SQL 缓存
【问题处理】—— Mybatis缓存可改导致的异常
【问题处理】—— Mybatis缓存可改导致的异常
93 0
|
6月前
|
Java 数据库连接 mybatis
【Mybatis异常】nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter
【Mybatis异常】nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter
171 0
|
10月前
|
SQL 数据库
springboot+mybatis,mapper调用查询的数据为空,报空指针异常
springboot+mybatis,mapper调用查询的数据为空,报空指针异常
690 0
|
XML SQL Java
mybatis-plus异常记录:org.apache.ibatis.binding.BindingException Invalid bound statement
mybatis-plus异常记录:org.apache.ibatis.binding.BindingException Invalid bound statement
1189 0
mybatis-plus异常记录:org.apache.ibatis.binding.BindingException Invalid bound statement
|
12月前
|
SQL Java 关系型数据库
Mybatis版本升级导致OffsetDateTime入参解析异常问题
最近有一个数据统计服务需要升级SpringBoot的版本,由1.5.x.RELEASE直接升级到2.3.0.RELEASE,考虑到没有用到SpringBoot的内建SPI,升级过程算是顺利。但是出于代码洁癖和版本洁癖,看到项目中依赖的MyBatis的版本是3.4.5,相比当时的最新版本3.5.5大有落后,于是顺便把它升级到3.5.5