1. 程式人生 > >MyBatis的學習(七)——關聯對映之一對多關聯

MyBatis的學習(七)——關聯對映之一對多關聯

本次部落格其它相關可以檢視之前的部落格的內容。

一對多使用到的主要是:collection

根據classes的id查詢對應的班級資訊,包括學生,老師

上一篇部落格已經建立classes和teacher表,這裡不再多說,本次建立student表即可:

定義student實體類:

package com.little.entity;

public class Student {
    private int id; // id===>s_id
    private String name; // name===>s_name

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

修改Classes類,新增一個List<Student> students屬性,使用一個List<Student>集合屬性表示班級擁有的學生,如下:

package com.little.entity;

import java.util.List;

public class Classes {
    private Integer id;
    private String name;
    private Teacher teacher;
    // 使用一個List<Student>集合屬性表示班級擁有的學生
    private List<Student> students;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    @Override
    public String toString() {
        return "Classes{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", teacher=" + teacher +
                ", students=" + students +
                '}';
    }
}

ClassesMapper介面:

Classes getClassesById3(Integer id);

ClasserMapper介面實現配置檔案:

<!-- 根據classesId查詢對應的班級資訊,包括學生,老師 -->
    <!-- 方式一: 巢狀結果: 使用巢狀結果對映來處理重複的聯合結果的子集 SELECT * FROM classes c, teacher t,student
        s WHERE c.teacher_id=t.t_id AND c.C_id=s.class_id AND c.c_id=1 -->
    <select id="getClassesById3" parameterType="int" resultMap="ClassResultMap3">
		select *
		from classes c, teacher t,student s where c.teacher_id=t.t_id and
		c.C_id=s.class_id and c.c_id=#{id}
	</select>
    <resultMap type="Classes" id="ClassResultMap3">
        <id property="id" column="c_id" />
        <result property="name" column="c_name" />
        <association property="teacher" column="teacher_id"
                     javaType="Teacher">
            <id property="id" column="t_id" />
            <result property="name" column="t_name" />
        </association>
        <!-- ofType指定students集合中的物件型別 -->
        <collection property="students" ofType="Student">
            <id property="id" column="s_id" />
            <result property="name" column="s_name" />
        </collection>
    </resultMap>

測試:

@Test
    public void t3(){
        Classes classes = cm.getClassesById3(1);
        System.out.println(classes);
    }

結果:

上面介紹了方式一,通過巢狀查詢結果的方式實現,下面介紹第二種方式,通過巢狀查詢條件的方式實現。

ClasserMapper介面實現配置檔案:

  <!-- 方式二:巢狀查詢:通過執行另外一個SQL對映語句來返回預期的複雜型別 SELECT * FROM classes WHERE c_id=1;
		SELECT * FROM teacher WHERE t_id=1 //1 是上一個查詢得到的teacher_id的值 SELECT * FROM
		student WHERE class_id=1 //1是第一個查詢得到的c_id欄位的值 -->

    <select id="getClassesById4" parameterType="int" resultMap="ClassResultMap4">
		select * from classes where c_id=#{id}
	</select>

    <resultMap type="Classes" id="ClassResultMap4">
        <id property="id" column="c_id" />
        <result property="name" column="c_name" />
        <association property="teacher" column="teacher_id" javaType="Teacher" select="getTeacher2"></association>
        <collection property="students" ofType="Student" column="c_id" select="getStudent"></collection>
    </resultMap>

    <select id="getTeacher2" parameterType="int" resultType="Teacher">
		SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
	</select>

    <select id="getStudent" parameterType="int" resultType="Student">
		SELECT s_id id, s_name name FROM student WHERE class_id=#{id}
	</select>

測試:

@Test
    public void t4(){
        Classes classes = cm.getClassesById4(1);
        System.out.println(classes);
    }

結果:

MyBatis中使用collection標籤來解決一對多的關聯查詢,ofType屬性指定集合中元素的物件型別