记一次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 +
        "}";
    }
}
复制代码

 

posted @   挑战者V  阅读(8167)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 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 开源技术推荐之个人使用心得
点击右上角即可分享
微信分享提示