🎈1 参考文档
🔍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问题
- 一开始看到日志的这句:
### The error may exist in com/hyc/nsms/mapper/LeaveMapper.java (best guess)。查看mapper层的文件,但我使用的是mybatis-plus,mapper层里面根本没有写任何内容。 - 以为是
target里面没有生成xml文件。查看target之后发现生成了xml文件,即使重新打包后,还是出现同样的错误。 - 百度了很多关于
xml相关的问题,发现都没有用。
3.2 sql语句错误
-
根据日志这句:
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。 -
输入
SELECT COUNT(*) AS total FROM leave WHERE is_delete = 0查询,发现报错。
-
输入
SELECT COUNT(*) AS total FROM leave WHERE is_delete = 0查询,发现查询成功。
-
最后发现问题:粗心大意,字段不能是MySQL关键字。
🚀四、解决方案
- 将表名
leave改成其他,如:ask_for_leave。 - 查看其他MySQL关键字,避免错误。

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

被折叠的 条评论
为什么被折叠?



