mybatis一对多 关系映射问题 argument type mismatch

本文详细解析了MyBatis框架中一对多关系的映射问题,包括错误的XML配置示例及解决方案,重点阐述了无参构造器的重要性,以及如何正确配置resultMap以实现对象集合的映射。

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

 

 

mybatis一对多 关系映射问题

2020-05-10 09:12:53.517 ERROR 40428 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.wang.test0509.pojo.User with invalid types (int,String,String,List) or values (1,root,c4ca4238a0b923820dcc509a6f75849b,1). Cause: java.lang.IllegalArgumentException: argument type mismatch] with root cause
 

错误版本

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wang.test0509.mapper.UserMapper">
    <select id="queryUserById"  parameterType="int" resultMap="resultMap" >
        select u.userId as userId,u.userName as userName,u.password as password,
            ub.ubid as ubId ,ub.bookName as booksName from user u ,userbooks ub
        where u.userId = #{id}
          and u.userId = ub.userId
    </select>

    <resultMap id="resultMap" type="user">
        <result column="userId" property="userId"></result>
        <result column="userName" property="userName"></result>
        <result column="password" property="password"></result>
        
        <collection property="booksList" javaType="list" ofType="UserBooks">
            <result property="userId" column="userId"></result>
            <result property="ubId" column="ubId"></result>
            <result property="booksName" column="booksName"></result>
        </collection>
    </resultMap>


</mapper>

报错原因是User对象没有定义无参构造器。修改之后报了如下的错误,collection里面的对象也没有无参构造。加上之后能够查询出来结果。

2020-05-10 09:22:30.087 ERROR 27116 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: Error attempting to get column 'userName' from result set.  Cause: java.sql.SQLDataException: Cannot determine value type from string 'root'
; Cannot determine value type from string 'root'; nested exception is java.sql.SQLDataException: Cannot determine value type from string 'root'] with root cause

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值