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>

相关文章

今天小编给大家分享的是MyBatis批量查询、插入、更新、删除如...
今天小编给大家分享的是Mybatis操作多数据源实现的方法,相信...
本篇文章和大家了解一下mybatis集成到spring的方式有哪些。有...
今天小编给大家分享的是mybatis-plus分页查询的3种方法,相信...
本篇内容主要讲解“mybatis之BaseTypeHandler怎么使用”,感...
这篇文章主要介绍了mybatisforeach怎么传两个参数的相关知识...