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

问题描述:
在使用如下代码进行查询时,报Error attempting to get column ‘ID’ from result set错误:

 LambdaQueryWrapper<TimeFeature> wrapper = new LambdaQueryWrapper<>();
 wrapper.eq(TimeFeature::getDate, currentDateTime);
 TimeFeature timeFeature = timeFeatureMapper.selectOne(wrapper);

首先排除数据库字段大小写问题。
然后考虑ID字段在本轮逻辑中不是很重要,对代码做了如下更改,指定了查询字段:

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

结果查询正常,然后能得出结果集到实体类的映射过程出现问题。
然后通过排查代码,发现我的实体类使用了@AllArgsConstructor注解,

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

你可能感兴趣的:(错误经验,mybatis)