项目场景:
Springboot博客项目,从数据库中获取帖子的标题等信息最终显示在网页上
问题描述
报错:org.springframework.dao.TransientDataAccessResourceException: Error attempting to get column 'title' from result set. Cause: java.sql.SQLException: Invalid value for getInt() - '标题4'
1、从数据库获取帖子的标题内容时,发现数据库中t_invitation的column与实体定义字段的类型不匹配。检查表和实体没问题之后,继续排查
2、检查sql语句,帖子表和帖子详情表(t_invitation_data)做了联合查询,排查帖子详情表的实体
<select id="selectIndexData" resultMap="indexDataResultMap">
select t1.*,u.*,t2.*
,u.id as uid
,t1.id as tiid
,t2.id as tdid
from t_invitation as t1
left join t_user as u on t1.uid=u.id
left join t_invitation_data as t2 on t1.id=t2.tid
order by t1.createTime DESC
</select>
@AllArgsConstructor
@Data
@TableName("t_invitation_data")
public class InvitationDataEntity {
@TableId(type = IdType.AUTO)
private Integer id; // 主键id
private Integer tid; // 帖子id
private Long pv; // 浏览量
private Long likes; // 点赞量
private Long comments; // 评论量
private Long collect; // 收藏量
}
发现问题出在@AllArgConstructor这个注解上,删除之后,重启项目解决
原因分析:
当springboot项目中使用了lombok,且使用了@AllArgsConstructor注解进行pojo类的全参数构造,如果你的mybatis中的结果返回采用的是resultType,则必须保证查询出来的字段顺序和pojo类的参数顺序保持一致,否则会出现参数错位,或者出现类型转换异常(比如第一个查出来的是字符串,pojo类中第一个参数时Date类型的时候就会出现类型转换异常)