信息发布→ 登录 注册 退出

Mybatis一对多和多对一处理的深入讲解

发布时间:2026-01-11

点击量:
目录
  • 建表
  • 多对一处理
    • mapper
    • 实体类
    • 按照查询嵌套处理
    • 按照结果嵌套处理
    • 回顾Mysql多对一查询方式
  • 一对多处理
    • mapper
    • 实体类
    • 按照查询嵌套处理
    • 按照查询嵌套处理
  • 结果映射
    • 面试高频点
      • 总结

        建表

        SQL:

        create table teacher(
            id int not null,
            name varchar(30) default null,
            primary key (id)
        );
        
        insert into teacher (id, name) values (1, '蔡老师');
        
        create table student(
            id int not null ,
            name varchar(30) default null,
            tid int default null,
            constraint fk_tid foreign key (tid) references teacher(id)
        );
        
        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);
        

        多对一处理

        • 多个学生对应一个老师
        • 对于学生这边而言,关联。即多个学生关联一个老师【多对一】
        • 对于老师这边而言,集合。即一个老师有很多的学生【一对多】

        mapper

        //查询所有的学生信息以及对应的老师的信息
        List<Student> queryStudentAndTeacher();
        

        实体类

        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public class Student {
            private int id;
            private String name;
        
            //学生需要关联一个老师
            private Teacher teacher;
        }
        
        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public class Teacher {
            private int id;
            private String name;
        }
        

        按照查询嵌套处理

        <!--思路:
        	1.查询所有的学生
        	2.根据查询出来的学生的tid寻找对应的老师  寻找对应的老师,子查询
        -->
        <resultMap id="rStuAndTea" type="student">
            <result property="id" column="id"/>
            <result property="name" column="name"/>
            <!-- 复杂的属性我们需要单独处理  
        							指定属性的类型
        		 对象使用association  javaType
        		 集合使用collection   ofType
        	-->
            <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
        </resultMap>
        <select id="queryStudentAndTeacher" resultMap="rStuAndTea">
            select * from student
        </select>
        <select id="getTeacher" resultType="teacher">
            select * from teacher where id = #{id}
        </select>
        

        按照结果嵌套处理

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

        回顾Mysql多对一查询方式

        • 子查询
        • 联表查询

        一对多处理

        • 一个老师有多个学生
        • 对于老师这边而言,集合。即一个老师有很多的学生【一对多】

        mapper

        //查询指定老师的信息及其所有的学生
        Teacher queryTeaAndStu(@Param("tid") int id);
        

        实体类

        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public class Teacher {
            private int id;
            private String name;
        
            //一个老师拥有多个学生
            private List<Student> students;
        }
        
        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public class Student {
            private int id;
            private String name;
            private int tid;
        }
        

        按照查询嵌套处理

        <!--按照结果嵌套查询-->
        <resultMap id="rTeaAndStu" 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>
        <select id="queryTeaAndStu" resultMap="rTeaAndStu">
            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>
        

        按照查询嵌套处理

        <!--按照查询嵌套处理-->
        <select id="queryTeaAndStu2" resultMap="rTeaAndStu2">
            select * from teacher where id = #{tid}
        </select>
        <resultMap id="rTeaAndStu2" type="teacher">
            <collection property="students" javaType="ArrayList" ofType="Student"
                        select="queryStudentByTeacherId" column="id"/>
        </resultMap>
        <select id="queryStudentByTeacherId" resultType="Student">
            select * from student where tid = #{tid}
        </select>
        

        结果映射

        面试高频点

        • MySQL引擎
        • InnoDB底层原理
        • 索引
        • 索引优化

        小结

        1. 关联 - association 【多对一】
        2. 集合 - collection 【一对多】
        3. javaType & ofType
          1. javaType 用来指定实体类中属性的类型
          2. ofType 用来指定映射到List或者集合中的entity类型,泛型中的约束类型

        注意点:

        • 保证SQL的可读性,尽量保证通俗易懂
        • 注意一对多和多对一中属性名和字段的问题
        • 如果问题不好排查错误,可以使用LOG4J日志

        总结

        在线客服
        服务热线

        服务热线

        4008888355

        微信咨询
        二维码
        返回顶部
        ×二维码

        截屏,微信识别二维码

        打开微信

        微信号已复制,请打开微信添加咨询详情!