mysql关键字引发错误:org.springframework.jdbc.BadSqlGrammarException

本文介绍了在使用MyBatis-Plus时遇到的SQL语法错误,重点在于解决因将MySQL关键字误用作字段名导致的问题,提供了解决方案和排错步骤,包括检查XML配置和避免关键字冲突。

🎈1 参考文档

mysql关键字简介(附简单使用说明) | 荒野漫步者-优快云


🔍2 问题描述

org.springframework.jdbc.BadSqlGrammarException: 
### Error querying 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 'leave WHERE is_delete = 0' at line 1
### The error may exist in com/hyc/nsms/mapper/LeaveMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT COUNT(*) AS total FROM leave WHERE is_delete = 0
### 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 'leave WHERE is_delete = 0' at line 1;
......

💡3 排错思路

3.1 xml问题

  1. 一开始看到日志的这句:### The error may exist in com/hyc/nsms/mapper/LeaveMapper.java (best guess)。查看mapper层的文件,但我使用的是mybatis-plusmapper层里面根本没有写任何内容。
  2. 以为是target里面没有生成xml文件。查看target之后发现生成了xml文件,即使重新打包后,还是出现同样的错误。
  3. 百度了很多关于xml相关的问题,发现都没有用。

3.2 sql语句错误

  1. 根据日志这句:check the manual that corresponds to your MySQL server version for the right syntax to use near 'leave WHERE is_delete = 0' at line 1

  2. 输入SELECT COUNT(*) AS total FROM leave WHERE is_delete = 0查询,发现报错。

    在这里插入图片描述

  3. 输入SELECT COUNT(*) AS total FROM leave WHERE is_delete = 0查询,发现查询成功。

    在这里插入图片描述

  4. 最后发现问题:粗心大意,字段不能是MySQL关键字


🚀四、解决方案

  1. 将表名leave改成其他,如:ask_for_leave
  2. 查看其他MySQL关键字,避免错误。
com.longshare.ta.common.exception.TaException: java.util.concurrent.CompletionException: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: ORA-00911: invalid character at com.longshare.ta.batch.batch.tasklet.io.DataExportTaskletConfig.executeExport(DataExportTaskletConfig.java:1242) at com.longshare.ta.batch.batch.tasklet.io.DataExportTaskletConfig.executeCustodyExport(DataExportTaskletConfig.java:1062) at com.longshare.ta.batch.batch.tasklet.io.DataExportTaskletConfig.lambda$kjqsExportTasklet$43(DataExportTaskletConfig.java:824) at jdk.internal.reflect.GeneratedMethodAccessor1855.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:137) at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:124) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:241) at jdk.proxy2/jdk.proxy2.$Proxy712.execute(Unknown Source) at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:407) at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:331) at org.springframework.transaction什么意思
最新发布
10-24
像这种报错日志2025-08-04 09:37:16.330 ; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your OceanBase version for the right syntax to use near 'IN' at line 40 2025-08-04 09:37:16.330 ### Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your OceanBase version for the right syntax to use near 'IN' at line 40 2025-08-04 09:37:16.330 ### SQL: SELECT COUNT(*) FROM medical_record_prescription_detail mrpd LEFT JOIN medical_record_prescription mrp ON mrpd.record_prescription_id = mrp.id LEFT JOIN medical_record mr ON mrp.record_id = mr.id LEFT JOIN order_prescription op ON op.record_prescription_id=mrp.id LEFT JOIN order_prescription_detail opd on opd.prescription_detail_id=mrpd.id WHERE op.is_deleted = 0 and mr.is_deleted = 0 and mrp.is_deleted = 0 AND op.store_code IN 2025-08-04 09:37:16.330 ### The error occurred while setting parameters 2025-08-04 09:37:16.330 ### The error may involve defaultParameterMap 2025-08-04 09:37:16.330 ### The error may exist in URL [jar:file:/app.jar!/BOOT-INF/classes!/mybatisplus/prescription/MedicalRecordPrescriptionMapper.xml] 2025-08-04 09:37:16.330 ### Error querying database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your OceanBase version for the right syntax to use near 'IN' at line 40 2025-08-04 09:37:16.330 Caused by: org.springframework.jdbc.BadSqlGrammarException: 2025-08-04 09:37:16.330 at java.lang.Thread.run(Thread.java:748) 2025-08-04 09:37:16.330 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 2025-08-04 09:37:16.330 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 2025-08-04 09:37:16.330 at java.util.concurrent.FutureTask.run(FutureTask.java:266) 2025-08-04 09:37:16.330 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 2025-08-04 09:37:16.330 at org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService$ConsumeRequest.run(ConsumeMessageConcurrentlyService.java:402) 2025-08-04 09:37:16.330 at com.alibaba.cloud.stream.binder.rocketmq.integration.inbound.RocketMQInboundChannelAdapter.lambda$onInit$5(RocketMQInboundChannelAdapter.java:124) 2025-08-04 09:37:16.330 at com.alibaba.cloud.stream.binder.rocketmq.integration.inbound.RocketMQInboundChannelAdapter.consumeMessage(RocketMQInboundChannelAdapter.java:162) 2025-08-04 09:37:16.330 at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:180) 2025-08-04 09:37:16.330 at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:287) 2025-08-04 09:37:16.330 at com.alibaba.cloud.stream.binder.rocketmq.integration.inbound.RocketMQInboundChannelAdapter.lambda$consumeMessage$6(RocketMQInboundChannelAdapter.java:163) 2025-08-04 09:37:16.330 at org.springframework.integration.endpoint.MessageProducerSupport.sendMessage(MessageProducerSupport.java:208) 2025-08-04 09:37:16.330 at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:109) 2025-08-04 09:37:16.330 at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47) 2025-08-04 09:37:16.330 at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:166) 2025-08-04 09:37:16.330 at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:187) 2025-08-04 09:37:16.330 at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:520) 2025-08-04 09:37:16.330 at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:570) 2025-08-04 09:37:16.330 at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:72) 2025-08-04 09:37:16.330 一般是查看Caused by: org.springframework.jdbc.BadSqlGrammarException: 后面的报错还是查看Caused by: org.springframework.jdbc.BadSqlGrammarException: 上面的报错日志
08-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值