使用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注解会不会导致类似的问题呢?

相关推荐
qq_三哥啊39 分钟前
【IDEA】设置Debug调试时调试器不进入特定类(Spring框架、Mybatis框架)
spring·intellij-idea·mybatis
柯南二号3 小时前
【Java后端】Spring Boot 集成 MyBatis-Plus 全攻略
java·spring boot·mybatis
记忆不曾留10 小时前
Mybatis 源码解读-SqlSession 会话源码和Executor SQL操作执行器源码
mybatis·二级缓存·sqlsession会话·executor执行器·一级缓存localcache
昵称为空C2 天前
SpringBoot 实现DataSource接口实现多租户数据源切换方案
后端·mybatis
isyangli_blog2 天前
(2-10-1)MyBatis的基础与基本使用
java·开发语言·mybatis
_码农121382 天前
Mybatis简单练习注解sql和配置文件sql+注解形式加载+配置文件加载
mybatis
期待のcode2 天前
Maven
java·spring·maven·mybatis
独泪了无痕2 天前
一文搞懂MyBatis中的TypeHandler
数据库·后端·mybatis
Easocen3 天前
Mybatis学习笔记(十)
mybatis
千睢3 天前
Mybatis实现页面增删改查
mybatis