班级表与教师表是一对一的关联(one-to-one association)
班级表与学生表是一对多的关联(one-to-many association)
t_teacher
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=utf8mb4;
INSERT INTO `t_teacher` VALUES ('1', '刘晓云');
INSERT INTO `t_teacher` VALUES ('2', '郑同华');
INSERT INTO `t_teacher` VALUES ('3', '李明军');
t_class
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=utf8mb4;
INSERT INTO `t_class` VALUES ('1', '2021软件1班', '3');
INSERT INTO `t_class` VALUES ('2', '2021软件2班', '2');
INSERT INTO `t_class` VALUES ('3', '2021软件3班', '1');
t_student
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=utf8mb4;
INSERT INTO `t_student` VALUES ('1', '佟大为', '男', '20', '1');
INSERT INTO `t_student` VALUEs ('2', '李方玉', '女', '19', '1');
INSERT INTO `t_student` VALUES ('3', '郑大林', '男', '19', '2');
INSERT INTO `t_student` VALUES ('4', '温晓辉', '男', '18', '1');
INSERT INTO `t_student` VALUES ('5', '吴文静', '女', '19', '2');
INSERT INTO `t_student` VALUES ('6', '肖文艳', '女', '18', '3');
INSERT INTO `t_student` VALUES ('7', '杨文军', '男', '20', '3');
INSERT INTO `t_student` VALUES ('8', '唐雨涵', '女', '19', '2');
INSERT INTo `t_student` VALUES ('9', '金雨欣', '女', '20', '2');
package net.huawei.mybatis.bean;
/**
* 功能:教师实体类
* 作者:华卫
* 日期:2023年04月11日
*/
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.huawei.mybatis.bean;
/**
* 功能:学生实体类
* 作者:华卫
* 日期:2023年04月11日
*/
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() +
'}';
}
}
class_id
(作为外键),但是学生实体类的第5个属性并不是班级编号,而是班级类(clazz),此时尚未创建,因此代码会报错。toString()
方法里,输出的不是班级实体类的完整信息,而是班级名称,通过clazz.getName()
获取。package net.huawei.mybatis.bean;
import java.util.List;
/**
* 功能:班级实体类
* 作者:华卫
* 日期:2023年04月11日
*/
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.getName() +
", students=" + students +
'}';
}
}
toString()
方法里,输出的不是教师实体类的完整信息,而是教师姓名,通过teacher.getName()
获取。
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 property="teacher" column="teacher_id" javaType="Teacher">
<result property="id" column="t_id"/>
<result property="name" column="t_name"/>
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" column="class_id" javaType="Clazz">
<result property="name" column="c_name"/>
association>
collection>
resultMap>
<select id="findAll" resultMap="clazzResultMap2">
SELECT * FROM t_class;
select>
<resultMap id="clazzResultMap2" type="Clazz">
<result property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column="teacher_id" javaType="Teacher" select="getTeacher"/>
resultMap>
<select id="getTeacher" resultType="Teacher">
SELECT t_id id, t_name name FROM t_teacher WHERE t_id = #{id};
select>
mapper>
package net.hw.mybatis.mapper;
import net.hw.mybatis.bean.Clazz;
import java.util.List;
/**
* 功能:班级映射器接口
* 作者:华卫
* 日期:2021年03月04日
*/
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.Student;
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月04日
*/
public class TestClazzMapper {
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);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testFindById() {
int id = 1;
Clazz clazz = clazzMapper.findById(id);
System.out.println(clazz);
System.out.println("班级编号[" + id + "]的学生:");
List<Student> students = clazz.getStudents();
students.forEach(student -> System.out.println(student));
}
@Test
public void testFindAll() {
List<Clazz> clazzes = clazzMapper.findAll();
clazzes.forEach(clazz -> System.out.println(clazz));
}
@After
public void destroy() {
// 关闭SQL会话
sqlSession.close();
}
}