• 一对多+多对一关系的查询


    比如:

    一对多: 一个老师对应多个学生

    多对一: 多个学生对应一个老师

     

    一、建表+实体类

    创建一个学生表和一个老师表

    通过学生的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>
  • 相关阅读:
    查看linux系统的版本
    单机运行环境搭建之 --CentOS-6.5安装配置Tengine
    nginx启动、重启、关闭
    JAVASE02-Unit010: 多线程基础 、 TCP通信
    俄罗斯方块小游戏
    JAVASE02-Unit09: 多线程基础
    JAVASE02-Unit08: 文本数据IO操作 、 异常处理
    JAVASE02-Unit07: 基本IO操作 、 文本数据IO操作
    JAVASE02-Unit06: 文件操作——File 、 文件操作—— RandomAccessFile
    JAVASE02-Unit05: 集合操作 —— 查找表
  • 原文地址:https://www.cnblogs.com/kzyuan/p/12608128.html
Copyright © 2020-2023  润新知