2020-09-02Mybatis执行流程和resultMap再理解

Mybatis笔记四:

Mybatis执行流程和resultMap再理解:

1、执行流程以及底层原理
在这里插入图片描述

2、resultMap理解
准备:
多对一 : 多个学生关联一个老师 (关联)
association
查询所有学生以及对应老师的信息:
Student:

package com.itcast.pojo;

public class Student {
    private int id;
    private String name;
    private Teacher teacher;

    public Student() {
    }

    public Student(int id, String name, Teacher teacher) {
        this.id = id;
        this.name = name;
        this.teacher = teacher;
    }

    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;
    }

    public Teacher getTeacher() {
        return teacher;
    }

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

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

Teacher:

package com.itcast.pojo;

public class Teacher {
    private int id;
    private String name;

    public Teacher() {
    }

    public Teacher(int id, String name) {
        this.id = id;
        this.name = 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 "Teacher{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

查询所有学生以及对应老师的信息:
1、查询嵌套:
①查询所有学生信息:

<select id="getAllStudent" resultMap="StudentTeacher">
        select * from student;
</select>
<resultMap id="StudentTeacher" type="student">
     <id column="id" property="id"/>
     <id column="name" property="name"/>
     <association property="teacher" column="tid" javaType="teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="teacher">
      select * from teacher;
</select>

2、结果集嵌套:

    <select id="getAllStudent" resultMap="StudentTeacher">
        select s.id as sid,s.name as sname,t.id as tid,t.name as tname from student s,teacher t
        where s.tid = t.id;
    </select>
    <resultMap id="StudentTeacher" type="student">
        <id property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="teacher">
            <result property="id" column="tid"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>

准备
一对多 :一个老师有很多学生(集合)
collection
Student1:

package com.itcast.pojo;

public class Student1 {
    private int id;
    private String name;
    private int tid;

    public Student1() {
    }

    public Student1(int id, String name, int tid) {
        this.id = id;
        this.name = name;
        this.tid = tid;
    }

    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;
    }

    public int getTid() {
        return tid;
    }

    public void setTid(int tid) {
        this.tid = tid;
    }

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

Teacher1:

package com.itcast.pojo;

import java.util.List;

public class Teacher1 {
    private int id;
    private String name;
    private List<Student1> student1List;

    public Teacher1() {
    }

    public Teacher1(int id, String name, List<Student1> student1List) {
        this.id = id;
        this.name = name;
        this.student1List = student1List;
    }

    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;
    }

    public List<Student1> getStudent1List() {
        return student1List;
    }

    public void setStudent1List(List<Student1> student1List) {
        this.student1List = student1List;
    }

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

结果期嵌套:

    <select id="getTeacher" resultMap="TeacherStudent">
        select t.id tid,t.name tname,s.id sid,s.name sname from teacher t,student s where s.tid = t.id;
    </select>
    <resultMap id="TeacherStudent" type="teacher1">
        <id property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="student1List" ofType="student1">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
        </collection>
    </resultMap>

**注意:association为javaType;collection为ofType

demo结构:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值