记一次MyBatisPlus问题(如果表名是数据库关键字怎么办)
问题信息:
如果表名是数据库关键字怎么办?
正常来说,如果是我们自己写sql的话,给表名加反引号即可解决问题。
但是由于我们使用MyBatisPlus,相关的sql基本上都是封装并自动生成的。如果是这种场景,我们就需要修改对应的实体,举例说明,如下代码:
import com.baomidou.mybatisplus.enums.IdType; import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.activerecord.Model; import java.io.Serializable; public class Group extends Model<Group> { private static final long serialVersionUID = 1L; /** * 组ID */ @TableId(value = "id", type = IdType.AUTO) private Integer id; /** * 组名 */ private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override protected Serializable pkVal() { return this.id; } @Override public String toString() { return "Group{" + "id=" + id + ", name=" + name + "}"; } }
用上述代码的自动生成肯定会有问题,以单条数据查询为例,默认是 select id,name from group where id = 1,又因为group属于关键字,接下来会出现如下错误信息:
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'group WHERE id=1' at line 1 ; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'group WHERE id=1' at line 1] with root cause com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'group WHERE id=1' at line 1
这种错误信息,很容易识别,一看就是sql写的有问题。但实际上sql并没有问题,只不过是因为关键字冲突导致sql错误。
那么如何解决这个问题呢?
答案是只需加一个@TableName注解即可解决该问题。修改后的实体代码如下:
import com.baomidou.mybatisplus.enums.IdType; import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.annotations.TableName; import com.baomidou.mybatisplus.activerecord.Model; import java.io.Serializable; @TableName("`group`") public class Group extends Model<Group> { private static final long serialVersionUID = 1L; /** * 组ID */ @TableId(value = "id", type = IdType.AUTO) private Integer id; /** * 组名 */ private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override protected Serializable pkVal() { return this.id; } @Override public String toString() { return "Group{" + "id=" + id + ", name=" + name + "}"; } }
分类:
MP实战系列
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
2018-08-31 开源技术推荐之个人使用心得