天天看點

【BUG記錄】mybatis-plus3 You have an error in your SQL syntax; check the manual that corresponds to your

BUG描述

當我使用mybatis-plus3執行CRUD的時候,出現如下錯誤

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18833453] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@188250896 wrapping com.mysql.cj.jdbc.ConnectionImpl@6bf35e7b] will not be managed by Spring
==>  Preparing: INSERT INTO order ( user_id, product_id, count, money ) VALUES ( ?, ?, ?, ? )
==> Parameters: 2(Integer), 2(Integer), 4(Integer), 7(Integer)
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18833453]
2022-02-17 23:04:59.343 ERROR 9464 --- [nio-9001-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [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 'order  ( user_id,
product_id,
count,
money )  VALUES  ( 2,
2,
4,
7 )' at line 1
### The error may exist in com/xt/springcloud/mapper/OrderMapper.java (best guess)
### The error may involve com.xt.springcloud.mapper.OrderMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO order  ( user_id, product_id, count, money )  VALUES  ( ?, ?, ?, ? )
### 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 'order  ( user_id,
product_id,
count,
money )  VALUES  ( 2,
2,
4,
7 )' 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 'order  ( user_id,
product_id,
count,
money )  VALUES  ( 2,
2,
4,
7 )' at line 1] with root 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 'order  ( user_id,
product_id,
count,
money )  VALUES  ( 2,
2,
4,
7 )' at line 1      

貌似是文法錯誤

于是我将日志裡面的sql 語句放到datagrip中執行

INSERT INTO order ( user_id, product_id, count, money ) VALUES ( 1, 2, 3, 4 )      

可以看見執行出錯,有文法錯誤,可見下圖的兩個詞都是mysql的關鍵詞,是以引起了錯誤

【BUG記錄】mybatis-plus3 You have an error in your SQL syntax; check the manual that corresponds to your

是以,隻需要把count和order字段換個名字就可以了

或者給對應的實體類加上mybatis-plus的注解@TableName,加個引号,這樣也可以

@TableName( "`order`")      

運作成功