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