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_sql

所以,只需要把count和order字段换个名字就可以了

或者给对应的实体类加上mybatis-plus的注解@TableName,加个引号,这样也可以

@TableName( "`order`")

运行成功

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

(写博客主要是对自己学习的归纳整理,资料大部分来源于书籍、网络资料、官方文档和自己的实践,整理的不足和错误之处,请大家评论区批评指正。同时感谢广大博主和广大作者辛苦整理出来的资源和分享的知识。)