根据班级id查询班级信息(带老师的信息)
创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关系。
1 CREATE TABLE teacher( 2 t_id INT PRIMARY KEY AUTO_INCREMENT, 3 t_name VARCHAR(20) 4 ); 5 CREATE TABLE class( 6 c_id INT PRIMARY KEY AUTO_INCREMENT, 7 c_name VARCHAR(20), 8 teacher_id INT 9 ); 10 ALTER TABLE class ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher(t_id); 11 12 INSERT INTO teacher(t_name) VALUES('teacher1'); 13 INSERT INTO teacher(t_name) VALUES('teacher2'); 14 15 INSERT INTO class(c_name, teacher_id) VALUES('class_a', 1); 16 INSERT INTO class(c_name, teacher_id) VALUES('class_b', 2);表之间的关系如下:
1、Teacher类,Teacher类是teacher表对应的实体类。
1 package me.gacl.domain; 2 3 /** 4 * @author gacl 5 * 定义teacher表对应的实体类 6 */ 7 public class Teacher { 8 9 //定义实体类的属性,与teacher表中的字段对应 10 private int id; //id===>t_id 11 private String name; //name===>t_name 12 13 public int getId() { 14 return id; 15 } 16 17 public void setId(int id) { 18 this.id = id; 19 } 20 21 public String getName() { 22 return name; 23 } 24 25 public void setName(String name) { 26 this.name = name; 27 } 28 29 @Override 30 public String toString() { 31 return "Teacher [id=" + id + ", name=" + name + "]"; 32 } 33 }2、Classes类,Classes类是class表对应的实体类
1 package me.gacl.domain; 2 3 /** 4 * @author gacl 5 * 定义class表对应的实体类 6 */ 7 public class Classes { 8 9 //定义实体类的属性,与class表中的字段对应 10 private int id; //id===>c_id 11 private String name; //name===>c_name 12 13 /** 14 * class表中有一个teacher_id字段,所以在Classes类中定义一个teacher属性, 15 * 用于维护teacher和class之间的一对一关系,通过这个teacher属性就可以知道这个班级是由哪个老师负责的 16 */ 17 private Teacher teacher; 18 19 public int getId() { 20 return id; 21 } 22 23 public void setId(int id) { 24 this.id = id; 25 } 26 27 public String getName() { 28 return name; 29 } 30 31 public void setName(String name) { 32 this.name = name; 33 } 34 35 public Teacher getTeacher() { 36 return teacher; 37 } 38 39 public void setTeacher(Teacher teacher) { 40 this.teacher = teacher; 41 } 42 43 @Override 44 public String toString() { 45 return "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher+ "]"; 46 } 47 }在conf.xml文件中注册classMapper.xml
<mappers> <!-- 注册classMapper.xml文件, classMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/classMapper.xml--> <mapper resource="me/gacl/mapping/classMapper.xml"/> </mappers>MyBatis中使用association标签来解决一对一的关联查询,association标签可用的属性如下:
property:对象属性的名称javaType:对象属性的类型column:所对应的外键字段名称select:使用另一个查询封装的结果根据classId查询对应的班级信息,包括学生,老师
在上面的一对一关联查询演示中,我们已经创建了班级表和教师表,因此这里再创建一张学生表
CREATE TABLE student( s_id INT PRIMARY KEY AUTO_INCREMENT, s_name VARCHAR(20), class_id INT ); INSERT INTO student(s_name, class_id) VALUES('student_A', 1); INSERT INTO student(s_name, class_id) VALUES('student_B', 1); INSERT INTO student(s_name, class_id) VALUES('student_C', 1); INSERT INTO student(s_name, class_id) VALUES('student_D', 2); INSERT INTO student(s_name, class_id) VALUES('student_E', 2); INSERT INTO student(s_name, class_id) VALUES('student_F', 2);
1、Student类
1 package me.gacl.domain; 2 3 /** 4 * @author gacl 5 * 定义student表所对应的实体类 6 */ 7 public class Student { 8 9 //定义属性,和student表中的字段对应 10 private int id; //id===>s_id 11 private String name; //name===>s_name 12 13 public int getId() { 14 return id; 15 } 16 17 public void setId(int id) { 18 this.id = id; 19 } 20 21 public String getName() { 22 return name; 23 } 24 25 public void setName(String name) { 26 this.name = name; 27 } 28 29 @Override 30 public String toString() { 31 return "Student [id=" + id + ", name=" + name + "]"; 32 } 33 }2、修改Classes类,添加一个List<Student> students属性,使用一个List<Student>集合属性表示班级拥有的学生,如下:
1 package me.gacl.domain; 2 3 import java.util.List; 4 5 /** 6 * @author gacl 7 * 定义class表对应的实体类 8 */ 9 public class Classes { 10 11 //定义实体类的属性,与class表中的字段对应 12 private int id; //id===>c_id 13 private String name; //name===>c_name 14 15 /** 16 * class表中有一个teacher_id字段,所以在Classes类中定义一个teacher属性, 17 * 用于维护teacher和class之间的一对一关系,通过这个teacher属性就可以知道这个班级是由哪个老师负责的 18 */ 19 private Teacher teacher; 20 //使用一个List<Student>集合属性表示班级拥有的学生 21 private List<Student> students; 22 23 public int getId() { 24 return id; 25 } 26 27 public void setId(int id) { 28 this.id = id; 29 } 30 31 public String getName() { 32 return name; 33 } 34 35 public void setName(String name) { 36 this.name = name; 37 } 38 39 public Teacher getTeacher() { 40 return teacher; 41 } 42 43 public void setTeacher(Teacher teacher) { 44 this.teacher = teacher; 45 } 46 47 public List<Student> getStudents() { 48 return students; 49 } 50 51 public void setStudents(List<Student> students) { 52 this.students = students; 53 } 54 55 @Override 56 public String toString() { 57 return "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher 58 + ", students=" + students + "]"; 59 } 60 }添加如下的SQL映射信息
1 <!-- 2 根据classId查询对应的班级信息,包括学生,老师 3 --> 4 <!-- 5 方式一: 嵌套结果: 使用嵌套结果映射来处理重复的联合结果的子集 6 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=1 7 --> 8 <select id="getClass3" parameterType="int" resultMap="ClassResultMap3"> 9 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} 10 </select> 11 <resultMap type="me.gacl.domain.Classes" id="ClassResultMap3"> 12 <id property="id" column="c_id"/> 13 <result property="name" column="c_name"/> 14 <association property="teacher" column="teacher_id" javaType="me.gacl.domain.Teacher"> 15 <id property="id" column="t_id"/> 16 <result property="name" column="t_name"/> 17 </association> 18 <!-- ofType指定students集合中的对象类型 --> 19 <collection property="students" ofType="me.gacl.domain.Student"> 20 <id property="id" column="s_id"/> 21 <result property="name" column="s_name"/> 22 </collection> 23 </resultMap> 24 25 <!-- 26 方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型 27 SELECT * FROM class WHERE c_id=1; 28 SELECT * FROM teacher WHERE t_id=1 //1 是上一个查询得到的teacher_id的值 29 SELECT * FROM student WHERE class_id=1 //1是第一个查询得到的c_id字段的值 30 --> 31 <select id="getClass4" parameterType="int" resultMap="ClassResultMap4"> 32 select * from class where c_id=#{id} 33 </select> 34 <resultMap type="me.gacl.domain.Classes" id="ClassResultMap4"> 35 <id property="id" column="c_id"/> 36 <result property="name" column="c_name"/> 37 <association property="teacher" column="teacher_id" javaType="me.gacl.domain.Teacher" select="getTeacher2"></association> 38 <collection property="students" ofType="me.gacl.domain.Student" column="c_id" select="getStudent"></collection> 39 </resultMap> 40 41 <select id="getTeacher2" parameterType="int" resultType="me.gacl.domain.Teacher"> 42 SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id} 43 </select> 44 45 <select id="getStudent" parameterType="int" resultType="me.gacl.domain.Student"> 46 SELECT s_id id, s_name name FROM student WHERE class_id=#{id} 47 </select>MyBatis中使用collection标签来解决一对多的关联查询,ofType属性指定集合中元素的对象类型。
转载于:https://www.cnblogs.com/kms1989/p/5534081.html
相关资源:JAVA上百实例源码以及开源项目