Spring Boot报错 org.springframework.jdbc.BadSqlGrammarException:

2年前 (2022) 程序员胖胖胖虎阿
275 0 0

org.springframework.jdbc.BadSqlGrammarException

  • 偶然间发现了这个问题,感觉挺简单吧,但是也不简单,感觉不简单吧,其实没有那么
  • 接下来就描述这个问题,今天在做查询表述一条数据的时候出现这个问题
  • 错误如下:
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 'desc,status FROM n_note WHERE id=1' at line 1
### The error may exist in com/an/notepad/mapper/NoteMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT id,created,title,user_id,content,desc,status FROM n_note 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 'desc,status FROM n_note WHERE id=1' at line 1
; 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 'desc,status FROM n_note WHERE id=1' at line 1
	at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:235) ~[spring-jdbc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:74) ~[mybatis-spring-2.0.2.jar:2.0.2]

其实下面东西挺多的,但是与这个错误没有太大的关系
我一开始觉得挺奇怪的,因为查询所有数据的时候可以,但是根据id查询具体一条数据的时候就出现这个错误提示
Spring Boot报错 org.springframework.jdbc.BadSqlGrammarException:
Spring Boot报错 org.springframework.jdbc.BadSqlGrammarException:
然后我就去查了一下这个异常,真是老脸通红哇,这个异常属于低级SQL异常 BadSqlGrammarException 哈哈,然后我就去看我的语句去了,然而更邪门的出现了。我的项目是集成Mybatis Plus,我压根就没写SQL语句,这…

  • 最后在我仔细分析了一下报错的语句,我发现了一个问题
    Spring Boot报错 org.springframework.jdbc.BadSqlGrammarException:
    我的表字段是这样的
    里面的文章描述我为了省事 使用了desc但是这正好是SQL语句中的一个关键字,这真是,一个让人拍脑门的错误,所以字段名一定不能和SQL关键字冲突。

解决方案

直接修改字段名和代码生成器生成的实体类中的变量名
Spring Boot报错 org.springframework.jdbc.BadSqlGrammarException:
Spring Boot报错 org.springframework.jdbc.BadSqlGrammarException:

总结

  • 有时候在命名的时候一定的注意,不能和SQL中的关键字冲突,java中也会是,我真的是被自己的偷懒上了一课哇,如果大家有这个问题,可以去看看是自己SQL有问题,还是字段名和关键字冲突了,都不是的话认真分析错误日志。

相关文章

暂无评论

暂无评论...