MyBatis之关联查询

系列文章目录

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
MyBatis之关联查询


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

在使用MyBatis进行数据库操作时,关联查询是一种重要的查询方式。它能够在一个查询中同时检索多个表中的数据,从而提高查询效率和性能。在开始学习和使用MyBatis的关联查询之前,我们需要先了解一些基础概念和知识,包括数据库表、实体类、Mapper接口和Mapper.xml文件等。
在接下来的博客文章中,我们将逐步学习如何使用MyBatis进行关联查询,并通过示例代码演示如何实现多对一和一对多的关联查询。希望通过这些介绍,你能够掌握MyBatis关联查询的基本知识和技能,并能够在实际项目中应用这些知识。


提示:以下是本篇文章正文内容,下面案例可供参考

一、一对一关联查询

在 MyBatis 中,一对一关联查询是指通过在查询语句中使用连接(JOIN)操作,从多个表中获取相关联的数据。这样可以一次性获取到涉及多个表的数据,避免了多次查询和手动关联的麻烦。
一对一关联查询通常涉及到两个表,其中一个表包含主要数据,称为“主表”,另一个表包含与主表关联的数据,称为“从表”。在关联查询中,通过在主表和从表之间建立连接条件,可以将相关的数据关联起来。
在 MyBatis 的映射文件中,可以使用标签来配置一对一关联查询。以下是一个简单的示例:

<resultMap id="studentMapper" type="com.zhangsan.pojo.Student">
  <!-- 主键列 -->
  <id property="sid" column="sid"></id>
  <!-- 普通列 -->
  <result property="name" column="name"></result>
  <result property="age" column="age"></result>
  <result property="sex" column="sex"></result>
  <!-- 一对一对象列 property:属性名  column:关联列名 javaType:对象类型-->
  <association property="classes" column="classId" javaType="com.zhangsan.pojo.Classes">
    <!-- 关联对象主键列 -->
    <id property="cid" column="cid"></id>
    <!-- 关联对象普通列 -->
    <result property="className" column="className"></result>
  </association>
</resultMap>
<!-- 多表查询,级联查询学生和其班级 -->
<select id="findAll" resultMap="studentMapper">
   select * from student left join classes on student.classId = classes.cid;
</select>

二、一对多关联查询

在 MyBatis 中,一对多关联查询是指通过在查询语句中使用连接(JOIN)操作,从一个表中查询出与之关联的多个表的数据。
一对多关联查询通常涉及到两个表,其中一个表包含主键,另一个表包含外键,并且外键与主键之间存在关联关系。例如,一个学生表(student)和一个成绩表(score)之间存在一对多的关联关系,学生表中的主键是学生 ID(student_id),成绩表中的外键是学生 ID(student_id),并且每个学生可能有多个成绩记录。
以下是一个示例,展示了如何在 MyBatis 中使用一对多关联查询:假设有一个学生表(student)和一个成绩表(score),学生表包含学生 ID(student_id)和学生姓名(student_name),成绩表包含学生 ID(student_id)和成绩(score)。
1.在学生表的映射文件中定义一个关联的结果集映射,用于获取学生的成绩:

<!-- 学生表的映射文件 -->
<mapper namespace="com.example.studentMapper">
    <resultMap id="studentWithScore" type="com.example.Student">
        <id column="student_id" property="studentId" />
        <result column="student_name" property="studentName" />
        <collection property="scores" ofType="com.example.Score">
            <id column="student_id" property="studentId" />
            <result column="score" property="score" />
        </collection>
    </resultMap>

    <select id="selectStudentWithScore" parameterType="int" resultMap="studentWithScore">
        SELECT s.student_id, s.student_name, sc.score
        FROM student s
        INNER JOIN score sc ON s.student_id = sc.student_id
        WHERE s.student_id = #{studentId}
    </select>
</mapper>

2.在使用 MyBatis 进行查询时,可以使用关联的结果集映射来获取关联的数据:

// 学生表的实体类
class Student {
    private Integer studentId;
    private String studentName;
    private List<Score> scores;
    // getter 和 setter 方法
}

// 成绩表的实体类
class Score {
    private Integer scoreId;
    private Integer studentId;
    private Integer score;
    // getter 和 setter 方法
}

public class Main {
    public static void main(String[] args) {
        // 创建学生表的 Mapper 对象
        StudentMapper studentMapper = new StudentMapper();
        // 创建成绩表的 Mapper 对象
        ScoreMapper scoreMapper = new ScoreMapper();

        // 输入学生 ID
        int studentId = 1;

        // 使用 MyBatis 查询学生及其成绩
        Student student = studentMapper.selectStudentWithScore(studentId);

        // 输出学生信息和成绩
        System.out.println("Student: " + student);
    }
}

上述示例中,通过在学生表的映射文件中定义了一个关联的结果集映射studentWithScore,将学生表和成绩表关联起来。然后,在成绩表的映射文件中,添加了对关联结果集映射的引用,并在使用时通过标签进行关联。
在查询学生信息时,使用了studentMapper.selectStudentWithScore(studentId)方法,该方法将返回一个包含学生信息和成绩的Student对象。通过该对象可以获取学生的信息和关联的成绩。

三、多对多关联查询

多对多关联查询通常涉及到三个或更多的表,其中至少有两个表之间存在多对多的关联关系。以下是一个示例,展示了如何在 MyBatis 中使用多对多关联查询:
MyBatis多对多关联查询本质就是两个一对多关联查询。例如有老师类和班级类:一个老师对应多个班级,也就是老师类中有一个班级集合属性。一个班级对应多个老师,也就是班级类中有一个老师集合属性。现在通过一个示例,将查询班级的时候,把同学和老师都查出来:

<resultMap id="classesMapper" type="com.zhangsan.pojo.Classes">
  <id property="cid" column="cid"></id>
  <result property="className" column="className"></result>
  <!-- 集合列  property:属性名  column:关联列名 ofType:集合的泛型 -->
  <collection property="studentList" column="classId" ofType="com.zhangsan.pojo.Student">
    <id property="sid" column="sid"></id>
    <result property="name" column="name"></result>
    <result property="age" column="age"></result>
    <result property="sex" column="sex"></result>
  </collection>
  <collection property="teacherList" column="cid" ofType="com.zhangsan.pojo.Teacher">
    <id property="tid" column="tid"></id>
    <result property="tname" column="tname"></result>
  </collection>
</resultMap>


<select id="findAll" resultMap="classesMapper">
   select *
   from classes
   left join student
   on classes.cid = student.classId
   left join classes_teacher
   on classes.cid = classes_teacher.cid
   left join teacher
   on classes_teacher.tid = teacher.tid;
</select>


总结

提示:这里对文章进行总结:

关联查询是 MyBatis 中一种常见的查询方式,它通过在映射文件中使用JOIN语句和结果集映射来实现。在使用关联查询时,需要注意表之间的关联关系、结果集映射的配置以及查询语句的编写。通过合理使用关联查询,可以方便地获取相关联的数据,提高查询效率和数据的完整性。

  • 16
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值