1、一对多查询
<resultMap type="com.cdxt.ds.web.lesson.pojo.CourseInfo" id="courseDetailInfo" >
<id column="courseID" property="courseID"/>
<result column="price" property="price"/>
<result column="name" property="name"/>
<result column="totalclass" property="totalClass"/>
<result column="type" property="type"/>
<result column="brief" property="brief"/>
<collection property="coursePlan" ofType="com.cdxt.ds.web.lesson.pojo.CoursePlan">
<id column="courseID" property="id"/>
<result column="classnumber" property="classNumber"/>
<result column="time" property="time"/>
</collection>
</resultMap>
<!-- 查询课程信息单一记录 -->
<select id="getCourseInfobyCpurseID" parameterType="int" resultMap="courseDetailInfo">
select a.courseID,a.price,a.name,a.totalclass,a.type,a.brief,b.id,
b.classnumber,b.time from XTEL_COURSEINFO a left join XTEL_CourseArrangement b
on a.courseID=b.courseID where a.COURSEID=#{cpurseID}
</select>
问题:测试的时候发现只能从表只能查询到一条记录。
原因:两张表主键一样,出现的数据覆盖
解决方法:
1、修改数据库id,不推荐
2、查询结果起别名
<resultMap type="com.cdxt.ds.web.lesson.pojo.CourseInfo" id="courseDetailInfo" > <id column="courseID" property="courseID"/> <result column="price" property="price"/> <result column="name" property="name"/> <result column="totalclass" property="totalClass"/> <result column="type" property="type"/> <result column="brief" property="brief"/> <collection property="coursePlan" ofType="com.cdxt.ds.web.lesson.pojo.CoursePlan"> <id column="courseID" property="id"/> <result column="classnumber" property="classNumber"/> <result column="time" property="time"/> </collection> </resultMap> <!-- 查询课程信息单一记录 --> <select id="getCourseInfobyCpurseID" parameterType="int" resultMap="courseDetailInfo"> select a.courseID,a.price,a.name,a.totalclass,a.type,a.brief,b.courseID id, b.classnumber,b.time from XTEL_COURSEINFO a left join XTEL_CourseArrangement b on a.courseID=b.courseID where a.COURSEID=#{cpurseID} </select>