1.如果查询多的一端带出少的一端,那么仍然使用<association>参考第八点
如果低的一端带出多的一端,那么使用<colletion>标签
property对应成员变量,
colum对应关联字段,
select对应从表的关联查询语句的id,
Javatype 为List
Oftpe 为list的泛型
2.首先有两个表,一个是学生表,还有一个是系别表
3.若是要根据学号,查学生信息和系别信息,和上篇一样
4.若是要根据系别号,查该系的学生信息,步骤如下:
(1)首先在DeptDao中创建方法
在StudentDao中创建
(2)然后在DeptDao.xml中
因为返回的学生信息很多,所有要用List,
<resultMap id="DeptResult" type="test.model.Dept">
<result property="dno" column="dno"></result>
<result property="dname" column="dname"></result>
<result property="dcount" column="dcount"></result>
<result property="address" column="address"></result>
<collection property="students" column="dno"
select="test.dao.StudentDao.queryByDnoNotFoundDept"
javaType="java.util.List"
ofType="test.model.Student">
</collection>
</resultMap>
<!-- 将查询结果中的 dno 列作为关联属性的条件 -->
<!-- 调用 StudentDao 的 queryByDnoNotFoundDept 方法进行关联查询 -->
<!-- 关联属性的类型为 List -->
<!-- List 中元素的类型为 test.model.Student -->
在StudentDao.xml中
<select id="queryByDnoNotFoundDept" parameterType="java.lang.String" resultType="test.model.Student">
select *
from student s
where s.dno=#{dno}
</select>
(3)最后测试类