mybatis mapper.xml 写关联查询 运用 resultmap 结果集中 用 association 关联其他表 并且 用 association 的 select 查询值 报错 java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for xxx
在学习用mybaits 的association写一个关联查询时,报错了Mapped Statements collection does not contain value for mapper.ClassMapper.getTeacher,查了网上的资源,发现没有能解决问题的。
我就只能去接一杯水,再检查一边代码,一个小时过去了,双眼发黑还是没能找到错,然后我又把报错一字不漏地看了一遍,咦,mapper.ClassMapper.getTeacher是什么,我的ClassMapper映射文件里没有getTeacher,它原本是在TeacherMapper里呀,这是怎么回事???
ClassMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="mapper.ClassMapper">
<resultMap type="ClassEntity" id="classResultMap">
<id column="classid" property="classid"/>
<result column="classname" property="classname"/>
<association property="teacherEntity" column="teacherid" select="getTeacher"/>
</resultMap>
<select id="getClassByID" parameterType="int" resultMap="classResultMap">
select * from class c
where c.classid=#{classid}
</select>
</mapper>
TeacherMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="mapper.TeacherMapper">
<resultMap type="TeacherEntity" id="teacherResultMap">
<id column="teacherid" property="teacherid"/>
<result column="teachername" property="teachername"/>
<result column="teachersex" property="teachersex"/>
</resultMap>
<select id="getTeacher" parameterType="int" resultMap="teacherResultMap">
select * from teacher t
where t.teacherid=#{teacherid}
</select>
</mapper>
是不是配置文件里没加mapper呢?
<mappers>
<mapper resource="mapper/ClassMapper.xml"/>
<mapper resource="mapper/TeacherMapper.xml"/>
</mappers>
加了的呀,但是getTeacher确实定义在TeacherMapper.xml里的,于是我把ClassMapper里的
(以下为原错误代码)
<association property="teacherEntity" column="teacherid" select="getTeacher"/>
修改为
<association property="teacherEntity" column="teacherid" select="mapper.TeacherMapper.getTeacher"/>
junit,大功告成~~~