CREATE TABLE `t_teacher` (
`t_id` int(11) NOT NULL AUTO_INCREMENT,
`t_name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`t_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
插入数据
INSERT INTO `t_teacher` VALUES ('1', '刘晓云');
INSERT INTO `t_teacher` VALUES ('2', '郑同华');
INSERT INTO `t_teacher` VALUES ('3', '李明军');
CREATE TABLE `t_class` (
`c_id` int(11) NOT NULL AUTO_INCREMENT,
`c_name` varchar(20) DEFAULT NULL,
`teacher_id` int(11) DEFAULT NULL,
PRIMARY KEY (`c_id`),
KEY `fk_teacher_id` (`teacher_id`),
CONSTRAINT `fk_teacher_id` FOREIGN KEY (`teacher_id`) REFERENCES `t_teacher` (`t_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
插入数据
INSERT INTO `t_class` VALUES ('1', '2020软件1班', '3');
INSERT INTO `t_class` VALUES ('2', '2020软件2班', '2');
INSERT INTO `t_class` VALUES ('3', '2020软件3班', '1');
CREATE TABLE `t_student` (
`s_id` int(11) NOT NULL AUTO_INCREMENT,
`s_name` varchar(30) DEFAULT NULL,
`s_gender` varchar(10) DEFAULT NULL,
`s_age` int(11) DEFAULT NULL,
`class_id` int(11) DEFAULT NULL,
PRIMARY KEY (`s_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
插入数据
INSERT INTO `t_student` VALUES ('1', '董大伟', '男','20','1');
INSERT INTO `t_student` VALUES ('2', '李方雨', '女','19','1');
package net.hw.mybatis.bean;
public class Teacher {
private int id;
private String 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 + '\'' +
'}';
}
}
package net.hw.mybatis.bean;
public class Student {
private int id;
private String name;
private String gender;
private int age;
private Clazz clazz;
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Clazz getClazz() {
return clazz;
}
public void setClazz(Clazz clazz) {
this.clazz = clazz;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", clazz=" + clazz.getName() +
'}';
}
}
package net.hw.mybatis.bean;
import java.util.List;
public class Clazz {
private int id;
private String name;
private Teacher teacher;
private List<Student> students;
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;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Clazz{" +
"id=" + id +
", name='" + name + '\'' +
", teacher=" + teacher +
", students=" + students +
'}';
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.hw.mybatis.mapper.ClazzMapper">
<!-- 三表关联查询 -->
<select id="findById" parameterType="int" resultMap="clazzResultMap">
SELECT * FROM t_teacher INNER JOIN t_class ON t_class.teacher_id = t_teacher.t_id
INNER JOIN t_student ON t_class.c_id = t_student.class_id WHERE c_id = #{id};
</select>
<!-- 定义班级结果映射-->
<resultMap id="clazzResultMap" type="Clazz">
<result property="id" column="c_id"/>
<result property="name" column="c_name"/>
<!-- association 一对一-->
<association property="teacher" javaType="Teacher" column="teacher_id">
<result property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
<!-- association 一对多-->
<collection property="students" ofType="Student">
<result property="id" column="s_id"/>
<result property="name" column="s_name"/>
<result property="gender" column="s_gender"/>
<result property="age" column="s_age"/>
<association property="clazz" javaType="Clazz" column="class_id">
<result property="name" column="c_name"/>
</association>
</collection>
</resultMap>
</mapper>
在配置里配置班级、教师与学生实体类别名,以及班级映射器配置文件
package net.hw.mybatis.mapper;
import net.hw.mybatis.bean.Clazz;
import java.util.List;
public interface ClazzMapper {
Clazz findById(int id);
List<Clazz> findAll();
}
package net.hw.mybatis.mapper;
import net.hw.mybatis.bean.Clazz;
import net.hw.mybatis.bean.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
/**
* 功能:测试用户映射器接口
* 作者:华卫
* 日期:2021年03月03日
*/
public class TextClazzMapper {
private SqlSession sqlSession; // SQL会话
private ClazzMapper clazzMapper; // 用户映射器
@Before
public void init() {
try {
// 读取MyBatis配置文件作为字符输入流
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 基于MyBatis配置文件构建SQL会话工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
// 利用SQL会话工厂获取SQL会话
sqlSession = factory.openSession();
// 利用SQL会话获取用户映射器对象
clazzMapper = sqlSession.getMapper(ClazzMapper.class);
// 提示用户SQL会话对象创建成功
System.out.println("sqlSession对象已创建。");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testFindById() {
int id = 1;
Clazz clazz = clazzMapper.findById(id);
System.out.println(clazz);
}
@Test
public void testFindByAll(){
List<Clazz> clazzes = clazzMapper.findAll();
for (Clazz clazz: clazzes) {
System.out.println(clazz);
}
}
@After
public void destroy() {
// 关闭SQL会话
sqlSession.close();
// 提示用户SQL会话对象关闭
System.out.println("sqlSession对象已关闭。");
}
}