多对一处理-Mybatis

按照查询嵌套处理

   <!--思路:
		1. 查询所有的学生信息
		2. 根据查询出来的学生的tid 寻找对应的老师 子查询
	-->

<select id="getAllStudents" resultMap="StudentMap">
        select * from mybatis.student;
    </select>

    <resultMap id="StudentMap" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="teacher" column="tid"  javaType="Teacher" 				select="getTeachers"/>
    </resultMap>

    <select id="getTeachers" resultType="teacher">
        select * from mybatis.teacher where id =#{tid};
    </select>
    

按结果嵌套处理

<!--第二种按查询结果嵌套处理-->
    <select id="getAllStudents2" resultMap="StudentMap2" >
        select s.id sid,s.name sname,t.name tname, t.id tid
        from mybatis.student s, mybatis.teacher t
        where s.tid = t.id;
    </select>

    <resultMap id="StudentMap2" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="Teacher">
            <result property="id" column="tid"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>
posted @ 2021-07-08 13:29  withLevi  阅读(72)  评论(0编辑  收藏  举报