MyBatis 3.0 中多表连接 一对多集合查询

lanchunchun 2010-11-08 05:30:04
现在是这样的情况,有一个学生类student成员(String id,String name,List<Goal> goals) 对应student(id ,name )表 ,一个Goal(int id,int student, int goal ,int course )类,对应goal表(id ,course_id,student_id,goal)
现在我想根据学好查询出这个学生所有的选课情况,包括(学号,姓名,课程号,课程成绩goal)mapper是这样写的

<resultMap type="Student" id="getStudentGoalAll">
<id property="id" column="id" />
<result property="name" column="name" />
<collection property="goals" javaType="ArrayList" ofType="Goal" select="getGoalsBystudentId" column="id">
</collection>
</resultMap>

<resultMap type="Goal" id="getGoal">
<id property="id" column="id"/>
<result property="course" column="course_id"/>
<result property="goal" column="goal"/>
<result property="student" column="student_id"/>
</resultMap>


<select id="selectStudentWithGoal" parameterType="int" resultMap="getStudentGoalAll">
select s.id ,
s.name ,
g.id ,
g.student_id ,
g.course_id ,
g.goal
from student s
left outer join goal g on s.id=g.student_id
where s.id=#{id}
</select>


<select id="getGoalsBystudentId" parameterType="int" resultMap="getGoal" >
select *
from goal where student_id = #{id}
</select>


测试的时候用的一下代码:
Integer pk = new Integer (1);

List<Student> s = session.selectList("selectStudentWithGoal", pk);
System.out.println(s.size ());

for(int i= 0; i< s.size (); i++)
{
System.out.println (s.get (0).getGoals ().get (i).getGoal ());
System.out.println (s.get (0).getGoals ().get (i).getCourse ());
}
打印出来的结果却只有一半一半的

4
80
null
90
null
100
null
90
null

按理说应该把课程号也打印出来的,但是去没有,我就有点弄不明白了,请各位帮忙看看
...全文
939 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧