通过mybatis实现表的关联映射,通过一下几步
一、使用ddl和dml语言创建MySQL中的学生表和老师表
1、学生表
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Sname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`t_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '张三', '男', 18, 1);
INSERT INTO `student` VALUES (2, '李四', '女', 18, 1);
INSERT INTO `student` VALUES (3, '王五', '男', 18, 1);
INSERT INTO `student` VALUES (4, '小白', '女', 18, 1);
INSERT INTO `student` VALUES (5, '小黑', '男', 18, 1);
INSERT INTO `student` VALUES (6, '小红', '女', 20, 2);
INSERT INTO `student` VALUES (7, '小李', '男', 20, 2);
INSERT INTO `student` VALUES (8, '小张', '女', 20, 2);
INSERT INTO `student` VALUES (9, '小赵', '男', 20, 2);
INSERT INTO `student` VALUES (10, '小王', '女', 20, 2);
SET FOREIGN_KEY_CHECKS = 1;
2、老师表
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Tname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES (1, '张老师');
INSERT INTO `teacher` VALUES (2, '李老师');
SET FOREIGN_KEY_CHECKS = 1;
二、映射关系 (一对一,多对一)
1、连表查询
① :建实体类
//学生类
public class Student {
private Integer id;
private String Sname;
private String sex;
private Integer age;
private Integer t_id;
//这个是重点
private Teacher teacher;
}
//老师类
pubic class Teacher{
private Integer id;
private String Tname;
}
同时设置这两个类的访问器和构造器,以及toString方法。完成以上的构建操作,我们就实现万丈高楼的基础工作了。
基于maven实现mybatis的简单操作(增删改查)_三思守心的博客-CSDN博客在这篇博客中已经在IDE中建立了相应的接口、类和xml文件
我们只需要在相应的包中,新建StudentDao,TeacherDao。和StudentDao.xml,TeacherDao.xml
②:查询语句
在StudentDao.xml里输入相应语句
查询每个学生的老师,因为老师不是学生表中的信息,所以我们需要,在Student类中声明,老师变量,上面的代码我已经设置了。
在下面的代码中,我们需要有<resultMap>映射,因为teacher对象中的信息是引用的,所以对应teacher的信息要用<association>
<!-- 按照结果嵌套处理-->
<select id="getStudent1" resultMap="StudentTeacher1">
SELECT student.id,student.Sname,teacher.Tname FROM student LEFT JOIN teacher on student.t_id = teacher.id
</select>
<resultMap id="StudentTeacher1" type="com.qcby.entity.Student">
<result property="id" column="id"/>
<result property="Sname" column="Sname"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<result property="t_id" column="t_id"/>
<!-- 复杂的属性我们需要单独去处理 对象:association 集合:collection -->
<!-- property="teacher" student类当中的关联字段 -->
<!-- javaType="com.javen.model.Teacher" 为复杂属性设置类类型-->
<association property="teacher" javaType="com.qcby.entity.Teacher">
<result property="id" column="id"/>
<result property="Tname" column="Tname"/>
</association>
</resultMap>
2、分步查询
①设置实体类
这里的实体类是不需要动的
②查询语句
这里的查询语句是要分成两步来进行的,在StudentDao.xml 中只需要执行select * from student;从student表中找到信息,在TeacherDap.xml 中我们执行select * from teacher where id = #{t_id} 这里自己分析一下为什么可以这样?
//StudentDao.xml
<select id = "getStudent" resultMap="StudentTeacher">
select * from student;
</select>
<!--结果映射集-->
<resultMap id="StudentTeacher" type="com.qcby.entity.Student">
<result property="id" column="id"/>
<result property="Sname" column="Sname"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<result property="t_id" column="t_id"/>
<!-- select="getTeacher" :调用下一个查询语句 -->
<!-- column="t_id" 两个表的关联字段-->
<!--复杂属性需要特殊处理,对象 associate-->
<!--property:实体类当中的字段名称-->
<!--javaType:复杂对象的类型-->
<association property="teacher" column="t_id" javaType="com.qcby.entity.Teacher" select="getTeacher"/>
</resultMap>
//TeacherDao.xml
<select id="getTeacher" resultType="com.qcby.entity.Teacher">
select * from teacher where id = #{t_id}; <!-- #{id}; 可以写任何东西,因为会自动匹配 t_id -->
</select>
TeacherDao.java 接口文件里定义这个方法
public interface TeacherDao {
Teacher getTeacher(Integer t_id);
}
三、一对多
查询老师有多少学生
在这里我们就要注释掉Student 实体类中的teacher对象,因为我们要在Teacher类中引用Student类的学生信息,不注释掉的话,就会形成死循环,会报错
public class Teacher {
private Integer id;
private String Tname;
//这个一定要有
private List<Student> students;
}
同之前查询学生的老师的步骤是差不多的,不同的是,学生的老师是一个学生对应一个老师,而老师的学生是一个老师对应多个学生,是一个集合
所以在下面的代码部分我们使用的不在是<association>标签,而是使用的是<collection>我们定义的student的对象是链表形式的,所以我们用ofType标签是获取泛型中的信息
<!--按照结果进行查询-->
<select id="getTeacher" resultMap="TeacherStudent">
SELECT teacher.id,teacher.Tname,student.Sname FROM teacher
LEFT JOIN student on student.t_id = teacher.id
</select>
<resultMap id="TeacherStudent" type="com.qcby.entity.Teacher">
<result property="id" column="id"/>
<result property="Tname" column="Tname"/>
<!-- 复杂的属性我么需要单独去处理
对象:association
集合:collection
在集合中的泛型信息,我们使用ofType获取
-->
<collection property="students" ofType="com.qcby.entity.Student">
<result property="Sname" column="Sname"/>
</collection>
</resultMap>
同上面的分步查询,这里也是有分步查询的。只需要注意我们应该有<collection>来接收学生的信息就好,其他的和查询学生的老师步骤是一样的。