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;

三、修复效果展示(略)

相关推荐
java1234_小锋5 小时前
简述Mybatis的插件运行原理?
java·开发语言·mybatis
z***677714 小时前
SpringBoot(整合MyBatis + MyBatis-Plus + MyBatisX插件使用)
spring boot·tomcat·mybatis
b***676416 小时前
Springboot3 Mybatis-plus 3.5.9
数据库·oracle·mybatis
k***1951 天前
Spring Boot中集成MyBatis操作数据库详细教程
数据库·spring boot·mybatis
e***74951 天前
SpringCloud 系列教程:微服务的未来(二)Mybatis-Plus的条件构造器、自定义SQL、Service接口基本用法
spring cloud·微服务·mybatis
chxii1 天前
在 Spring Boot 中,MyBatis 的“自动提交”行为解析
java·数据库·mybatis
有一个好名字2 天前
MyBatis-Plus 三种数据库操作方式详解 + 常用方法大全
数据库·mybatis
颜如玉2 天前
动态拼接SQL实践备忘📝
java·sql·mybatis
朝新_2 天前
【实战】博客系统:项目公共模块 + 博客列表的实现
数据库·笔记·sql·mybatis·交互·javaee
小小哭包2 天前
Spring Boot整合多个MyBatis数据源实战教程
spring boot·后端·mybatis