前期准备,有两张表student2,和student2card
Student2表中cardid作为外键,与student2card表中的cradid关联。
一对一关联查询有两种方式:
方式一:使用业务拓展类(适合数据量较小)
<!-- 利用业务扩展类实现一对一 -->
<select id="queryStudentByNoWithOO" parameterType="int" resultType="StudentBusiness">
select s.*,c.*
from student2 s join student2card c
on s.cardid=c.cardid
where s.stuno=#{stuNo}
</select>
业务拓展类指的是resultType返回值,因为根据学生编号,可以查询出student2表中的一条数据,同时也能查询出student2card表中的一条。这个时候,java类中没有一种类型能够包括两张表的数据,所以就自定义一种类型,通过继承实现一种新类型。
//学生业务扩展类
public class StudentBusiness extends Student{
private int cardId;
private String cardInfo;
public int getCardId() {
ret