1. 程式人生 > 其它 >六步學會mybatis---------第四章:resultMap處理 多表聯合之聯表查詢

六步學會mybatis---------第四章:resultMap處理 多表聯合之聯表查詢

<resultMap>標籤下的子標籤
<association>處理單個物件資料
<collection>處理多個物件

1. 一對一

Student類

public class Student {
    private Integer id;
    private String name;
    private Integer age;
    // 一對一
    private Computer computer;
    //...省略
    }

Computer類

public class Computer {
    private Integer id;
private String moudle; }

資料庫裡學生和電腦資訊在兩張表裡儲存
在這裡插入圖片描述
在這裡插入圖片描述
要求查出學生時,把電腦資訊也查出來

<select id="selAll" resultMap="StudentResultMap">
        select s.`s_id`,s.`s_name`,s.`s_age`,s.`c_id`,c.`c_moudle`
            from student s inner join computer c where s.`c_id`=c.`c_id`
    </select
>
<!--一對一--> <resultMap id="StudentResultMap" type="Student"> <id column="s_id" property="id"></id> <result column="s_name" property="name"></result> <result column="
s_age"
property="age">
</result> <association property="computer" javaType="Computer"> <id column="c_id" property="id"></id> <result column="c_moudle" property="moudle"></result> </association> </resultMap>
 <association>

property : 屬性名
javaType : 該屬性的型別

2. 一對多

public class User {
    private Integer id;
    private String name;
    private Integer age;
    private String gender;
    // 一對多
    private List<Order> orderList;
    // ...
    }
public class Order {
    private Integer id;
    private String content;
    private Double price;
    // ...
    }

查使用者時把使用者的訂單都查出來

<select id="selAll" resultMap="UserResultMap">
        SELECT u.`u_id`,u.`u_name`,u.`u_gender`,u.`u_age`,o.`o_id`,o.`o_content`,o.`o_prince`
            FROM USER u INNER JOIN orders o WHERE u.`u_id`=o.`u_id`
    </select>

    <!--一對多-->
    <resultMap id="UserResultMap" type="com.wh.pojo.User">
        <id column="u_id" property="id"></id>
        <result column="u_name" property="name"></result>
        <result column="u_gender" property="gender"></result>
        <result column="u_age" property="age"></result>
        <collection property="orderList" ofType="Order">
            <id column="o_id" property="id"></id>
            <result column="o_content" property="content"></result>
            <result column="o_prince" property="price"></result>
        </collection>
    </resultMap>
<collection>

property: 屬性名
ofType :多的一方屬性肯定是集合,ofType表示集合的泛型型別

3.多對多

多對多建表有三張表,多了個關聯表。
建實體物件是有些人把關聯表也建立成物件
有些人只不建立關聯表物件。。。
(1)建立關聯表物件

public class StudentTeacher {
    private Integer sid;
    private Integer tid;
    private Student student;
    private Teacher teacher;
    //...
    }
public class Student {
    private Integer id;
    private String name;
    private List<StudentTeacher> studentTeacherList;
    //...
    }
public class Teacher {
    private Integer id;
    private String name;
    private List<StudentTeacher> studentTeacherList;
    //...
    }
<select id="selTeaById" parameterType="int" resultMap="TeacherResultMap">
        select * from teacher t inner join student_teacher st
	        INNER JOIN student s where t.t_id=st.st_tid and st.st_sid=s.s_id and t.t_id=#{id}
    </select>
    <resultMap id="BaseResultMap" type="Teacher">
        <id column="t_id" property="id"></id>
        <result column="t_name" property="name"></result>
    </resultMap>
    <resultMap id="TeacherResultMap" type="Teacher" extends="BaseResultMap">
        <collection property="studentTeacherList" ofType="StudentTeacher">
            <result column="st_sid" property="sid"></result>
            <result column="st_tid" property="tid"></result>
            <association property="student" javaType="Student">
                <id column="s_id" property="id"></id>
                <result column="s_name" property="name"></result>
            </association>
        </collection>
    </resultMap>

<resultMap 標籤裡的內容是可以繼承的
extends="BaseResultMap"就是將標籤的內容繼承過來

不建關係表物件

public class Teacher {
    private Integer id;
    private String name;
    private String title;
    // 多對多
    private List<Course> courseList;
    }
public class Course {
    private Integer id;
    private String name;
    private Integer time;
    // 多對多
    private List<Teacher> teacherList;
    }
<select id="selAll" resultMap="TeacherResultMap">
        SELECT * FROM `teacher` t INNER JOIN `r_teacher_course` r
                    INNER JOIN `course` c WHERE t.t_id=r.t_id AND c.c_id=r.c_id
    </select>

    <!--多對多-->
    <resultMap id="TeacherResultMap" type="Teacher">
        <id column="t_id" property="id"></id>
        <result column="t_name" property="name"></result>
        <result column="t_title" property="title"></result>
        <collection property="courseList" ofType="Course">
            <id column="c_id" property="id"></id>
            <result column="c_name" property="name"></result>
            <result column="c_time" property="time"></result>
        </collection>
    </resultMap>

這裡用的都是 聯表查詢
聯表查詢基於笛卡爾積,如果資料量大,笛卡爾積的記憶體消耗會很大。
一種sql優化就是將聯表查詢進行拆分,拆成單表查詢