MyBatis進修教程(五)-完成聯系關系表查詢辦法詳解。本站提示廣大學習愛好者:(MyBatis進修教程(五)-完成聯系關系表查詢辦法詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是MyBatis進修教程(五)-完成聯系關系表查詢辦法詳解正文
1、一對一聯系關系
1.1、提出需求
依據班級id查詢班級信息(帶先生的信息)
1.2、創立表和數據
創立一張教員表和班級表,這裡我們假定一個先生只擔任教一個班,那末先生和班級之間的關系就是一種一對一的關系。
CREATE TABLE teacher( t_id INT PRIMARY KEY AUTO_INCREMENT, t_name VARCHAR() ); CREATE TABLE class( c_id INT PRIMARY KEY AUTO_INCREMENT, c_name VARCHAR(), teacher_id INT ); ALTER TABLE class ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher(t_id); INSERT INTO teacher(t_name) VALUES('teacher'); INSERT INTO teacher(t_name) VALUES('teacher'); INSERT INTO class(c_name, teacher_id) VALUES('class_a', ); INSERT INTO class(c_name, teacher_id) VALUES('class_b', 2);
表之間的關系以下:
1.3、界說實體類
1、Teacher類,Teacher類是teacher表對應的實體類。
package me.gacl.domain; /** * @author gacl * 界說teacher表對應的實體類 */ public class Teacher { //界說實體類的屬性,與teacher表中的字段對應 private int id; //id===>t_id private String name; //name===>t_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 + "]"; } }
2、Classes類,Classes類是class表對應的實體類
package me.gacl.domain; /** * @author gacl * 界說class表對應的實體類 */ public class Classes { //界說實體類的屬性,與class表中的字段對應 private int id; //id===>c_id private String name; //name===>c_name /** * class表中有一個teacher_id字段,所以在Classes類中界說一個teacher屬性, * 用於保護teacher和class之間的一對一關系,經由過程這個teacher屬性便可以曉得這個班級是由哪一個先生擔任的 */ private Teacher teacher; 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; } @Override public String toString() { return "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher+ "]"; } }
1.4、界說sql映照文件classMapper.xml
<?xml version="." encoding="UTF-" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper .//EN" "http://mybatis.org/dtd/mybatis--mapper.dtd"> <!-- 為這個mapper指定一個獨一的namespace,namespace的值習氣上設置成包名+sql映照文件名,如許就可以夠包管namespace的值是獨一的 例如namespace="me.gacl.mapping.classMapper"就是me.gacl.mapping(包名)+classMapper(classMapper.xml文件去除後綴) --> <mapper namespace="me.gacl.mapping.classMapper"> <!-- 依據班級id查詢班級信息(帶先生的信息) ##. 聯表查詢 SELECT * FROM class c,teacher t WHERE c.teacher_id=t.t_id AND c.c_id=; ##. 履行兩次查詢 SELECT * FROM class WHERE c_id=; //teacher_id= SELECT * FROM teacher WHERE t_id=;//應用下面獲得的teacher_id --> <!-- 方法一:嵌套成果:應用嵌套成果映照來處置反復的結合成果的子集 封裝聯表查詢的數據(去除反復的數據) select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id= --> <select id="getClass" parameterType="int" resultMap="ClassResultMap"> select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=#{id} </select> <!-- 應用resultMap映照實體類和字段之間的逐個對應關系 --> <resultMap type="me.gacl.domain.Classes" id="ClassResultMap"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" javaType="me.gacl.domain.Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> </association> </resultMap> <!-- 方法二:嵌套查詢:經由過程履行別的一個SQL映照語句來前往預期的龐雜類型 SELECT * FROM class WHERE c_id=; SELECT * FROM teacher WHERE t_id= // 是上一個查詢獲得的teacher_id的值 --> <select id="getClass" parameterType="int" resultMap="ClassResultMap"> select * from class where c_id=#{id} </select> <!-- 應用resultMap映照實體類和字段之間的逐個對應關系 --> <resultMap type="me.gacl.domain.Classes" id="ClassResultMap"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" column="teacher_id" select="getTeacher"/> </resultMap> <select id="getTeacher" parameterType="int" resultType="me.gacl.domain.Teacher"> SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id} </select> </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.5、編寫單位測試代碼
package me.gacl.test; import me.gacl.domain.Classes; import me.gacl.util.MyBatisUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; public class Test { @Test public void testGetClass(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(); /** * 映照sql的標識字符串, * me.gacl.mapping.classMapper是classMapper.xml文件中mapper標簽的namespace屬性的值, * getClass是select標簽的id屬性值,經由過程select標簽的id屬性值便可以找到要履行的SQL */ String statement = "me.gacl.mapping.classMapper.getClass";//映照sql的標識字符串 //履行查詢操作,將查詢成果主動封裝成Classes對象前往 Classes clazz = sqlSession.selectOne(statement,);//查詢class表中id為的記載 //應用SqlSession履行完SQL以後須要封閉SqlSession sqlSession.close(); System.out.println(clazz);//打印成果:Classes [id=, name=class_a, teacher=Teacher [id=, name=teacher]] } @Test public void testGetClass(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(); /** * 映照sql的標識字符串, * me.gacl.mapping.classMapper是classMapper.xml文件中mapper標簽的namespace屬性的值, * getClass是select標簽的id屬性值,經由過程select標簽的id屬性值便可以找到要履行的SQL */ String statement = "me.gacl.mapping.classMapper.getClass";//映照sql的標識字符串 //履行查詢操作,將查詢成果主動封裝成Classes對象前往 Classes clazz = sqlSession.selectOne(statement,);//查詢class表中id為的記載 //應用SqlSession履行完SQL以後須要封閉SqlSession sqlSession.close(); System.out.println(clazz);//打印成果:Classes [id=, name=class_a, teacher=Teacher [id=, name=teacher]] } }
1.6、MyBatis一對一聯系關系查詢總結
MyBatis中應用association標簽來處理一對一的聯系關系查詢,association標簽可用的屬性以下:
•property:對象屬性的稱號
•javaType:對象屬性的類型
•column:所對應的外鍵字段稱號
•select:應用另外一個查詢封裝的成果
2、一對多聯系關系
2.1、提出需求
依據classId查詢對應的班級信息,包含先生,先生
2.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.3、界說實體類
1、Student類
package me.gacl.domain; /** * @author gacl * 界說student表所對應的實體類 */ public class Student { //界說屬性,和student表中的字段對應 private int id; //id===>s_id private String name; //name===>s_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 "Student [id=" + id + ", name=" + name + "]"; } }
2、修正Classes類,添加一個List<Student> students屬性,應用一個List<Student>聚集屬性表現班級具有的先生,以下:
package me.gacl.domain; import java.util.List; /** * @author gacl * 界說class表對應的實體類 */ public class Classes { //界說實體類的屬性,與class表中的字段對應 private int id; //id===>c_id private String name; //name===>c_name /** * class表中有一個teacher_id字段,所以在Classes類中界說一個teacher屬性, * 用於保護teacher和class之間的一對一關系,經由過程這個teacher屬性便可以曉得這個班級是由哪一個先生擔任的 */ private Teacher teacher; //應用一個List<Student>聚集屬性表現班級具有的先生 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 "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher + ", students=" + students + "]"; } }
2.4、修正sql映照文件classMapper.xml
添加以下的SQL映照信息
<!-- 依據classId查詢對應的班級信息,包含先生,先生 --> <!-- 方法一: 嵌套成果: 應用嵌套成果映照來處置反復的結合成果的子集 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= --> <select id="getClass" parameterType="int" resultMap="ClassResultMap"> 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="me.gacl.domain.Classes" id="ClassResultMap"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" column="teacher_id" javaType="me.gacl.domain.Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> </association> <!-- ofType指定students聚集中的對象類型 --> <collection property="students" ofType="me.gacl.domain.Student"> <id property="id" column="s_id"/> <result property="name" column="s_name"/> </collection> </resultMap> <!-- 方法二:嵌套查詢:經由過程履行別的一個SQL映照語句來前往預期的龐雜類型 SELECT * FROM class WHERE c_id=; SELECT * FROM teacher WHERE t_id= // 是上一個查詢獲得的teacher_id的值 SELECT * FROM student WHERE class_id= //是第一個查詢獲得的c_id字段的值 --> <select id="getClass" parameterType="int" resultMap="ClassResultMap"> select * from class where c_id=#{id} </select> <resultMap type="me.gacl.domain.Classes" id="ClassResultMap"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" column="teacher_id" javaType="me.gacl.domain.Teacher" select="getTeacher"></association> <collection property="students" ofType="me.gacl.domain.Student" column="c_id" select="getStudent"></collection> </resultMap> <select id="getTeacher" parameterType="int" resultType="me.gacl.domain.Teacher"> SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id} </select> <select id="getStudent" parameterType="int" resultType="me.gacl.domain.Student"> SELECT s_id id, s_name name FROM student WHERE class_id=#{id} </select>
2.5、編寫單位測試代碼
package me.gacl.test; import me.gacl.domain.Classes; import me.gacl.util.MyBatisUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; public class Test { @Test public void testGetClass(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(); /** * 映照sql的標識字符串, * me.gacl.mapping.classMapper是classMapper.xml文件中mapper標簽的namespace屬性的值, * getClass是select標簽的id屬性值,經由過程select標簽的id屬性值便可以找到要履行的SQL */ String statement = "me.gacl.mapping.classMapper.getClass";//映照sql的標識字符串 //履行查詢操作,將查詢成果主動封裝成Classes對象前往 Classes clazz = sqlSession.selectOne(statement,);//查詢class表中id為的記載 //應用SqlSession履行完SQL以後須要封閉SqlSession sqlSession.close(); //打印成果:Classes [id=, name=class_a, teacher=Teacher [id=, name=teacher], students=[Student [id=, name=student_A], Student [id=, name=student_B], Student [id=, name=student_C]]] System.out.println(clazz); } @Test public void testGetClass(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(); /** * 映照sql的標識字符串, * me.gacl.mapping.classMapper是classMapper.xml文件中mapper標簽的namespace屬性的值, * getClass是select標簽的id屬性值,經由過程select標簽的id屬性值便可以找到要履行的SQL */ String statement = "me.gacl.mapping.classMapper.getClass";//映照sql的標識字符串 //履行查詢操作,將查詢成果主動封裝成Classes對象前往 Classes clazz = sqlSession.selectOne(statement,);//查詢class表中id為的記載 //應用SqlSession履行完SQL以後須要封閉SqlSession sqlSession.close(); //打印成果:Classes [id=, name=class_a, teacher=Teacher [id=, name=teacher], students=[Student [id=, name=student_A], Student [id=, name=student_B], Student [id=, name=student_C]]] System.out.println(clazz); } }
2.6、MyBatis一對多聯系關系查詢總結
MyBatis中應用collection標簽來處理一對多的聯系關系查詢,ofType屬性指定聚集中元素的對象類型。
關於MyBatis進修教程(五)-完成聯系關系表查詢的相干內容就給年夜家引見這麼多,願望對年夜家有所贊助!