mybatis-plus 表字段存在关键字的处理办法

一、问题复现

当数据库表中字段出现关键字时,若不做其他处理,mybatis-plus的BaseMapper并不会做其他的处理,最终导致SQL执行时,抛出SQL 语法错误。示例如下:

  • Java查询语句
java 复制代码
 QueryWrapper<User> userQuery = new QueryWrapper<>();
 userQuery.lambda().isNull(User::getDeletedAt)
         .like(User::getCnName, keyword);
 List<User> users = userMapper.selectList(userQuery);
  • 异常信息
java 复制代码
Error querying 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,remarks

二、处理办法

应用mybatis-plus提供的注解--TableField,就可以很方便的解决此问题。示例如下:

注意:需要在value值里,特别的加上字段''号

java 复制代码
    @TableField(value = "`key`")
    private String key;

三、修复效果展示(略)

相关推荐
勿在浮沙筑高台2 小时前
无法获取实体类com.example.springdemo2.entity.po.UserPO对应的表名!
java·spring boot·mybatis
柯南二号1 天前
【Java后端】MyBatis-Plus 原理解析
java·开发语言·mybatis
Easocen1 天前
Mybatis学习笔记(五)
笔记·学习·mybatis
qq_三哥啊1 天前
【IDEA】设置Debug调试时调试器不进入特定类(Spring框架、Mybatis框架)
spring·intellij-idea·mybatis
柯南二号1 天前
【Java后端】Spring Boot 集成 MyBatis-Plus 全攻略
java·spring boot·mybatis
记忆不曾留2 天前
Mybatis 源码解读-SqlSession 会话源码和Executor SQL操作执行器源码
mybatis·二级缓存·sqlsession会话·executor执行器·一级缓存localcache
昵称为空C3 天前
SpringBoot 实现DataSource接口实现多租户数据源切换方案
后端·mybatis
isyangli_blog3 天前
(2-10-1)MyBatis的基础与基本使用
java·开发语言·mybatis
_码农121383 天前
Mybatis简单练习注解sql和配置文件sql+注解形式加载+配置文件加载
mybatis
期待のcode3 天前
Maven
java·spring·maven·mybatis