java.sql.SQLException: Fail to convert to internal representation

本文探讨了MyBatis框架中构造函数参数顺序与结果集不匹配导致的错误。通过调整Java类构造函数参数顺序使其与MyBatis配置文件中的resultMap元素保持一致,解决了异常问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

message: "Error attempting to get column 'PARAM_NAME' from result set. 
Cause: java.sql.SQLException: Fail to convert to internal representation↵; 
uncategorized SQLException; SQL state [99999]; error code [17059];
 Fail to convert to internal representation; 
nested exception is java.sql.SQLException: 
Fail to convert to internal representation"

 

Java

package com.app.dto;



public class DiversityCustomAttrDTO {

    private Long numberValue;

    private String paramName;



    public DiversityCustomAttrDTO(Long numberValue, String paramName) {

       this.numberValue = numberValue;

       this.paramName = paramName;

    }



// getters setters

}

 

MyBatis

<resultMap id="customAttrResultMap" type="com.app.dto.DiversityCustomAttrDTO">

    <result property="paramName" column="PARAM_NAME" jdbcType="VARCHAR"/>

    <result property="numberValue" column="NUMBER_VALUE" jdbcType="NUMERIC" />

</resultMap>

 

怎么看都没问题,删掉代码一行行加回来,原来是IDEA自动生成构造函数的参数顺序和resultset里result顺序不匹配时就会报错。

删掉构造函数 - 工作

加上默认无参的构造函数 - 工作

调整参数顺序和resultset里一样 - 工作

 

那么基本可以推测,mybatis ibatis 源码里当遇到有构造函数的时候,而且有没有无参的构造函数,参数顺序就必须和resultset里是一致的。

推测底层是通过反射来实例化的,看下反射类中的Constructor,只能拿到

private Class<?>[] parameterTypes;

所以当我们的构造函数第一个是long,而result里是varchar,也就是String时,就报错了。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值