MyBatis 查询的条目与预期的不一致

预期查询的数据条目是 4 条:

image

但是 MyBatis 查询出来的结果只有 2 条数据:

image

resultMap 开启了 autoMapping 功能,不需要 result 子标签。下面是错误的映射操作:

file:[错误的 mapper]
<!-- resultMap 开启了 autoMapping,结果集映射 result 标签不需要写 -->
lit:[<resultMap id="mapOfQueryMyCourses" type="UniCourse" autoMapping="true">
  <result column="term" property="term"/>]:lit
  <association property="course" javaType="Course" columnPrefix="c_" autoMapping="true"/>
  <association property="teacher" javaType="Teacher" columnPrefix="t_" autoMapping="true"/>
</resultMap>
<select id="queryMyCourses" resultMap="mapOfQueryMyCourses" parameterType="Student">
  SELECT sc.term,
         sc.stu_id,
         c.name     c_name,
         c.property c_property,
         c.credit   c_credit,
         t.name     t_name
  FROM `stu_course` AS sc
         JOIN `courses` AS c ON sc.course_id = c.cno
         JOIN `teachers` AS t ON sc.teacher_id = t.tno
  WHERE sc.stu_id = #{sno}
</select>

删除 result 子标签。autoMapping 功能开启,不需要 result 子标签,两者不能同时存在:

file:[正确的 mapper]
<resultMap id="mapOfQueryMyCourses" type="UniCourse" autoMapping="true">
  <association property="course" javaType="Course" columnPrefix="c_" autoMapping="true"/>
  <association property="teacher" javaType="Teacher" columnPrefix="t_" autoMapping="true"/>
</resultMap>

image

posted @ 2022-12-18 23:17  Himmelbleu  阅读(13)  评论(0编辑  收藏  举报