溫馨提示×

mybatis實(shí)體類的關(guān)聯(lián)查詢

小樊
82
2024-09-11 08:50:15
欄目: 編程語言

MyBatis 可以通過關(guān)聯(lián)查詢來獲取實(shí)體類之間的關(guān)系。關(guān)聯(lián)查詢主要用于處理一對一、一對多和多對一的關(guān)系。以下是一些示例,展示了如何在 MyBatis 中執(zhí)行關(guān)聯(lián)查詢。

  1. 一對一關(guān)系:

假設(shè)我們有兩個(gè)實(shí)體類:User 和 UserDetails,其中 User 與 UserDetails 之間存在一對一的關(guān)系。

首先,創(chuàng)建 User 和 UserDetails 的實(shí)體類:

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 文件中編寫關(guān)聯(lián)查詢:

    <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. 一對多關(guān)系:

假設(shè)我們有兩個(gè)實(shí)體類:Author 和 Book,其中 Author 與 Book 之間存在一對多的關(guān)系。

首先,創(chuàng)建 Author 和 Book 的實(shí)體類:

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 文件中編寫關(guān)聯(lián)查詢:

    <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. 多對一關(guān)系:

假設(shè)我們有兩個(gè)實(shí)體類:Student 和 Classroom,其中 Student 與 Classroom 之間存在多對一的關(guān)系。

首先,創(chuàng)建 Student 和 Classroom 的實(shí)體類:

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 文件中編寫關(guān)聯(lián)查詢:

    <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 中執(zhí)行一對一、一對多和多對一的關(guān)聯(lián)查詢。你可以根據(jù)自己的需求調(diào)整這些示例以適應(yīng)你的項(xiàng)目。

0