广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Mybatis关联映射举例详解
  • 385
分享到

Mybatis关联映射举例详解

2024-04-02 19:04:59 385人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录一、关联映射二、一对一多对一的关系1.第一种形式-连表查询2.第二种形式-分步查询三、一对多第一种形式按照结果嵌套处理第二种形式按照查询嵌套处理一、关联映射 举例关系说明 数据库

一、关联映射

举例关系说明

数据库创建表,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;
}

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

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=“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

到此这篇关于Mybatis关联映射举例详解的文章就介绍到这了,更多相关Mybatis关联映射内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Mybatis关联映射举例详解

本文链接: https://www.lsjlt.com/news/165641.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作