• MyBatis一对多、多对一映射


    表格:

    Student

    字段类型备注
    idint主键
    namevarchar2姓名
    tidint教师id

    Teacher

    字段类型备注
    idint主键
    namevarchar姓名

    多对一(或者一对一):association

    Student类

    public class Student {
        private int id;
        private String name;
        private Teacher teacher;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Teacher类

    public class Teacher {
        private int id;
        private String name;
    }
    
    • 1
    • 2
    • 3
    • 4

    第一种,按照结果嵌套查询

    这种就简单的理解为 :

    我们已经把需要的表并在一起了,我们只需要选择我们需要的属性进行映射就行了。

    <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="name"/>
        <association property="teacher" javaType="teacher">
            <result property="name" column="tname"/>
        association>
    resultMap>
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    第二种:按查询进行嵌套处理

    这中就简单的理解为子查询

    我们先查学生表的所有信息,将查到的tid 给子查询,让子查询找到对应的数据,再把结果返回

    <select id="getStudent" resultMap="StudentTeacher">
        select * from student
        
    select>
    
    <resultMap id="StudentTeacher" type="student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="teacher" javaType="teacher" select="getTeacher"column="tid"/>
    resultMap>
    
    
    <select id="getTeacher" resultType="teacher">
        select * from teacher where id = #{??};
    select>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    一对多:collections

    Student类

    public class Student2 {
        private int id;
        private String name;
        private int tid;//关联老师
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Teacher类

    public class Teacher2 {
        private int id;
        private String name;
        private List<Student> students;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    第一种,按照结果嵌套查询

    <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="Teacher2">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
    
        <collection property="students" javaType="ArrayList" ofType="Student2">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        collection>
    resultMap>
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    第二种:按照查询进行嵌套

    <select id="getTeacherById" resultMap="TeacherStudent">
    	select id ddd, name from teacher where id = #{tid}
    select>
    
    <resultMap id="TeacherStudent" type="Teacher2">
    	<result property="id" column="ddd"/>
    	<result property="name" column="name"/>
    	<collection property="students" javaType="ArrayList" ofType="Student2"
                select="getStudentByTid" column="ddd"/>
    resultMap>
    
    <select id="getStudentByTid" resultType="student2">
    	select * from student where tid = #{hh}
    select>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    有错误请指正,共同学习。

  • 相关阅读:
    第3章_瑞萨MCU零基础入门系列教程之开发环境搭建与体验
    磷酸化甘露糖苷修饰白蛋白纳米粒/卵白蛋白-葡聚糖纳米凝胶的
    使用 OpenTelemetry 构建 .NET 应用可观测性(1):什么是可观测性
    2.6 Docker部署多个前端项目
    c: struct sort descending and ascending in windows and Ubuntu
    一图了解原码、反码、补码的演进历史
    Worthington公司α-淀粉酶的历史和分子特征详解
    没有对象怎么面向对象编程呢?这份风趣幽默的图解面向对象编程你值得拥有!
    CTF PWN 中常用的工具安装【Ubuntu 20.04】
    FPGA学习笔记(四)通过数码管学习顶层模块和例化的编写
  • 原文地址:https://blog.csdn.net/yuyanhaonan/article/details/133956172