hibinate的联合查询分为一对一,一对多,多对一以及多对多,而mybatis则将一对多,多对一,多对多进行了合并处理 (可以通过例子来理解,可以把多对多理解为多个一对多相加),因此在mybatis的联合查询中只有一对一和一对多
1. 一对一实现
a.通过业务扩展类实现
- 核心:用resultType指定类的属性 包含 多表查询的所有字段
- 说明:例如:业务扩展类 StudentBusiness包含父类student,并且在自身类中实现了StudentCard类的属性。这里StudentBusiness实际上包含了两个实体类的属性,扩展了student的整体属性,我们称为业务扩展
我们通过业务扩展类来实现mybatis的一对一查询 ( 说人话就是将多表联合查询的所有字段都与新建的业务扩展类的属性进行对应 )
package com.zzw.entity;
public class StudentBusiness extends Student{//学生业务扩展类
private int cardId;
private String cardInfo;
public int getCardId() {
return cardId;
}
public void setCardId(int cardId) {
this.cardId = cardId;
}
public String getCardInfo() {
return cardInfo;
}
public void setCardInfo(String cardInfo) {
this.cardInfo = cardInfo;
}
@Override
public String toString() {
return super.toString()+"\n"+this.cardId+"--"+this.cardInfo;
}
}
b.通过resultMap实现
- 通过属性成员 将2个类建立起联系(和表与表之间建立外键约束很相似)
package com.zzw.entity;
public class Student {//包含学生与学生证信息
private int stuNo ;
private String stuName ;
private int stuAge ;
private String graName ;
private boolean stuSex;
private StudentCard studentCard;
public Student() {
}
public Student(int stuNo, String stuName, int stuAge, String graName,boolean sex) {
this.stuNo = stuNo;
this.stuName = stuName;
this.stuAge = stuAge;
this.graName = graName;
this.stuSex = sex;
}
public StudentCard getStudentCard() {
return studentCard;
}
public void setStudentCard(StudentCard studentCard) {
this.studentCard = studentCard;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getGraName() {
return graName;
}
public void setGraName(String graName) {
this.graName = graName;
}
public boolean isStuSex() {
return stuSex;
}
public void setStuSex(boolean stuSex) {
this.stuSex = stuSex;
}
@Override
public String toString() {
//return this.stuNo+"-"+this.stuName+"-"+this.stuAge+"-"+this.graName+"-"+this.stuSex;
return this.stuNo+"-"+this.stuName+"-"+this.stuAge+"-"+
this.studentCard.getCardId()+"-"+this.studentCard.getCardInfo();
}
}
- 在mapper.xml中使用resultMap对上面类之间以及表之间的关系进行映射(一对一使用association标签)
<resultMap type="student" id="student_card_map">
<!-- 学生的信息 -->
<id property="stuNo" column="stuNo"/>
<result property="stuName" column="stuName" />
<result property="stuAge" column="stuAge" />
<!-- 一对一时,对象成员使用 association映射;javaType指定该属性的类型-->
<association property="card" javaType="StudentCard" >
<id property="cardId" column="cardId"/>
<result property="cardInfo" column="cardInfo"/>
</association>
</resultMap>
以上就是mybatis联合查询的一对一的两种方式
2.一对多实现
(例子中表达的意思是一般班级包含多个学生,即一对多)
1.建立班级类,包含一个学生的list
package com.zzw.entity;
import java.util.List;
public class StudentClass {
private int classId;
private String className;
//班级里的学生 通过该属性建立起两者的联系
List<Student> students;
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public int getClassId() {
return classId;
}
public void setClassId(int classId) {
this.classId = classId;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return this.classId+"-"+this.className;
}
}
2.处理一对多的关键,通过resultMap来实现表和类的映射(一对多使用collection标签)
<select id="queryClassAndStudents" parameterType="int" resultMap="class_student_map">
<!-- 查询g1班的所有信息及班级学生信息 -->
select s.*,c.* from student s inner join studentclass c on s.classid=c.classid where c.classid=#{classId}
</select>
<!-- resultMap里永远放类与表的对应关系-->
<resultMap type="studentClass" id="class_student_map">
<!-- 应为type的主类是studentClass 所以先配置该类 -->
<id property="classId" column="classid"/>
<result property="className" column="classname"/>
<!-- 配置成员属性学生 一对多; 属性类型:javaType 属性的元素类型:ofType -->
<collection property="students" ofType="Student">
<id property="stuNo" column="stuno"/>
<result property="stuName" column="stuname"/>
</collection>
</resultMap>