首页 > 其他分享 >Mybatis 复杂查询多对一 和 一对多

Mybatis 复杂查询多对一 和 一对多

时间:2022-10-22 20:57:41浏览次数:36  
标签:name id 查询 student Mybatis 一对 teacher select

多对一的处理:

<!--方法一 根据结构查询  联表 -->
    <select id="getStudent2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.name tname from student s,teacher t where s.tid=t.id;
    </select>
    
    <resultMap id="StudentTeacher2" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>
 <!-- 方法二  子查询   嵌套-->
    <select id="getStudent" resultMap="StudentTeacher">
        select * from student ;
    </select>

    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--          复杂的属性,我们需要单独处理   对象:association  集合:collection-->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>

    <select id="getTeacher" resultType="Teacher">
        select * from teacher;
    </select>

 

一对多的处理:

 

标签:name,id,查询,student,Mybatis,一对,teacher,select
From: https://www.cnblogs.com/kidzxy/p/16817256.html

相关文章