问题

  MyBatis 批量 update 更新数据时,SQL 语句并没有错,但还报错, SQL 语句如下:

1    <!-- 批量更新,通过接收传进来的参数list进行循环着组装sql -->
2    <update id="updateListOther" parameterType="java.util.List">
3        <foreach collection="list" item="value" index="index" open="" close="" separator=";">
4            update picture_bed.other set content = #{value.content} where id = #{value.id}
5        </foreach>
6    </update>

报错信息

 1Caused by: org.springframework.jdbc.BadSqlGrammarException: 
 2### 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 picture_bed.other set content = '111' where id = 2' at line 3
 3### The error may exist in file [C:\software\IntelliJ IDEA Workspace\picture_bed_back_end\target\classes\mapper\OtherDao.xml]
 4### The error may involve top.zyxwmj.picture_bed_back_end.dao.OtherDao.updateListOther-Inline
 5### The error occurred while setting parameters
 6### SQL: update picture_bed.other set content = ? where id = ?          ;              update picture_bed.other set content = ? where id = ?
 7### 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 picture_bed.other set content = '111' where id = 2' at line 3
 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 picture_bed.other set content = '111' where id = 2' at line 3
 9	at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:235)
10	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
11	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:88)
12	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
13	at com.sun.proxy.$Proxy87.update(Unknown Source)
14	at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:287)
15	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:67)
16	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:93)
17	at com.sun.proxy.$Proxy89.updateListOther(Unknown Source)
18	at top.zyxwmj.picture_bed_back_end.service.impl.OtherServiceImpl.updateOther(OtherServiceImpl.java:50)
19	at top.zyxwmj.picture_bed_back_end.controller.background.AdminOtherController.updateOther(AdminOtherController.java:51)
20	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
21	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
22	at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
23	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45009)
24	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45012)
25	at java.lang.reflect.Method.invoke(Method.java:498)
26	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
27	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
28	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
29	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
30	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
31	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
32	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
33	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
34	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
35	... 80 more
36Caused by: 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 picture_bed.other set content = '111' where id = 2' at line 3
37	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
38	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
39	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
40	at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:970)
41	at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:387)
42	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3051)
43	at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
44	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3049)
45	at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:167)
46	at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:498)
47	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
48	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
49	at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
50	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45009)
51	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45012)
52	at java.lang.reflect.Method.invoke(Method.java:498)
53	at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59)
54	at com.sun.proxy.$Proxy91.execute(Unknown Source)
55	at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:47)
56	at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
57	at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
58	at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
59	at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
60	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
61	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
62	at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
63	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45009)
64	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45012)
65	at java.lang.reflect.Method.invoke(Method.java:498)
66	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
67	at com.sun.proxy.$Proxy125.update(Unknown Source)
68	at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)
69	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
70	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
71	at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
72	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45009)
73	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45012)
74	at java.lang.reflect.Method.invoke(Method.java:498)
75	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426)
76	... 102 more

解决方法

  在连接数据库的 url 后边加上 &allowMultiQueries=true 就 OK 了。

1jdbc:mysql://localhost:3306/picture_bed?serverTimezone=GMT%2B8&useSSL=false&serverTimezone=UTC&allowMultiQueries=true

标题:MyBatis 批量执行 SQL 语句出错
作者:Yi-Xing
地址:http://zyxwmj.top/articles/2020/08/27/1598512990658.html
博客中若有不恰当的地方,请您一定要告诉我。前路崎岖,望我们可以互相帮助,并肩前行!