软件编程
位置:首页>> 软件编程>> java编程>> Mybatis关联映射举例详解

Mybatis关联映射举例详解

作者:·~简单就好  发布时间:2023-08-05 18:18:40 

标签:Mybatis,关联映射

一、关联映射

举例关系说明

数据库创建表,student,teacher

Mybatis关联映射举例详解

Mybatis关联映射举例详解

关系说明:

  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=&ldquo;teacher&rdquo; javaType=&ldquo;com.qcby.entity.Teacher&rdquo;>,特殊数据特殊处理

  • < result property=&ldquo;id&rdquo; column=&ldquo;id&rdquo;/> :所要查询的字段,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;
}

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

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>

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

mybatis查询语句: 对于特殊字段集合采用分布查询的方式,特殊字段特殊处理:< collection property=&ldquo;students&rdquo; column=&ldquo;t_id&rdquo;

ofType=&ldquo;com.qcby.entity.Student&rdquo; select=&ldquo;getStudentByTeacherId&rdquo; />,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

来源:https://blog.csdn.net/qq_45830276/article/details/125320784

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com