MyBatis 3.0 中多表连接 一对多集合查询
现在是这样的情况,有一个学生类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
按理说应该把课程号也打印出来的,但是去没有,我就有点弄不明白了,请各位帮忙看看