mybatis-plus insert时报错 表名order

博客作者在尝试插入数据到数据库时遇到了一个问题,插入操作虽然没有语法错误,但无法成功执行。经过排查,发现问题是由于使用了MySQL的关键字'order'作为表名,导致SQL语句执行时出现语法错误。解决方案是将表名改为'pro_order',从而避开了关键字冲突,问题得到解决。这个案例提醒我们在数据库设计时需要注意避免使用保留字作为表名或字段名。

今天遇到一个问题就是insert的时候语法没问题 插入日志也没问题 但是就是插不进去(忙了一上午啊 我的时间啊 可恶)

报错如图:

SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3180131e] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1409112532 wrapping com.mysql.cj.jdbc.ConnectionImpl@43c3354] will not be managed by Spring
==>  Preparing: INSERT INTO order ( pk_id, order_price, product_id ) VALUES ( ?, ?, ? )
==> Parameters: 1(String), 50(Integer), 1501002853813071874(String)
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3180131e]

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  ( pk_id,
order_price,
product_id )  VALUES  ( '1',
50,
'15010028538130718' at line 1
### The error may exist in com/zhang/mapper/OrderMapper.java (best guess)
### The error may involve com.zhang.mapper.OrderMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO order  ( pk_id, order_price, product_id )  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  ( pk_id,
order_price,
product_id )  VALUES  ( '1',
50,
'15010028538130718' 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  ( pk_id,
order_price,
product_id )  VALUES  ( '1',
50,
'15010028538130718' at line 1

然后关注点放在

Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to

左想想 右想想 

我把表名改了 order->pro_order他就没问题了 可恶啊(order为mysql的关键字 拼接sql语句会出现问题)

MyBatis-Plus中,有两种常见的方法可以使insert方法返回自增主键。 第一种方法是使用`selectKey`标签进行配置。示例代码如下: ```xml <insert id="insert"> insert into ums_admin(username, password, icon, email, nick_name, note, create_time, login_time) values (#{username}, #{password}, #{icon}, #{email}, #{nickName}, #{note}, #{createTime}, #{loginTime}) <selectKey keyColumn="id" resultType="long" keyProperty="id" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> </insert> ``` 该方法通过`selectKey`标签在插入操作后获取自增的ID,并将其赋值给实体类的对应属性[^2]。 第二种方法是设置`useGeneratedKeys`和`keyProperty`属性。示例代码如下: ```xml <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="TbUser"> INSERT INTO tb_user SET <if test="name!=null"> name = #{name}, </if> <if test="sex!=null"> sex = #{sex}, </if> <if test="tel!=null"> tel = #{tel}, </if> <if test="email!=null"> email=#{email}, </if> create_time = #{createTime} </insert> ``` 其中,`useGeneratedKeys`设置为`true`示开启主键自增,`keyProperty`指定数据库主键对应的实体类属性,将自增的主键赋值给传入的实体类参数。插入并获取自增主键值的代码如下: ```java //插入 userDao.insert(tbUser); //tbUser.getId()获取到自增主键值 System.out.println(tbUser.getId()); ``` 使用这种方法,在执行插入操作后,可直接通过实体类对象的对应属性获取自增主键值[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新生代农民工-小王八

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值