解决MySQL 中使用index desc 作为字段名时Mybatis-Plus操作出现的问题

解决方法
  •    再有关键字实体类属性上添加 @TableField("`表的列名`") 重点是  “`”这个符号
    
  •   # 不加的语句
      INSERT INTO test  ( index, desc )  VALUES  ( ?, ? );
      # 加的语句
      INSERT INTO test ( `index`, `desc` ) VALUES ( ?, ? ) 
    
结果
  1. 测试类

    @Autowired
    TestMapper mapper;
    @Test
    void test(){
        com.book.yomance.entity.Test test = new com.book.yomance.entity.Test();
        test.setDesc("描述")
            .setIndex(1);
        mapper.insert(test);
        System.out.println(mapper.selectById(test.getId()));
    }
    
  2. 实体类

    public class Test {
        @TableField("`index`")
        private Integer index;
        @TableField("`desc`")
        private String desc;
        @TableId(type = IdType.AUTO)
        private Integer id;
        // 省略get set
    }
    
  3. 结果

    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@51f95f0d] was not registered for synchronization because synchronization is not active
    2021-02-27 16:53:36.436  INFO 21164 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
    2021-02-27 16:53:36.663  INFO 21164 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
    JDBC Connection [HikariProxyConnection@1576526619 wrapping com.mysql.cj.jdbc.ConnectionImpl@63a9c661] will not be managed by Spring
    ==>  Preparing: INSERT INTO test ( `index`, `desc` ) VALUES ( ?, ? ) 
    ==> Parameters: 1(Integer), 描述(String)
    <==    Updates: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@51f95f0d]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@149c39b] was not registered for synchronization because synchronization is not active
    JDBC Connection [HikariProxyConnection@816760435 wrapping com.mysql.cj.jdbc.ConnectionImpl@63a9c661] will not be managed by Spring
    ==>  Preparing: SELECT id,`index`,`desc` FROM test WHERE id=? 
    ==> Parameters: 1(Integer)
    <==    Columns: id, index, desc
    <==        Row: 1, 1, 描述
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@149c39b]
    Test{index=1, desc='描述', id=1}
    
    image-20210227170032724
报错情况
```sql
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 'index,
desc )  VALUES  ( 1,
'描述' )' at line 1
### The error may exist in com/book/yomance/mapper/TestMapper.java (best guess)
### The error may involve com.book.yomance.mapper.TestMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO test  ( index, desc )  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 'index,
desc )  VALUES  ( 1,
'描述' )' 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 'index,
desc )  VALUES  ( 1,
'描述' )' at line 1
```
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值