baomidou / mybatis-plus

An powerful enhanced toolkit of MyBatis for simplify development
https://baomidou.com
Apache License 2.0
16.42k stars 4.31k forks source link

使用mybatis-plus对含有保留字段的表插入数据会报错 #792

Closed wangyaochong closed 5 years ago

wangyaochong commented 5 years ago

当前使用版本(必须填写清楚,否则不予处理)

3.0.5

该问题是怎么引起的?(最新版上已修复的会直接close掉)

插入数据的字段包含保留字段,比如有一个实体类,其中有一个字段名为change

重现步骤

在mysql建立一张具有保留字段(比如change)的表,使用mybatis-plus尝试给该表插入数据时报错

报错信息

Caused by: org.apache.ibatis.executor.BatchExecutorException: com.wangyaochong.tfp.tushare.generated.mapper.TushareDailyMapper.insert (batch index #1) failed. Cause: java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'change

miemieYaho commented 5 years ago

自行注解里用转义符包裹

wangyaochong commented 5 years ago

很感谢你的回复! 但我还是不太明白你说的解决办法,我看了之前有人提过查询时含有关键字的报错的问题,https://github.com/baomidou/mybatis-plus/issues/775 解决方案是在查询条件中包裹字段, 但是对于插入操作,不涉及到注解,只是简单调用一下AbcService.save()方法,我不明白你说的注解里用转义符包裹是什么意思,希望你能进一步说明,非常感谢。

miemieYaho commented 5 years ago

就是那个意思

wangyaochong commented 5 years ago

但我是插入操作啊,没有用到Wrapper

miemieYaho commented 5 years ago

https://github.com/baomidou/mybatis-plus/blob/3.0/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/base/entity/mysql/MysqlData.java

wangyaochong commented 5 years ago

明白了,你的回答对我非常有帮助,非常感谢!!!

hipopular commented 5 years ago

只能这样手动解决吗?自动生产的时候能不能配置识别保留字段?

applenele commented 5 years ago

为什么不在生成的sql语句里面所有的字段都加上``? 或者想楼上的说的识别保留字段.

lilizing commented 5 years ago

这样做即可:

主键用@TableId,并将字段名用``包裹,例如:

@TableId(value = "`key`")
String key;

非主键用@TableField,并将字段名用``包裹,例如:

@TableField(value = "`value`")
String value;
hyhy01 commented 3 years ago

这样做即可:

主键用@TableId,并将字段名用``包裹,例如:

@TableId(value = "`key`")
String key;

非主键用@TableField,并将字段名用``包裹,例如:

@TableField(value = "`value`")
String value;

不行,会生成

`yt` AS use

只有as前面有引号,as后面没引号

allenhsu commented 11 months ago

这样做即可:

主键用@TableId,并将字段名用``包裹,例如:

@TableId(value = "`key`")
String key;

非主键用@TableField,并将字段名用``包裹,例如:

@TableField(value = "`value`")
String value;

Just my two cents. IMO, It's a workaround but not a solution. TableField should clearly define the exact field name. The backquote is not part of the field name, it's part of MySQL syntax. So the quoting / escaping is better happening on SQL building or map generating stage, rather than field definition stage.

YorkHwang commented 4 months ago

这个问题在哪个版本修复了? 准备升级