多對一處理 和一對多處理的處理

HuDu發表於2020-06-20

在前面建立的兩個表中,學生和老師的關係,學生對於老師是多對一的關係,老師對於學生是一對多的關係。在MyBatis中處理這樣的相對複雜的關係需要用到物件(association)和 集合(collection)。

多對一

  • 實體類

    public class Student2 {
      private int id;
      private String name;
      //學生和老師是多對一的關係,需要在學生中關聯老師
      private Teacher2 teacher;
    }
    public class Teacher2 {
    private int id;
    private String name;
    }
  • 介面

    public interface Student2Mapper {
      //獲取所有學生資訊以及他的老師
      List<Student2> getStudent();
      List<Student2> getStudent2();
    }
  • xml配置

    <!--按照結果巢狀處理-->
      <select id="getStudent" resultMap="StudentTeacher">
          select s.id sid,s.name sname,t.name tname,t.id tid
          from student s,teacher t
          where s.tid = t.id
      </select>
    
      <resultMap id="StudentTeacher" type="Student2">
          <result property="id" column="sid"/>
          <result property="name" column="sname"/>
          <association property="teacher" javaType="Teacher2">
              <result property="id" column="tid"/>
              <result property="name" column="tname"/>
          </association>
      </resultMap>
    
      <!--通過子查詢的方法-->
      <select id="getStudent2" resultMap="StudentTeacher2">
          select * from student
      </select>
    
      <resultMap id="StudentTeacher2" type="Student2">
          <!-- 複雜的屬性需要單獨處理 物件:association 集合:collection -->
          <association property="teacher" column="tid" javaType="Teacher2" select="getTeacher"/>
      </resultMap>
      <select id="getTeacher" resultType="Teacher2">
          select * from teacher where id = #{tid}
      </select>
  • 一對多

這是從老師的角度,實體類如下

public class Student {
    private int id;
    private String name;
    private int tid;
}

public class Teacher {
  private int id;
 private String name;
 private List<Student> students;
}
List<Teacher> getTeacher2(@Param("tid") int id);
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hudu.dao.TeacherMapper">
    <select id="getTeacher" resultType="Teacher">
        select * from teacher
    </select>

    <!--按照結果巢狀-->
    <select id="getTeacher2" resultMap="TeacherStudent">
        SELECT s.id sid,s.name sname,t.name tname,t.id tid
        FROM teacher t,student s
        where t.id = s.tid 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="getTeacher3" resultMap="TeacherStudent3">
        select * from teacher where id = #{tid}
    </select>

    <resultMap id="TeacherStudent3" type="Teacher">
        <!--<result property="id" column="id"/>-->
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
    </resultMap>

    <select id="getStudentByTeacherId" resultType="Student">
        select * from student where tid = #{tid};
    </select>
</mapper>
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章