• 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

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

  • 相关阅读:
    CF1712 解题报告
    C++ 继承
    【前端版】分布式医疗云平台【Open-his 环境搭建、前台 vue-element-ui 搭建】(十六)
    云计算发展
    处理飞书在线文档导出Word后无法自动编号问题
    (1-线性回归问题)RBF神经网络
    C现代方法(第3、4章)笔记
    ICML2021 | RSD: 一种基于几何距离的可迁移回归表征学习方法
    应用多元统计分析--多元数据的直观表示(R语言)
    SAP 批量删除变式
  • 原文地址:https://blog.csdn.net/yuyanhaonan/article/details/133956172