Mybatis Plus出现SQLSyntaxErrorException: Unknown column问题解决方法

时间:2025-04-06 15:36:21

出现此问题有两种情况:

第一种情况:实体类Model的注解类中增加了非数据库使用字段;

解决方法:

排除非表字段 noColumn 属性用法,例如:

1、使用 transient 修饰

private transient String noColumn;

2、使用 static 修饰

private static String noColumn;

3、使用 TableField 注解

@TableField(exist=false)
private String noColumn;

第二种情况:数据库中列名命名不规范,按照规范,数据库User表里边对应userId的字段名应该为 user_id。
如果数据库的字段名也是userId的话(没有下划线),那么使用MybatisPlus的时候就会碰到映射问题,实际查询的时候默认是查询user_id。

解决方法:

1.按照规范命名,对未规范的命名全部修改

添加一行配置,关闭驼峰到下划线的映射即可

-underscore-to-camel-case=false

这个方法我试过,没成功

-plus的setDbColumnUnderline设置使得数据库字段强行按照java实体的骆驼式命名法大写字母前转化为下划线加小写的命名规范。

设置不用其下划线命名法,在中设置

MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
(true);