zoukankan      html  css  js  c++  java
  • Mybatis-07-多对一和一对多处理

    多对一处理

    如,

    • 多个学生,对应一个老师
    • 多个学生关联一个老师(多对一)
    • 一个老师有很多学生(一对多)

    SQL:

    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);
    insert into student(id,name,tid) values(2,'小红',1);
    insert into student(id,name,tid) values(3,'小张',1);
    insert into student(id,name,tid) values(4,'小李',1);
    insert into student(id,name,tid) values(5,'小王',1);
    

    测试环境搭建

    1. 导入lombok
    2. 新建实体类Teacher,Student
    3. 建立Mapper接口
    4. 建立Mapper.xml文件
    5. 在核心配置文件中绑定注册Mapper接口或文件
    6. 测试查询是否成功

    按照查询嵌套处理

    <!--思路:
        1.查询所有的学生信息
        2.根据tid,寻找对应的老师
    -->
    
    <select id="getStudent" resultMap="StudentTeacher">
        select * from student
    </select>
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="Teacher">
        select * from teacher where id=#{id};
    </select>
    

    按照结果嵌套处理

    <!--按照结果嵌套处理-->
    <select id="getStudent2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.name tname
        from student s,teacher t
        where s.tid=t.id;
    </select>
    <resultMap id="StudentTeacher2" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>
    

    回顾对比Mysql多对一查询方式:

    • 子查询
    • 联表查询

    一对多处理

    步骤:

    1. 环境搭建

    2. 实体类

    @Data
    public class Student {
        private int id;
        private String name;
        private int tid;
    }
    
    @Data
    public class Teacher {
        private int id;
        private String name;
        private List<Student> students;
    }
    
    1. 按照结果嵌套处理
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.id sid,s.name sname,t.name tname,t.id tid
        from student s,teacher t
        where s.tid=t.id and t.id=#{tid};
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>
    
    1. 按照查询嵌套处理
    <select id="getTeacher2" resultMap="TeacherStudent2">
        select * from mybatis.teacher where id=#{tid}
    </select>
    <resultMap id="TeacherStudent2" type="Teacher">
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
    </resultMap>
    <select id="getStudentByTeacherId" resultType="Student">
        select * from mybatis.student where tid=#{tid}
    </select>
    

    小结

    1. 关联 association多对一
    2. 集合 collection 一对多
    3. javaType 和 ofType
      1. JavaType 用来指定实体类中属性的类型
      2. ofType 用来指定映射到List中的pojo类型,泛型中的约束类型

    注意点:

    • 保证SQL的可读性,尽量通俗易懂

    • 注意属性名和字段名区别

    • 可以使用日志排除错误,推荐Log4j

    面试高频

    • Mysql引擎
    • InnoDB底层原理
    • 索引
    • 索引优化
  • 相关阅读:
    MySQL 连接 触发器 函数 视图 存储过程
    Django的ORM2
    nmp安装vuejs
    Docker 技术入门与实践(第3版)笔记
    docker官方 入门
    centos 各目录介绍
    阿里云es 安装docker
    linux命令
    centos 7 mini 安装青岛OJ
    leetcode_304. 二维区域和检索
  • 原文地址:https://www.cnblogs.com/CodeHuba/p/13489695.html
Copyright © 2011-2022 走看看