【MyBatis学习笔记】一对一、一对多关联

一对一关联

1.用业务扩展类实现:

​ 三张表三个实体类 Student StudentCard StudentBusiness(继承多属性的类重写少属性的类)

xml:

<select id="queryStudentByOneToOne" parameterType="int" resultType="StudentBusiness">
    select s.*, s2.* from student s inner join studentcard s2 on s.cid = s2.cid where s.sid = #{sid}
</select>

java.test:

StudentBusiness studentBusiness = studentMapper.queryStudentByOneToOne(4);

2.用resultMap实现:

​ a.通过 属性成员 将2个类建立起联系

xml:

<select id="queryStudentOTOWithResultMap" parameterType="int" resultMap="student_card_map">
    select s.*, s2.* from student s inner join studentcard s2 on s.cid = s2.cid
    where s.sid = #{sid}
</select>

<resultMap id="student_card_map" type="student">
    <id property="sid" column="sid"/>
    <result property="sname" column="sname"/>
    <result property="sage" column="sage"/>
    <result property="sgender" column="sgender"/>
    <result property="gname" column="gname"/>

    <!--
        一对一时,对象成员使用association映射
        javaType指定该属性类型
    -->
    <association property="studentCard" javaType="StudentCard">
        <id property="cid" column="cid"/>
        <result property="cinfo" column="cinfo"/>
    </association>

    <association property="address" javaType="Address">
        <result property="homeAddress" column="homeaddress"/>
        <result property="schoolAddress" column="schooladdress"/>
    </association>
</resultMap>

test.java:

student stu = studentMapper.queryStudentOTOWithResultMap(4);
一对多关联

xml:

select c.*, s.* from class c inner join student s on c.classid = s.classid where c.classid = #{classid}
<resultMap id="class_student_map" type="Class">
    <id property="classid" column="classid"/>
    <result property="classname" column="classname"/>

    <!--
        一对多时,对象成员使用collection映射
        javaType指定该属性类型
        ofType指属性的元素类型
    -->
    <collection property="students" ofType="student">
        <id property="sid" column="sid"/>
        <result property="sname" column="sname"/>
        <result property="sage" column="sage"/>
        <result property="sgender" column="sgender"/>
        <result property="gname" column="gname"/>

        <association property="studentCard" javaType="StudentCard">
            <id property="cid" column="cid"/>
            <result property="cinfo" column="cinfo"/>
        </association>

        <association property="address" javaType="Address">
            <result property="homeAddress" column="homeaddress"/>
            <result property="schoolAddress" column="schooladdress"/>
        </association>
    </collection>
</resultMap>

test.java:

    Class Class = studentMapper.queryClassAndStudent(1);
    List<student> students = Class.getStudents();
    for(student stu : students){
        System.out.println(stu);
    }
    //System.out.println(Class);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值