Mybatis(三)——关联映射

本文详细介绍了Mybatis中的一对一、多对一和一对多关联映射的查询方法。通过示例展示了如何使用连表查询和分步查询来获取学生信息及其对应的教师信息,以及查询教师对应的学生信息。同时,讨论了在实体类中如何处理这些关联关系。
摘要由CSDN通过智能技术生成

Mybatis(三)——关联映射

一、关联映射:举例关系说明

数据库创建表,student,teacher

在这里插入图片描述

在这里插入图片描述

关系说明:

  1. 一个老师可以有多个学生
  2. 一个学生只有一个老师
  3. 一个老师对学生:一对多的关系
  4. 一个学生老师:一对一的关系

二、一对一、多对一的关系 : 查询学生信息及其对应的教师信息

学生实体:用对象来存储教师信息,因为一个学生对应一个教师对象

public class Student {

    private Integer id;
    private String Sname;
    private String sex;
    private Integer age;
    private Integer t_id;
    //这个是重点
    private Teacher teacher;
}

教师实体:

public class Teacher {
    private Integer id;
    private String Tname;
}

1.第一种形式:连表查询

数据库查询sql:

SELECT  student.id,student.name,teacher.name FROM student LEFT JOIN teacher  on student.t_id = teacher.id
mybatis多表联查查询语句:(嵌套其他实体对象)

对于特殊数据:

  • 如果是对象:用association :< association property=“teacher” javaType=“com.qcby.entity.Teacher”>,特殊数据特殊处理
  • < result property=“id” column=“id”/> :所要查询的字段,property代表java中实体的属性名称,column:表示数据库的字段
   <!--    按照结果嵌套处理-->
<select id="getStudent1" resultMap="StudentTeacher1">
   SELECT  student.id,student.Sname,teacher.Tname FROM student  LEFT JOIN teacher  on student.t_id = teacher.id
</select>
   <resultMap id="StudentTeacher1" type="com.qcby.entity.Student">
       <result property="id" column="id"/>
       <result property="Sname" column="Sname"/>
       <result property="sex" column="sex"/>
       <result property="age" column="age"/>
       <result property="t_id" column="t_id"/>
     <!-- 复杂的属性我们需要单独去处理 对象:association   集合:collection   -->
   	 <!-- property="teacher" student类当中的关联字段 -->
   	 <!-- javaType="com.javen.model.Teacher" 为复杂属性设置类类型-->	
       <association property="teacher" javaType="com.qcby.entity.Teacher">
           <result property="id" column="id"/>
           <result property="Tname" column="Tname"/>
       </association>
   </resultMap>

2.第二种形式 分步查询

数据库查询sql:

SELECT s.id,s.Sname,t.Tname FROM student s,teacher t where s.t_id = t.id
mybatis分布查询查询语句:
<select id = "getStudent"  resultMap="StudentTeacher">
    select * from student;
</select>
<!--结果映射集-->
<resultMap id="StudentTeacher"  type="com.qcby.entity.Student">
    <result property="id" column="id"/>
    <result property="Sname" column="Sname"/>
    <result property="sex" column="sex"/>
    <result property="age" column="age"/>
    <result property="t_id" column="t_id"/>
    <!-- select="getTeacher"  :调用下一个查询语句       -->
    <!-- column="t_id" 两个表的关联字段-->
    <association property="teacher" column="t_id" javaType="com.qcby.entity.Teacher" select="getTeacher"/>
</resultMap>

<select id="getTeacher" resultType="com.qcby.entity.Teacher">
    select  * from  teacher where id = #{t_id};    <!-- #{id}; 可以写任何东西,因为会自动匹配 t_id -->
</select>

三、一对多 :查询教师对应的学生信息

设立教师实体:用集合来存储对应的学生信息,因为一个教师对应多个学生

public class Teacher {
    private Integer id;
    private String Tname;
    //这个一定要有
    private List<Student> students;
}

1.第一种形式:按照结果嵌套处理

mybatis查询语句:

<!--按照结果进行查询-->
<select id="getTeacher" resultMap="TeacherStudent">
    SELECT  teacher.id,teacher.Tname,student.Sname FROM teacher
        LEFT JOIN student  on student.t_id = teacher.id
 </select>
<resultMap id="TeacherStudent" type="com.qcby.entity.Teacher">
    <result property="id" column="id"/>
    <result property="Tname" column="Tname"/>
    <!-- 复杂的属性我么需要单独去处理 对象:association   集合:collection
      在集合中的泛型信息,我们使用ofType获取
      -->
    <collection property="students" ofType="com.qcby.entity.Student">
    	<!-- 查询什么写什么 -->
        <result property="Sname" column="Sname"/>
    </collection>
</resultMap>

2.第二种形式:按照查询嵌套处理

mybatis查询语句: 对于特殊字段集合采用分布查询的方式,特殊字段特殊处理:< collection property=“students” column=“t_id”
ofType=“com.qcby.entity.Student” select=“getStudentByTeacherId” />,getStudentByTeacherId一个新的查询语句

<!--按照查询嵌套处理:分布查询-->
<select id="getTeacher" resultMap="TeacherStudent2">
    select * from teacher
</select>
<resultMap id="TeacherStudent2" type="com.qcby.entity.Teacher">
    	<!--column="t_id"  传值-->
    <collection property="students" column="t_id"  
                ofType="com.qcby.entity.Student" select="getStudentByTeacherId" />  <!--实现分布查询-->
</resultMap>

<select id="getStudentByTeacherId" resultType="com.qcby.entity.Student">
    select * from student where id = #{t_id}
</select
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值