ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

多对一和一对多

2022-04-20 17:00:42  阅读:234  来源: 互联网

标签:name teacher mybatis student tid 一对 id


多对一查询的实现方式

1.子查询 [SQL简单,映射复杂]

Student 类:  int id ;String name; Teacher teacher

Teacher类:int id ; String name;

<!--    思路1:
            1.查询所有的学生
            2.根据查询出来的学生的tid,寻找对应的老师
-->
        <select id="getStudent"  resultMap="Student_Teacher">
         select * from mybatis.student
        </select>

    <resultMap id="Student_Teacher" type="Student">
<!--        复杂的属性单独处理  对象association   集合collection-->
        <association property="teacher" column="tid"
                     javaType="Teacher" select="getTeacher"/>
    </resultMap>

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


在student表内利用getStudent获取全部的学生  返回类型为Student , 但是因为学生的属性是 ID  name 和 teacher对象, 所以返回类型需要使用resultMap加以"扩展", 在里面进行新的关系映射( teacher对象映射为tid字段),并且指定javaType   ,然后根据映射的字段,在teacher表内查询(结果赋给student对象的teacher属性)

 

2.连表查询(推荐)   [SQL复杂,映射简单]

直接通过sql语句查询出来,然后映射到属性

    <select id="getStudent" resultMap="Student_Teacher">
        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="Student_Teacher" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
            <result property="id" column="tid"/>
        </association>
    </resultMap>

通过起别名和连表查询在数据库中得到新表α

仍然通过resultMap"扩展",   经过与新表α属性名-字段的关系映射  ,直接获得结果

 

属性名:JAVA类中的属性名

字段:数据库中的字段

 

一对多实现 

    <select id="get_Teacher_students" resultMap="Student_Teacher">
        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 and t.id=#{id}
    </select>
    <resultMap id="Student_Teacher" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>

        <collection property="student" ofType="Student" >
            <result property="name" column="sname"/>
            <result property="id" column="sid"/>
        </collection>
    </resultMap>

 数据库

teacher:id name

studnet: id name tid   (tid=id)

使用嵌套查询实现:

javaType="ArrayList"  :指定实体类中属性的类型  (可省略)
ofType="Student"      :指定映射到list或者集合中的类型,泛型中的约束类型()
    <select id="get_Teacher_students02" resultMap="Student_Teacher02">
        select * from  mybatis.teacher where id=#{tid}
    </select>

    <resultMap id="Student_Teacher02" type="Teacher">
        <collection property="student" javaType="ArrayList" ofType="Student"
                    select="getStudentByteacher" column="id"/>
    </resultMap>

    <select id="getStudentByteacher" resultType="Student">
        select *
        from mybatis.student
        where tid=#{tid_sb}
    </select>

先select查询到指定ID的老师,    对返回结果进行拓展,  里面的student集合α(通过另外一个select 查询得到)

 select :利用colum传递查询的条件id(老师的id)    对学生表的tid字段进行查询,得到符合条件的student集合α

 

mysql引擎

InnoDB的底层原理

索引

索引优化

标签:name,teacher,mybatis,student,tid,一对,id
来源: https://www.cnblogs.com/liujinmeng/p/16158550.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有