MyBatis 可以通過關聯查詢來獲取實體類之間的關系。關聯查詢主要用于處理一對一、一對多和多對一的關系。以下是一些示例,展示了如何在 MyBatis 中執行關聯查詢。
假設我們有兩個實體類: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>
假設我們有兩個實體類: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>
假設我們有兩個實體類: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 中執行一對一、一對多和多對一的關聯查詢。你可以根據自己的需求調整這些示例以適應你的項目。