关联查询:
一对一:
a.业务扩展类
<select id="queryStudentBystunoWithOO" parameterType="int"
resultType="studentBusiness">
select student.*,studentcard.* from student student inner join studentcard
on student.cardid=studentcard.cardid
where student.stuno=#{stuNo}
</select>
写一个业务扩展类,继承表中数据多的。
用resultType指定类的属性 包含多表查询的所有字段
b.resultMap
1.通过属性成员,将两个类联系到一起
2.写映射文件
<resultMap type="student" id="student_card_map">
<!-- 学生信息 -->
<id property="stuNo" column="stuNo" />
<result property="stuName" column="stuName" />
<result property="stuAge" column="stuAge" />
<!-- 一一对应,对象成员使用association映射,javatype指定该属性的类型 -->
<association property="card" javaType="StudentCard">
<id property="cardid" column="cardid" />
<result property="cardinfo" column="cardinfo" />
</association>
</resultMap>
一对一用:association
一对多用:collection
建立联系:
表:用外键
类:添加成员变量