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
按理说应该把课程号也打印出来的,但是去没有,我就有点弄不明白了,请各位帮忙看看
现在我想根据学好查询出这个学生所有的选课情况,包括(学号,姓名,课程号,课程成绩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
按理说应该把课程号也打印出来的,但是去没有,我就有点弄不明白了,请各位帮忙看看
...全文
相关推荐
结合mybatis-plus实现简单不需要写sql的多表查询
主要给大家介绍了关于结合mybatis-plus实现简单不需要写sql的多表查询的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用mybatis-plus具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
MyBatis 多表关联查询
MyBatis 多表关联查询:一对多查询, 多对一查询, 自连接查询: 使用多对一的方式实现自连接, 使用一对多的方式实现自连接, 多对多查询
Mybatis一对多关联关系映射实现过程解析
主要介绍了Mybatis一对多关联关系映射实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
MyBatis 三表外关联查询的实现(用户、角色、权限)
主要介绍了MyBatis 三表外关联查询的实现(用户、角色、权限),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Java工程mybatis实现多表查询过程详解
主要介绍了Java工程mybatis实现多表查询过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下


