日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Mybatis對于多對一和一對多的處理

作者:Qiddo 更新時間: 2023-12-11 編程語言

采用老師和學生的案例來講解,每個處理兩種方法

多對一

  • 多個學生對應一個老師

  • 如果對于學生這邊,就是一個多對一的現象,即從學生這邊關聯一個老師!

?兩種方法

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qi.dao.StudentMapper">
    <!--按照結果進行嵌套處理-->
    <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>

<!--=============================================================-->
    <!--
        1.查詢所有的學生信息
        2.根據查詢出來的學生的tid,尋找對應的老師!(子查詢)
    -->
    <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 where id = #{tid}
    </select>
</mapper>

上面的sql語句難寫點,resultMap簡單

下面的sql語句簡單,是子查詢

一對多

  • 一個老師擁有多個學生

  • 如果對于老師這邊,就是一個一對多的現象,即從一個老師下面擁有一群學生(集合)!

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qi.dao.TeacherMapper">
    <!--按結果嵌套查詢-->
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.id sid,s.name sname,t.name tname,t.id tid
        from student s,teacher t
        where s.tid=t.id and t.id=#{tid}
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <!--復雜的屬性我們需要單獨處理 對象:association 集合:collection-->
        <!--javaType="" 指定屬性的類型-->
        <!--集合中的泛型信息,我們使用ofType獲取-->
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

    <!--======================================================================-->
    <select id="getTeacher2" resultMap="TeacherStudent2">
        select * from mybatis.teacher where id =#{tid}
    </select>
    <resultMap id="TeacherStudent2" type="Teacher">
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
    </resultMap>
    <select id="getStudentByTeacherId" resultType="Student">
        select * from mybatis.student where tid =#{tid}
    </select>

</mapper>

大家對照著去理解,有點基礎的應該都能看得懂,照貓畫虎就行!

原文鏈接:https://blog.csdn.net/m0_73944607/article/details/134764374

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新