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结构: