多对一

<select id="selectAllStudent" resultMap="StudentTeacher">
        select s.id as sid,s.name as sname,t.name as tname,t.id as tid from student as s,teacher as t
        where s.tid=t.id
    </select>

    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher"  javaType="Teacher">
            <result property="id" column="tid"></result>
            <result property="name" column="tname"></result>

        </association>
    </resultMap>

javaType 参数是实体类的类型,property实体类变量的名。

一对多

 <select id="getTeacher" resultMap="TeacherStudent">
        select s.id as sid,s.name as sname,t.name as tname,t.id as tid
        from student as s,teacher as t
        where t.id=s.tid
        and t.id=#{tid}
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="Student">
            <result property="id" column="sid"></result>
            <result property="name" column="sname"></result>
            <result property="tid" column="tid"></result>
        </collection>
    </resultMap>