需求:根据classId查询对应的班级信息,包括学生,老师
Student实体类
public class Student { private int id; private String name; //... }
Classes 实体类
public class Classes { private int id; private String name; private Teacher teacher; private List<Student> students; //... }
Student实体类
public class Student { private int id; private String name; //... }
方式一: 嵌套结果: 使用嵌套结果映射来处理重复的联合结果的子集
select * from class c, teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id}
<select id="getClass" parameterType="int" resultMap="ClassResultMap3"> select * from class c, teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id} </select> <resultMap type="com.mybatis.test04.Classes" id="ClassResultMap3"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" column="teacher_id" javaType="com.mybatis.test04.Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> </association> <!-- ofType指定students集合中的对象类型 --> <collection property="students" ofType="com.mybatis.test04.Student"> <id property="id" column="s_id"/> <result property="name" column="s_name"/> </collection> </resultMap>
方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型
SELECT * FROM class WHERE c_id=1;
SELECT * FROM teacher WHERE t_id=1 //1 是上一个查询得到的teacher_id的值
SELECT * FROM student WHERE class_id=1 //1是第一个查询得到的c_id字段的值
<select id="getClass2" resultMap="getClass2Map"> select * from class where c_id=#{id} </select> <select id="getTeacher" resultType="com.mybatis.test04.Teacher"> select t_id id, t_name name from teacher where t_id=#{id} </select> <select id="getStudent" resultType="com.mybatis.test04.Student"> select s_id id, s_name name from student where class_id=#{id} </select> <resultMap type="com.mybatis.test04.Classes" id="getClass2Map"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" column="teacher_id" select="getTeacher"></association> <collection property="students" column="c_id" select="getStudent"></collection> </resultMap>
collection 一对多关联
ofType 指定集合中元素对象的类型
附:建表语句
DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` ( `t_id` int(11) NOT NULL auto_increment, `t_name` varchar(20) default NULL, PRIMARY KEY (`t_id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; INSERT INTO `teacher` VALUES (‘1‘, ‘孔子‘); INSERT INTO `teacher` VALUES (‘2‘, ‘孟子‘); DROP TABLE IF EXISTS `class`; CREATE TABLE `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 `teacher` (`t_id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; INSERT INTO `class` VALUES (‘1‘, ‘1121班‘, ‘1‘); INSERT INTO `class` VALUES (‘2‘, ‘1122班‘, ‘2‘); DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `s_id` int(11) NOT NULL auto_increment, `s_name` varchar(20) default NULL, `class_id` int(11) default NULL, PRIMARY KEY (`s_id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; INSERT INTO `student` VALUES (‘1‘, ‘张三‘, ‘1‘); INSERT INTO `student` VALUES (‘2‘, ‘李四‘, ‘1‘); INSERT INTO `student` VALUES (‘3‘, ‘王五‘, ‘1‘); INSERT INTO `student` VALUES (‘4‘, ‘Tom‘, ‘2‘); INSERT INTO `student` VALUES (‘5‘, ‘Jack‘, ‘2‘); INSERT INTO `student` VALUES (‘6‘, ‘Robert‘, ‘2‘);
时间: 2024-10-01 18:25:25