1、一對一關聯java
1.一、提出需求sql
根據班級id查詢班級信息(帶老師的信息)apache
1.二、建立表和數據session
建立一張教師表和班級表,這裏咱們假設一個老師只負責教一個班,那麼老師和班級之間的關係就是一種一對一的關係。mybatis
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);
表之間的關係以下:app
1.三、定義實體類dom
一、Teacher類,Teacher類是teacher表對應的實體類。ide
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 }
二、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 }
1.四、定義sql映射文件classMapper.xml測試
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <!-- 爲這個mapper指定一個惟一的namespace,namespace的值習慣上設置成包名+sql映射文件名,這樣就可以保證namespace的值是惟一的 4 例如namespace="me.gacl.mapping.classMapper"就是me.gacl.mapping(包名)+classMapper(classMapper.xml文件去除後綴) 5 --> 6 <mapper namespace="me.gacl.mapping.classMapper"> 7 8 <!-- 9 根據班級id查詢班級信息(帶老師的信息) 10 ##1. 聯表查詢 11 SELECT * FROM class c,teacher t WHERE c.teacher_id=t.t_id AND c.c_id=1; 12 13 ##2. 執行兩次查詢 14 SELECT * FROM class WHERE c_id=1; //teacher_id=1 15 SELECT * FROM teacher WHERE t_id=1;//使用上面獲得的teacher_id 16 --> 17 18 <!-- 19 方式一:嵌套結果:使用嵌套結果映射來處理重複的聯合結果的子集 20 封裝聯表查詢的數據(去除重複的數據) 21 select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=1 22 --> 23 <select id="getClass" parameterType="int" resultMap="ClassResultMap"> 24 select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=#{id} 25 </select> 26 <!-- 使用resultMap映射實體類和字段之間的一一對應關係 --> 27 <resultMap type="me.gacl.domain.Classes" id="ClassResultMap"> 28 <id property="id" column="c_id"/> 29 <result property="name" column="c_name"/> 30 <association property="teacher" javaType="me.gacl.domain.Teacher"> 31 <id property="id" column="t_id"/> 32 <result property="name" column="t_name"/> 33 </association> 34 </resultMap> 35 36 <!-- 37 方式二:嵌套查詢:經過執行另一個SQL映射語句來返回預期的複雜類型 38 SELECT * FROM class WHERE c_id=1; 39 SELECT * FROM teacher WHERE t_id=1 //1 是上一個查詢獲得的teacher_id的值 40 --> 41 <select id="getClass2" parameterType="int" resultMap="ClassResultMap2"> 42 select * from class where c_id=#{id} 43 </select> 44 <!-- 使用resultMap映射實體類和字段之間的一一對應關係 --> 45 <resultMap type="me.gacl.domain.Classes" id="ClassResultMap2"> 46 <id property="id" column="c_id"/> 47 <result property="name" column="c_name"/> 48 <association property="teacher" column="teacher_id" select="getTeacher"/> 49 </resultMap> 50 51 <select id="getTeacher" parameterType="int" resultType="me.gacl.domain.Teacher"> 52 SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id} 53 </select> 54 55 </mapper>
在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> 1.五、編寫單元測試代碼
1 package me.gacl.test; 2 3 import me.gacl.domain.Classes; 4 import me.gacl.util.MyBatisUtil; 5 import org.apache.ibatis.session.SqlSession; 6 import org.junit.Test; 7 8 public class Test3 { 9 10 @Test 11 public void testGetClass(){ 12 SqlSession sqlSession = MyBatisUtil.getSqlSession(); 13 /** 14 * 映射sql的標識字符串, 15 * me.gacl.mapping.classMapper是classMapper.xml文件中mapper標籤的namespace屬性的值, 16 * getClass是select標籤的id屬性值,經過select標籤的id屬性值就能夠找到要執行的SQL 17 */ 18 String statement = "me.gacl.mapping.classMapper.getClass";//映射sql的標識字符串 19 //執行查詢操做,將查詢結果自動封裝成Classes對象返回 20 Classes clazz = sqlSession.selectOne(statement,1);//查詢class表中id爲1的記錄 21 //使用SqlSession執行完SQL以後須要關閉SqlSession 22 sqlSession.close(); 23 System.out.println(clazz);//打印結果:Classes [id=1, name=class_a, teacher=Teacher [id=1, name=teacher1]] 24 } 25 26 @Test 27 public void testGetClass2(){ 28 SqlSession sqlSession = MyBatisUtil.getSqlSession(); 29 /** 30 * 映射sql的標識字符串, 31 * me.gacl.mapping.classMapper是classMapper.xml文件中mapper標籤的namespace屬性的值, 32 * getClass2是select標籤的id屬性值,經過select標籤的id屬性值就能夠找到要執行的SQL 33 */ 34 String statement = "me.gacl.mapping.classMapper.getClass2";//映射sql的標識字符串 35 //執行查詢操做,將查詢結果自動封裝成Classes對象返回 36 Classes clazz = sqlSession.selectOne(statement,1);//查詢class表中id爲1的記錄 37 //使用SqlSession執行完SQL以後須要關閉SqlSession 38 sqlSession.close(); 39 System.out.println(clazz);//打印結果:Classes [id=1, name=class_a, teacher=Teacher [id=1, name=teacher1]] 40 } 41 }
1.六、MyBatis一對一關聯查詢總結
MyBatis中使用association標籤來解決一對一的關聯查詢,association標籤可用的屬性以下:
property:對象屬性的名稱 javaType:對象屬性的類型 column:所對應的外鍵字段名稱 select:使用另外一個查詢封裝的結果 2、一對多關聯
2.一、提出需求
根據classId查詢對應的班級信息,包括學生,老師
2.二、建立表和數據
在上面的一對一關聯查詢演示中,咱們已經建立了班級表和教師表,所以這裏再建立一張學生表
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);
2.三、定義實體類
一、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 }
二、修改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 }
2.四、修改sql映射文件classMapper.xml
添加以下的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>
2.五、編寫單元測試代碼
1 package me.gacl.test; 2 3 import me.gacl.domain.Classes; 4 import me.gacl.util.MyBatisUtil; 5 import org.apache.ibatis.session.SqlSession; 6 import org.junit.Test; 7 8 public class Test4 { 9 10 @Test 11 public void testGetClass3(){ 12 SqlSession sqlSession = MyBatisUtil.getSqlSession(); 13 /** 14 * 映射sql的標識字符串, 15 * me.gacl.mapping.classMapper是classMapper.xml文件中mapper標籤的namespace屬性的值, 16 * getClass3是select標籤的id屬性值,經過select標籤的id屬性值就能夠找到要執行的SQL 17 */ 18 String statement = "me.gacl.mapping.classMapper.getClass3";//映射sql的標識字符串 19 //執行查詢操做,將查詢結果自動封裝成Classes對象返回 20 Classes clazz = sqlSession.selectOne(statement,1);//查詢class表中id爲1的記錄 21 //使用SqlSession執行完SQL以後須要關閉SqlSession 22 sqlSession.close(); 23 //打印結果:Classes [id=1, name=class_a, teacher=Teacher [id=1, name=teacher1], students=[Student [id=1, name=student_A], Student [id=2, name=student_B], Student [id=3, name=student_C]]] 24 System.out.println(clazz); 25 } 26 27 @Test 28 public void testGetClass4(){ 29 SqlSession sqlSession = MyBatisUtil.getSqlSession(); 30 /** 31 * 映射sql的標識字符串, 32 * me.gacl.mapping.classMapper是classMapper.xml文件中mapper標籤的namespace屬性的值, 33 * getClass4是select標籤的id屬性值,經過select標籤的id屬性值就能夠找到要執行的SQL 34 */ 35 String statement = "me.gacl.mapping.classMapper.getClass4";//映射sql的標識字符串 36 //執行查詢操做,將查詢結果自動封裝成Classes對象返回 37 Classes clazz = sqlSession.selectOne(statement,1);//查詢class表中id爲1的記錄 38 //使用SqlSession執行完SQL以後須要關閉SqlSession 39 sqlSession.close(); 40 //打印結果:Classes [id=1, name=class_a, teacher=Teacher [id=1, name=teacher1], students=[Student [id=1, name=student_A], Student [id=2, name=student_B], Student [id=3, name=student_C]]] 41 System.out.println(clazz); 42 } 43 }
2.六、MyBatis一對多關聯查詢總結
MyBatis中使用collection標籤來解決一對多的關聯查詢,ofType屬性指定集合中元素的對象類型。