使用mybatisplus查询sql时,报Error attempting to get column ‘ID‘ from result set错误

问题描述:

在使用如下代码进行查询时,报Error attempting to get column 'ID' from result set错误:

java 复制代码
 LambdaQueryWrapper<TimeFeature> wrapper = new LambdaQueryWrapper<>();
 wrapper.eq(TimeFeature::getDate, currentDateTime);
 TimeFeature timeFeature = timeFeatureMapper.selectOne(wrapper);

首先排除数据库字段大小写问题。

然后考虑ID字段在本轮逻辑中不是很重要,对代码做了如下更改,指定了查询字段:

java 复制代码
LambdaQueryWrapper<TimeFeature> wrapper = new LambdaQueryWrapper<>();
wrapper.select(TimeFeature::getDate,TimeFeature::getDayType5)
.eq(TimeFeature::getDate, currentDateTime);
TimeFeature timeFeature = timeFeatureMapper.selectOne(wrapper);

结果查询正常,然后能得出结果集到实体类的映射过程出现问题。

然后通过排查代码,发现我的实体类使用了@AllArgsConstructor注解,

java 复制代码
@Data
@TableName("TIME_FEATURE")
@ApiModel(value = "TimeFeature对象", description = "日期类型映射")
@AllArgsConstructor
public class TimeFeature extends BaseEntity{

    @ApiModelProperty("日期")
    private LocalDateTime date;

    @ApiModelProperty("类型")
    private Integer dayType5;

}

这个注解导致了指定的默认构造方法是只包含date,dayType5两个参数的方法。所以无法通过默认的构造方法将对应的结果集映射到实体类。

所以将这个注解去掉就解决问题了。

引申思考,那使用@Builder注解会不会导致类似的问题呢?

相关推荐
摇滚侠2 小时前
MyBatis 入门到项目实战 MyBatis 逆向工程 62
java·开发语言·mybatis
摇滚侠3 小时前
MyBatis 入门到项目实战 MyBatis 各种查询功能 30-33
java·后端·spring·maven·intellij-idea·mybatis
摇滚侠20 小时前
Mybatis 入门到项目实战 搭建 MyBatis 框架 01-14
java·tomcat·mybatis
敲个大西瓜20 小时前
mybatis插件原理与编写
mybatis
可乐ea1 天前
【Spring Boot + MyBatis|第7篇】JWT 登录认证与拦截器实现
java·spring boot·后端·mybatis·状态模式
摇滚侠1 天前
MyBatis 入门到项目实战 MyBatis 核心配置文件 15-19
java·tomcat·mybatis
摇滚侠1 天前
MyBatis 入门到项目实战 IDEA 配置模板 20-22
java·intellij-idea·mybatis
独泪了无痕1 天前
MyBatis魔法堂:结果集映射
后端·mybatis
就叫_这个吧2 天前
IDEA Mybatis xml文件,实现sql语句联想,自动填入补充
xml·mysql·intellij-idea·mybatis
熠熠仔2 天前
Spring Boot 与 MyBatis-Plus 空间几何数据集成指南
spring boot·后端·mybatis