Mybatis-day09-复杂查询

多对一处理

一、查询嵌套查询 

<!--
    1、查询所有的学生信息
    2、根据查询出来的学生的tid,查询对应的老师
-->
    <select id="getStudent" resultMap="StudentTeacher">
        select * from mybatis.student
    </select>

    <resultMap id="StudentTeacher" type="stu">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
<!--        复杂的部分需要单独处理 对象:association 集合:collection
-->
<!--        peoperty是实体类中的属性;tid是对应的数据库中的属性;
            javaType是查询结果的实体类;select是嵌套查询的sql语句的名称-->
        <association property="teacher" column="tid" javaType="tea" select="getTeacher"/>

    </resultMap>

    <select id="getTeacher" resultType="tea">
        select * from mybatis.teacher where id = #{id}
    </select>

二、按照查询结果查询

    <!--按照查询结构嵌套-->
    <select id="getStudent2" resultMap="StudentTea">
         select s.id sid,s.name sname,t.name tname
         from student s,teacher t
         where s.tid=t.id;
    </select>
    <resultMap id="StudentTea" type="stu">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="tea">
            <result property="id" column="id"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐