温馨提示×

mybatis实体类的关联查询

小樊
89
2024-09-11 08:50:15
栏目: 编程语言

MyBatis 可以通过关联查询来获取实体类之间的关系。关联查询主要用于处理一对一、一对多和多对一的关系。以下是一些示例,展示了如何在 MyBatis 中执行关联查询。

  1. 一对一关系:

假设我们有两个实体类:User 和 UserDetails,其中 User 与 UserDetails 之间存在一对一的关系。

首先,创建 User 和 UserDetails 的实体类:

public class User {
    private int id;
    private String name;
    private UserDetails userDetails;
    // getter and setter methods
}

public class UserDetails {
    private int id;
    private String address;
    private int userId;
    // getter and setter methods
}

然后,在 UserMapper.xml 文件中编写关联查询:

    <id property="id" column="user_id"/>
   <result property="name" column="user_name"/>
   <association property="userDetails" javaType="UserDetails" resultMap="UserDetailsResultMap"/>
</resultMap><resultMap id="UserDetailsResultMap" type="UserDetails">
    <id property="id" column="user_details_id"/>
   <result property="address" column="address"/>
   <result property="userId" column="user_id"/>
</resultMap><select id="getUserWithDetails" resultMap="UserResultMap">
    SELECT u.id as user_id, u.name as user_name, ud.id as user_details_id, ud.address, ud.user_id
    FROM user u
    JOIN user_details ud ON u.id = ud.user_id
    WHERE u.id = #{userId}
</select>
  1. 一对多关系:

假设我们有两个实体类:Author 和 Book,其中 Author 与 Book 之间存在一对多的关系。

首先,创建 Author 和 Book 的实体类:

public class Author {
    private int id;
    private String name;
    private List<Book> books;
    // getter and setter methods
}

public class Book {
    private int id;
    private String title;
    private int authorId;
    // getter and setter methods
}

然后,在 AuthorMapper.xml 文件中编写关联查询:

    <id property="id" column="author_id"/>
   <result property="name" column="author_name"/>
   <collection property="books" ofType="Book" resultMap="BookResultMap"/>
</resultMap><resultMap id="BookResultMap" type="Book">
    <id property="id" column="book_id"/>
   <result property="title" column="title"/>
   <result property="authorId" column="author_id"/>
</resultMap><select id="getAuthorWithBooks" resultMap="AuthorResultMap">
    SELECT a.id as author_id, a.name as author_name, b.id as book_id, b.title, b.author_id
    FROM author a
    LEFT JOIN book b ON a.id = b.author_id
    WHERE a.id = #{authorId}
</select>
  1. 多对一关系:

假设我们有两个实体类:Student 和 Classroom,其中 Student 与 Classroom 之间存在多对一的关系。

首先,创建 Student 和 Classroom 的实体类:

public class Student {
    private int id;
    private String name;
    private Classroom classroom;
    // getter and setter methods
}

public class Classroom {
    private int id;
    private String name;
    // getter and setter methods
}

然后,在 StudentMapper.xml 文件中编写关联查询:

    <id property="id" column="student_id"/>
   <result property="name" column="student_name"/>
   <association property="classroom" javaType="Classroom" resultMap="ClassroomResultMap"/>
</resultMap><resultMap id="ClassroomResultMap" type="Classroom">
    <id property="id" column="classroom_id"/>
   <result property="name" column="classroom_name"/>
</resultMap><select id="getStudentWithClassroom" resultMap="StudentResultMap">
    SELECT s.id as student_id, s.name as student_name, c.id as classroom_id, c.name as classroom_name
    FROM student s
    JOIN classroom c ON s.classroom_id = c.id
    WHERE s.id = #{studentId}
</select>

这些示例展示了如何在 MyBatis 中执行一对一、一对多和多对一的关联查询。你可以根据自己的需求调整这些示例以适应你的项目。

0