码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis复杂对象查询

时间:2018-08-20 20:14:37      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:info   ice   type   from   com   sso   sele   highlight   mybatis   

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>

 

mybatis复杂对象查询

标签:info   ice   type   from   com   sso   sele   highlight   mybatis   

原文地址:https://www.cnblogs.com/magic101/p/9507689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!