Mybatis多对多查询案例!

在MyBatis中执行多对多查询需要使用两个主要表和一个连接表(通常称为关联表)来演示。在这个示例中,我们将使用一个示例数据库模型,其中有三个表:`students`、`courses` 和 `student_courses`,它们之间建立了多对多关系。`students` 表存储学生信息,`courses` 表存储课程信息,`student_courses` 表用于将学生与他们所选的课程关联起来。

以下是如何在MyBatis中执行多对多查询的步骤:

  1. 创建数据库表和数据。首先,确保你已经创建了适当的数据库表并插入了示例数据。以下是表的基本结构:
  • `students` 表包含 `student_id` 和 `student_name` 列。

  • `courses` 表包含 `course_id` 和 `course_name` 列。

  • `student_courses` 表用于将学生与课程关联,包含 `student_id` 和 `course_id` 列。

  1. 创建 MyBatis 映射文件。你需要创建一个MyBatis映射文件来定义SQL查询。以下是一个示例映射文件的结构:

```xml

XML 复制代码
<mapper namespace="com.example.StudentCourseMapper">
    <select id="getStudentCourses" parameterType="int" resultMap="studentCourseResult">
        SELECT s.student_id, s.student_name, c.course_id, c.course_name
        FROM students s
        JOIN student_courses sc ON s.student_id = sc.student_id
        JOIN courses c ON sc.course_id = c.course_id
        WHERE s.student_id = #{studentId}
    </select>
    
    <resultMap id="studentCourseResult" type="com.example.StudentCourse">
        <result property="studentId" column="student_id"/>
        <result property="studentName" column="student_name"/>
        <result property="courseId" column="course_id"/>
        <result property="courseName" column="course_name"/>
    </resultMap>
</mapper>

```

  1. 创建Java模型类。创建一个Java模型类来表示多对多关系的结果。例如,可以创建一个名为 `StudentCourse` 的类:

```java

java 复制代码
public class StudentCourse {
    private int studentId;
    private String studentName;
    private int courseId;
    private String courseName;

    // 省略 getter 和 setter 方法
}

```

  1. 创建Mapper接口。创建一个Mapper接口,定义用于执行多对多查询的方法:

```java

java 复制代码
public interface StudentCourseMapper {
    List<StudentCourse> getStudentCourses(int studentId);
}

```

  1. 配置MyBatis。在MyBatis的配置文件中配置数据源和映射文件。

  2. 执行多对多查询。在应用程序中调用 `getStudentCourses` 方法,将学生的 `studentId` 作为参数传递:

```java

java 复制代码
SqlSessionFactory sqlSessionFactory = MyBatisConfig.getSqlSessionFactory();
try (SqlSession session = sqlSessionFactory.openSession()) {
    StudentCourseMapper mapper = session.getMapper(StudentCourseMapper.class);
    List<StudentCourse> studentCourses = mapper.getStudentCourses(1); // 传入学生的ID
    for (StudentCourse sc : studentCourses) {
        System.out.println("Student: " + sc.getStudentName() + ", Course: " + sc.getCourseName());
    }
}

```

这将执行查询,返回与给定学生相关的课程列表。

以上示例演示了如何在MyBatis中执行多对多查询。你可以根据自己的数据模型和需求进行相应的调整。

相关推荐
清风~徐~来6 分钟前
【MySQL】库的操作
数据库·mysql·oracle
文牧之3 小时前
PostgreSQL 用户资源管理
运维·数据库·postgresql
Paraverse_徐志斌7 小时前
MySQL 线上大表 DDL 如何避免锁表(pt-online-schema-change)
数据库·mysql·ddl·mysql锁·锁表·pt-osc
哈哈幸运8 小时前
MySQL运维三部曲初级篇:从零开始打造稳定高效的数据库环境
linux·运维·数据库·mysql·性能优化
愚公搬代码8 小时前
【愚公系列】《Python网络爬虫从入门到精通》055-Scrapy_Redis分布式爬虫(安装Redis数据库)
数据库·爬虫·python
pwzs8 小时前
深入浅出 MVCC:MySQL 并发背后的多版本世界
数据库·后端·mysql
大熊猫今天吃什么8 小时前
【一天一坑】空数组,使用 allMatch 默认返回true
前端·数据库
双叶8369 小时前
(51单片机)LCD显示数据存储(DS1302时钟模块教学)(LCD1602教程)(独立按键教程)(延时函数教程)(I2C总线认识)(AT24C02认识)
c语言·数据库·单片机·嵌入式硬件·mongodb·51单片机·nosql
XY.散人9 小时前
初识Redis · C++客户端list和hash
数据库·redis·缓存
码上飞扬9 小时前
深入 MySQL 高级查询:JOIN、子查询与窗口函数的实用指南
数据库·mysql