baomidou / mybatis-plus

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

当@TableId 和 @TableField同时标注同一个字段时,@TableField没有起作用 #6291

Closed vietor closed 3 weeks ago

vietor commented 3 weeks ago

当前使用版本 3.4.3.4

当前环境信息 例如: Java8 + Mysql8

描述bug现象 对于使用关键字作为字段名的情况,又同时指定为@TableId时,对于@TableField的定义会被忽略

提供问题复现步骤

创建表

CREATE TABLE `my_config` (
  `key` varchar(50) NOT NULL,
  `value` text NOT NULL,
  PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

定义对象

package tester;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("my_config")
public class ConfigPlusEntity {
    @TableId
    @TableField("`key`")
    private String key;
    private String value;
}

定义Mapper

package tester;

import org.apache.ibatis.annotations.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

@Mapper
public interface ConfigPlusMapper extends BaseMapper<ConfigPlusEntity> {
}

测试

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ApplicationTest {
    private static final String VALUE_KEY = "value";

    @Autowired
    private ConfigPlusMapper configPlusMapper;

    @Test
    @Order(1003)
    void tesConfigPlusValueAndDelete() {
        String payload = UUID.randomUUID().toString();

        ConfigPlusEntity entity = new ConfigPlusEntity();
        entity.setKey(VALUE_KEY);
        entity.setValue(payload);
        configPlusMapper.insert(entity);

        Assertions.assertEquals(payload, configPlusMapper.selectById(VALUE_KEY).getValue());
    }
}

输出

### 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 'key,
value )  VALUES  ( 'value',
'02771668-5485-4bc7-b75a-6702ed520b7e' )' at line 1
### The error may exist in tester/ConfigPlusMapper.java (best guess)
### The error may involve tester.ConfigPlusMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO via_config  ( key, value )  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 'key,
value )  VALUES  ( 'value',
'02771668-5485-4bc7-b75a-6702ed520b7e' )' 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 'key,
value )  VALUES  ( 'value',
'02771668-5485-4bc7-b75a-6702ed520b7e' )' at line 1
        at tester.ApplicationTest.tesConfigPlusValueAndDelete(ApplicationTest.java:92)
Caused by: 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 'key,
value )  VALUES  ( 'value',
'02771668-5485-4bc7-b75a-6702ed520b7e' )' at line 1
        at tester.ApplicationTest.tesConfigPlusValueAndDelete(ApplicationTest.java:92)
miemieYaho commented 3 weeks ago

不支持同时标注