错误类型:reflection.ReflectionException: Could not set property ‘xxx‘ of ‘class ‘xxx‘ with value ‘xxx‘

这是我遇到的问题的全写:

Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'providerName' of 'class com.smbms.pojo.Bill' with value 'Provider(id=null, proCode=null, proName=深圳市泰香米业有限公司, proDesc=null, proContact=null, proPhone=null, proAddress=null, proFax=null, createdBy=null, creationDate=null, modifyBy=null, modifyDate=null)' Cause: java.lang.IllegalArgumentException: argument type mismatch

这种情况下他还是可以查出部分结果的,但是还是会报错,下面这张图是我出现错误的控制台显示

解决办法

这个问题困扰了我一个下午才得以解决,其实是一个 collection 复杂类型集合下的 resultMap 标签的 type 写错了,因为我在实体类中将我的 providerName 定义为 String 类型,但是我却在 Type 中写成 com.smbms.pojo.Provider,这边导致了我的程序一直出现这个问题,尽管调试了很多别的地方也没有效果。

这个是我之前错误的代码:

<resultMap id="billList" type="com.smbms.pojo.Bill">
        <result property="billCode" column="billCode"></result>
        <result property="productName" column="productName"></result>
        <result property="providerName" column="proName"></result>
        <result property="totalPrice" column="totalPrice"></result>
        <result property="isPayment" column="isPayment"></result>
        <result property="creationDate" column="creationDate"></result>
        <association property="providerName" javaType="com.smbms.pojo.Provider" resultMap="provider"></association>
    </resultMap>
    <resultMap id="provider" type="com.smbms.pojo.Provider">
        <result property="proName" column="proName"></result>
    </resultMap>

这个是我更改后的代码:

<resultMap id="billList" type="com.smbms.pojo.Bill">
        <result property="billCode" column="billCode"></result>
        <result property="productName" column="productName"></result>
        <result property="providerName" column="proName"></result>
        <result property="totalPrice" column="totalPrice"></result>
        <result property="isPayment" column="isPayment"></result>
        <result property="creationDate" column="creationDate"></result>
        <association property="providerName" javaType="com.smbms.pojo.Provider" resultMap="provider"></association>
    </resultMap>
    <resultMap id="provider" type="java.lang.String">
        <result property="proName" column="proName"></result>
    </resultMap>

大家可以看到,在下面的 resultMap 标签的 type 中改为 java.lang.String,运行就成功了。

下面这张图是运行成功之后的结果,大家可以看到此时查出来的结果是完整的

不过令我感到疑惑的是,我把下面这条代码中的  javaType="com.smbms.pojo.Provider" 改为  javaType= " java.lang.String" 仍然是没有错误的,希望懂的小伙伴可以留言在评论区。

<association property="providerName" javaType="com.smbms.pojo.Provider" resultMap="provider"></association>

补充知识点

collection

复杂类型集合,一对多

内部嵌套,映射一个嵌套结果集到一个列表

属性        property:映射数据库列的实体对象的属性

                ofType:完整Java类名或者别名(集合所包括的类型)

                resultMap:引用外部resultMap

子元素        id

                result        property:映射数据库列的实体对象的属性

                                column:数据库列名或者别名

猜你喜欢

转载自blog.csdn.net/lemonzjk/article/details/124529171
今日推荐