在这篇文章里面主要讲解如何在mybatis里面使用一对一、一对多、多表联合查询(类似视图)操作的例子。
注:阅读本文前请先大概看一下之前两篇文章。
一、表结构
班级表class,学生表student,班级学生关系表ClassStudent。
这里一个学生只会在一个班级里面,也就是一对一的关系;一个班级有多个学生,也就是一对多的关系。
结构如下:
CREATETABLE[dbo].[Class](
[class_id][int]NOTNULL,
[class_name]varchar NOTNULL,
CONSTRAINT[PK_Class]PRIMARYKEYCLUSTERED
(
[class_id]ASC
)WITH (PAD_INDEX =OFF, STATISTICS_NORECOMPUTE =OFF, IGNORE_DUP_KEY =OFF, ALLOW_ROW_LOCKS =ON, ALLOW_PAGE_LOCKS =ON) ON[PRIMARY]
) ON[PRIMARY]CREATETABLE[dbo].[ClassStudent](
[class_id][int]NOTNULL,
[student_id][int]NOTNULL
) ON[PRIMARY]CREATETABLE[dbo].[Student](
[s_id][int]NOTNULL,
[s_name]varchar NOTNULL,
CONSTRAINT[PK_Student]PRIMARYKEYCLUSTERED
(
[s_id]ASC
)WITH (PAD_INDEX =OFF, STATISTICS_NORECOMPUTE =OFF, IGNORE_DUP_KEY =OFF, ALLOW_ROW_LOCKS =ON, ALLOW_PAGE_LOCKS =ON) ON[PRIMARY]
) ON[PRIMARY]
3张表的数据如下:
insertinto Class values(1,’三(1)班’)
insertinto Class values(2,’三(2)班’)
insertinto Class values(3,’三(3)班’)
insertinto Class values(4,’三(4)班’)
insertinto Student values(1001,’张三’)
insertinto Student values(1002,’李四’)
insertinto Student values(1003,’赵五’)
insertinto Student values(1004,’王二麻子’)
insertinto ClassStudent values(1,1001)
insertinto ClassStudent values(1,1003)
insertinto ClassStudent values(4,1002)
insertinto ClassStudent values(3,1004)
二、在原项目下新建一个包com.mybatis.sqljoinrelation,在包里面新建一个sqlMapper.xml的映射文件,在项目的mybatis的配置文件conf.xml中对这个sqlMapper.xml进行注册,注册片段如下:
紧接着在原项目下再新建一个包com.mybatis.bean,在里面新建实体类
学生:
package com.mybatis.bean;
/**
* 学生(临时)
*/publicclass StudentTemp {
privateint studentid;
private String studentname;
private Class tempClass;
public Class getTempClass() {
return tempClass;
}
publicvoid setTempClass(Class tempClass) {
this.tempClass = tempClass;
}
publicint getStudentid() {
return studentid;
}
publicvoid setStudentid(int studentid) {
this.studentid = studentid;
}
public String getStudentname() {
return studentname;
}
publicvoid setStudentname(String studentname) {
this.studentname = studentname;
}
@Override
public String toString() {
return "StudentTemp [studentid=" + studentid + ", studentname="
+ studentname + ", tempClass=" + tempClass + "]";
}
}
班级:
package com.mybatis.bean;
/**
* 班级
*/publicclass Class {
privateint classid;
private String classname;
publicint getClassid() {
return classid;
}
publicvoid setClassid(int classid) {
this.classid = classid;
}
public String getClassname() {
return classname;
}
publicvoid setClassname(String classname) {
this.classname = classname;
}
@Override
public String toString() {
return "Class [classid=" + classid + ", classname=" + classname + "]";
}
}
学生班级视图类:
package com.mybatis.bean;
publicclass studentclass {
privateint s_id;
private String s_name;
privateint class_id;
private String class_name;
publicint getS_id() {
return s_id;
}
publicvoid setS_id(int s_id) {
this.s_id = s_id;
}
public String getS_name() {
return s_name;
}
publicvoid setS_name(String s_name) {
this.s_name = s_name;
}
publicint getClass_id() {
return class_id;
}
publicvoid setClass_id(int class_id) {
this.class_id = class_id;
}
public String getClass_name() {
return class_name;
}
publicvoid setClass_name(String class_name) {
this.class_name = class_name;
}
@Override
public String toString() {
return “studentclass [s_id=” + s_id + “, s_name=” + s_name
+ “, class_id=” + class_id + “, class_name=” + class_name + “]”;
}
}
三、查询操作
1、 一对一,查询学生编号等于1001且他所在班级信息
2、一对多,查询班级编号等于1且包含所有学生信息
3、类似视图查询学生编号等于1001且他所在班级信息
sqlMapper.xml如下: