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;

三、修复效果展示(略)

相关推荐
DokiDoki之父17 小时前
MyBatis—增删查改操作
java·spring boot·mybatis
DokiDoki之父1 天前
Mybatis—入门 & (配置)SQL提示和日志输出
数据库·sql·mybatis
计算机学姐3 天前
基于微信小程序的垃圾分类管理系统【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
哲此一生9843 天前
SpringBoot3集成Mybatis(开启第一个集成Mybatis的后端接口)
java·spring boot·mybatis
九转苍翎3 天前
Java外功精要(3)——Spring配置文件和mybatis
spring boot·mybatis
程序员三明治3 天前
【Mybatis从入门到入土】ResultMap映射、多表查询与缓存机制全解析
java·sql·缓存·mybatis·resultmap·缓存机制·多表查询
此剑之势丶愈斩愈烈3 天前
mybatis-plus分页插件使用
mybatis
!if4 天前
springboot mybatisplus 配置SQL日志,但是没有日志输出
spring boot·sql·mybatis
讓丄帝愛伱4 天前
Mybatis Log Free插件使用
java·开发语言·mybatis
gaoshan123456789104 天前
‌MyBatis-Plus 的 LambdaQueryWrapper 可以实现 OR 条件查询‌
java·tomcat·mybatis