一對多+多對一關系的查詢


比如:

一對多: 一個老師對應多個學生

多對一: 多個學生對應一個老師

 

一、建表+實體類

創建一個學生表和一個老師表

通過學生的tid與老師的id形成聯系

SQLyog的建表代碼

CREATE TABLE `teacher`( `id` INT(10) NOT NULL, `name` VARCHAR(30) DEFAULT NULL, PRIMARY KEY(`id`) )ENGINE=INNODB DEFAULT CHARSET=utf8 ​ INSERT INTO teacher(`id`,`name`) VALUES(1,'哈哈'); ​ CREATE TABLE `student`( `id` INT(10) NOT NULL, `name` VARCHAR(30) DEFAULT NULL, `tid` INT(10) DEFAULT NULL, PRIMARY KEY (`id`), KEY `fktid` (`tid`), CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`) )ENGINE=INNODB DEFAULT CHARSET=utf8 ​ INSERT INTO student (`id`,`name`,`tid`) VALUES ('1','學生1','1'); INSERT INTO student (`id`,`name`,`tid`) VALUES ('2','學生2','1'); INSERT INTO student (`id`,`name`,`tid`) VALUES ('3','學生3','1'); INSERT INTO student (`id`,`name`,`tid`) VALUES ('4','學生4','1'); INSERT INTO student (`id`,`name`,`tid`) VALUES ('5','學生5','1');

 

pojo(使用了Lombok)

student:

package com.zy.pojo; ​ import lombok.Data; ​ @Data public class Student { ​ private int id; private String name; ​ private int tid; ​ private Teacher teacher; ​ }
 

teacher

 package com.zy.pojo; ​ import lombok.Data; ​ import java.util.List; ​ @Data public class Teacher { ​ private int id; private String name; ​ private List<Student> students; ​ }

 

 

二、多對一

多個學生對應一個老師

 

StudentMapper

package com.zy.mapper; ​ import com.zy.pojo.Student; ​ import java.util.List; ​ public interface StudentMapper { ​ List<Student> getStudentList(); ​ List<Student> getStudentList2(); ​ List<Student> getStudentList3(); ​ }
 

StudentMapper.xml(關鍵點)

 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.zy.mapper.StudentMapper">
     <select id="getStudentList" resultType="student"> select * from student </select> ​ ​ <select id="getStudentList2" resultMap="studentMap2"> select * from student </select>
     <resultMap id="studentMap2" type="student">
         <result property="id" column="id"/>
         <result property="name" column="name"/>
         <result property="tid" column="tid"/>
         <association property="teacher" javaType="teacher" select="getTeacherById" column="tid"/>
     </resultMap>
     <select id="getTeacherById" resultType="teacher"> select * from teacher where id=#{tid} </select> ​ ​ <select id="getStudentList3" resultMap="studentMap3"> select s.id sid,s.name sname,s.tid stid,t.id tid,t.name tname from student s,teacher t where s.tid=t.id </select>
     <resultMap id="studentMap3" type="student">
         <result property="id" column="sid"/>
         <result property="name" column="sname"/>
         <result property="tid" column="stid"/>
         <association property="teacher" javaType="teacher">
             <result property="id" column="tid"/>
             <result property="name" column="tname"/>
         </association>
     </resultMap>
 </mapper>

 

測試

getStudentList:

 

 

 

getStudentList2:

 

getStudentList3:

 

getStudentList2與getStudentList3代碼不同,效果是相同的

 

三、一對多

一個老師對應多個學生

TeacherMapper

package com.zy.mapper; ​ import com.zy.pojo.Teacher; ​ import java.util.List; ​ public interface TeacherMapper { ​ List<Teacher> getTeacherList(); ​ List<Teacher> getTeacherList2(); ​ List<Teacher> getTeacherList3(); ​ }
 

TeacherMapper.xml(關鍵點)

<?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.zy.mapper.TeacherMapper"><select id="getTeacherList" resultType="teacher"> select * from teacher </select><select id="getTeacherList2" resultMap="teacherMap2"> select * from teacher </select>
     <resultMap id="teacherMap2" type="teacher">
         <result property="id" column="id"/>
         <result property="name" column="name"/>
         <collection property="students" javaType="ArrayList" ofType="student" select="getStudentById" column="id"/>
     </resultMap>
     <select id="getStudentById" resultType="student"> select * from student where tid=#{id} </select>
     
     <select id="getTeacherList3" resultMap="teacherMap3"> select t.id tid,t.name tname,s.id sid,s.name sname from teacher t,student s where t.id=s.tid </select>
     <resultMap id="teacherMap3" type="teacher">
         <result property="id" column="tid"/>
         <result property="name" column="tname"/>
         <collection property="students" javaType="ArrayList" ofType="Student">
             <result property="id" column="sid"/>
             <result property="name" column="sname"/>
             <result property="tid" column="tid"/>
         </collection></resultMap></mapper>

 

測試

getTeacherList:

 

getTeacherList2:

 

getTeacherList3:

 

getTeacherList2與getTeacherList3代碼不同,效果是相同的

 

無論是一對多還是多對一,重點都在寫xml中的代碼,合理利用resultMap可以寫出多個表的CRUD

 

比如 Mybatis官網 的例子:

<!-- 非常復雜的語句 -->
<select id="selectBlogDetails" resultMap="detailedBlogResultMap"> select B.id as blog_id, B.title as blog_title, B.author_id as blog_author_id, A.id as author_id, A.username as author_username, A.password as author_password, A.email as author_email, A.bio as author_bio, A.favourite_section as author_favourite_section, P.id as post_id, P.blog_id as post_blog_id, P.author_id as post_author_id, P.created_on as post_created_on, P.section as post_section, P.subject as post_subject, P.draft as draft, P.body as post_body, C.id as comment_id, C.post_id as comment_post_id, C.name as comment_name, C.comment as comment_text, T.id as tag_id, T.name as tag_name from Blog B left outer join Author A on B.author_id = A.id left outer join Post P on B.id = P.blog_id left outer join Comment C on P.id = C.post_id left outer join Post_Tag PT on PT.post_id = P.id left outer join Tag T on PT.tag_id = T.id where B.id = #{id} </select>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM