java批量修改foreach_springboot+Mybatis批量改foreach抛出java.sql.SQLSyntaxErrorException

博客围绕Java批量修改时的SQL语法错误展开。连接MySQL数据库执行多条query语句报错,虽检查语法无误,但报错指向分号后第二条语句。判断是数据源默认只允许单条语句执行,解决办法是在数据源URL后添加allowMultiQueries=true,并给出批量修改代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Type Exception Report

Message Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is 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 t_yydxx_gjc a

set a.name = '找到工作,不需要'

' at line 5

### The error may involve com.wawa.dao.newdao.VcgeekDao.updateNoname-Inline

### The error occurred while setting parameters

### SQL: update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.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 t_yydxx_gjc a

set a.name = '找到工作,不需要'

' at line 5

; 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 t_yydxx_gjc a

set a.name = '找到工作,不需要'

' at line 5

org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)

org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)

javax.servlet.http.HttpServlet.service(HttpServlet.java:634)

org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)

javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

com.wawa.service.system.HeaderFilter.doFilter(HeaderFilter.java:46)

org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)

org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)

Root Cause

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 t_yydxx_gjc a

set a.name = '找到工作,不需要'

' at line 5

### The error may involve com.wawa.dao.newdao.VcgeekDao.updateNoname-Inline

### The error occurred while setting parameters

### SQL: update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.id = ? ; update t_yydxx_gjc a set a.name = ? where a.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 t_yydxx_gjc a

set a.name = '找到工作,不需要'

' at line 5

; 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 t_yydxx_gjc a

set a.name = '找到工作,不需要'

' at line 5

org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)

org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)

org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)

org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:368)

com.sun.proxy.$Proxy24.update(Unknown Source)

org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:254)

org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:54)

org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)

com.sun.proxy.$Proxy38.updateNoname(Unknown Source)

com.wawa.controller.newYygl.TemplateDesignController.yydUpdateNoname(TemplateDesignController.java:207)

sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

java.lang.reflect.Method.invoke(Method.java:498)

org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)

org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)

org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)

org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)

org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)

com.wawa.utils.spring.ReloadDispatcherServlet.doDispatch(ReloadDispatcherServlet.java:27)

org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)

org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)

org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)

javax.servlet.http.HttpServlet.service(HttpServlet.java:634)

org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)

javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

com.wawa.service.system.HeaderFilter.doFilter(HeaderFilter.java:46)

org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)

org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)

连接的是mysql数据库,检查sql语法并无错误。而此处为 多条query语句,报错信息指向分号后的第二条语句。

判断可能是连接的datasource默认只允许执行单个query语句。

在连接datasource的url后面

加个allowMultiQueries=true

url=jdbc:mysql://localhost:3306/MyDb?characterEncoding=utf8&serverTimezone=GMT%2B8&allowMultiQueries=true

顺便批量修改把代码贴出来

// Dao

int updateNoname(@Param("idName") ArrayList idName);

//Mapper.xml

update t_yydxx_gjc a

set a.name = #{item.name}

where a.id = #{item.id}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值