mybatis中association和collection的使用與區(qū)別
在 MyBatis 中,<association> 和 <collection> 是用于配置結果映射中關聯(lián)關系的兩個元素。
<association> 用于配置一對一的關聯(lián)關系,表示兩個對象之間的關系是一對一的。例如,一個訂單對象關聯(lián)一個用戶對象,使用 <association> 進行配置。
<collection> 用于配置一對多的關聯(lián)關系,表示一個對象關聯(lián)多個對象。例如,一個部門對象關聯(lián)多個員工對象,使用 <collection> 進行配置。
主要區(qū)別:
關聯(lián)關系類型:
<association>表示一對一的關聯(lián)關系,而<collection>表示一對多的關聯(lián)關系。配置位置:
<association>和<collection>元素通常在<resultMap>中使用,用于定義結果映射規(guī)則。<association>用于配置單個屬性的關聯(lián)關系,而<collection>用于配置集合屬性的關聯(lián)關系。屬性映射:
<association>使用<id>和<result>進行屬性映射的配置,用于將關聯(lián)對象的屬性與查詢結果進行映射。<collection>除了使用<id>和<result>進行屬性映射外,還使用<association>進行嵌套的關聯(lián)關系配置,用于定義集合元素對象內(nèi)部的關聯(lián)關系。查詢語句:
<association>通常對應一個單獨的查詢語句,用于獲取關聯(lián)對象的數(shù)據(jù)。<collection>通常也對應一個查詢語句,用于獲取關聯(lián)對象的集合數(shù)據(jù)。
association標簽

實體類
/**
*書籍
*/
@Data
public class Book {
private String id;
private String name;
private String author;
private Double price;
//出版社
private Publisher pub;//一本書對應一個出版社
}
/**
*出版社
*/
@Data
public class Publisher {
private String id;
private String name;
private String phone;
private String address;
}XML關聯(lián)查詢
<!--配置關聯(lián)實體類-->
<resultMap id="bookResultMap" type="com.entity.Book">
<!--主鍵屬性-->
<id property="id" column="id" jdbcType="VARCHAR"></id>
<!--普通屬性-->
<result property="name" column="name" jdbcType="VARCHAR"></result>
<result property="author" column="author" jdbcType="VARCHAR"></result>
<result property="price" column="price" jdbcType="VARCHAR"></result>
<!--一對一映射-->
<association property="pub" javaType="com.entity.Publisher">
<id property="id" column="id" jdbcType="VARCHAR"></id>
<result property="name" column="name" jdbcType="VARCHAR"></result>
<result property="phone" column="phone" jdbcType="VARCHAR"></result>
<result property="address" column="address" jdbcType="VARCHAR"></result>
</association>
</resultMap>
<!--關聯(lián)查詢-->
<select id="selectAllBook" resultMap="bookResultMap">
SELECT * FROM book e
left JOIN publisher d ON e.publisher_id = d.id
</select>嵌套查詢
<resultMap id="bookResultMap" type="com.entity.Book">
<!--主鍵屬性-->
<id property="id" column="id" jdbcType="VARCHAR"></id>
<!--普通屬性-->
<result property="name" column="name" jdbcType="VARCHAR"></result>
<result property="author" column="author" jdbcType="VARCHAR"></result>
<result property="price" column="price" jdbcType="VARCHAR"></result>
<association column="publisher_id" property="pub"
javaType="com.entity.Publisher" select="selectPublisher"></association>
</resultMap>
<!--書籍查詢-->
<select id="selectAllBook" resultMap="bookResultMap">
select * from book
</select>
<!--出版社映射Map-->
<resultMap id="publisherResultMap" type="com.entity.Publisher">
<id property="id" column="id" jdbcType="VARCHAR"></id>
<result property="name" column="name" jdbcType="VARCHAR"></result>
<result property="phone" column="phone" jdbcType="VARCHAR"></result>
<result property="address" column="address" jdbcType="VARCHAR"></result>
</resultMap>
<!--嵌套查詢-->
<select id="selectPublisher" resultMap="publisherResultMap">
SELECT * FROM publisher d
WHERE d.id = #{publisher_id}
</select>collection標簽
<collection>和<association>標簽屬性基本相同,就多了一個ofType屬性。

實體類
/**
*出版社
*/
@Data
public class Publisher {
private String id;
private String name;
private String phone;
private String address;
// 書籍列表
List<Book> bookList;//一個出版社對應多本書
}/**
*書籍
*/
@Data
public class Book {
private String id;
private String name;
private String author;
private Double price;
}XML關聯(lián)查詢
<resultMap id="publisherResultMap" type="com.entity.Publisher">
<id property="id" column="id" jdbcType="VARCHAR"></id>
<result property="name" column="name" jdbcType="VARCHAR"></result>
<result property="phone" column="phone" jdbcType="VARCHAR"></result>
<result property="address" column="address" jdbcType="VARCHAR"></result>
<collection property="bookList" ofType="com.entity.Book">
<id property="id" column="id" jdbcType="VARCHAR"></id>
<result property="name" column="name" jdbcType="VARCHAR"></result>
<result property="author" column="author" jdbcType="VARCHAR"></result>
<result property="price" column="price" jdbcType="VARCHAR"></result>
</collection>
</resultMap>
<select id="selectAllPublisher" resultMap="publisherResultMap">
SELECT * FROM publisher d
left JOIN book e ON e.publisher_id = d.id
</select>嵌套查詢
<resultMap id="publisherResultMap" type="com.entity.Publisher">
<id property="id" column="id" jdbcType="VARCHAR"></id>
<result property="name" column="name" jdbcType="VARCHAR"></result>
<result property="phone" column="phone" jdbcType="VARCHAR"></result>
<result property="address" column="address" jdbcType="VARCHAR"></result>
<collection column="id" property="bookList"
javaType="java.util.ArrayList" ofType="com.entity.Book"
select="selectBookList"/>
</resultMap>
<select id="selectAllPublisher" resultMap="publisherResultMap">
SELECT * FROM publisher d
</select>
<resultMap id="bookResultMap" type="com.worldly.config.entity.Employee">
<id property="id" column="id" jdbcType="VARCHAR"></id>
<result property="name" column="name" jdbcType="VARCHAR"></result>
<result property="author" column="author" jdbcType="VARCHAR"></result>
<result property="price" column="price" jdbcType="VARCHAR"></result>
</resultMap>
<select id="selectBookList" resultMap="bookResultMap">
SELECT * FROM book e
WHERE e.publisher_id = #{id}
</select>多條件查詢
修改collection標簽的column屬性,{參數(shù)名1=列名1,參數(shù)名2=列名2}
/**
*出版社
*/
@Data
public class Publisher {
private String id;
private String name;
private String phone;
private String address;
// 新增---狀態(tài)
private String status;
// 書籍列表
List<Book> bookList;//一個出版社對應多本書
}/**
*書籍
*/
@Data
public class Book {
private String id;
private String name;
private String author;
private Double price;
// 新增---狀態(tài)
private String status;
}<!--修改collection標簽的column屬性-->
<collection column="{publisherId=id,status=status}" property="bookList"
javaType="java.util.ArrayList" ofType="com.entity.Book"
select="selectBookList"/>
<select id="selectEmpBydepId" resultMap="empResultMap">
SELECT * FROM book e
WHERE e.publisher_id = #{publisherId} AND e.status=#{status}
</select>
示例
下面是一個示例的 Java 實體類,用于表示訂單(Order)、用戶(User)和訂單項(OrderItem)的關系:
public class Order {
private int orderId;
private String orderNumber;
private User user;
private List<OrderItem> orderItems;
}
public class User {
private int userId;
private String username;
}
public class OrderItem {
private int orderItemId;
private String itemName;
private int quantity;
}
在上述示例中,Order 類表示訂單,包含了訂單的基本信息(orderId 和 orderNumber),以及關聯(lián)的用戶對象(user)和訂單項對象集合(orderItems)。
User 類表示用戶,包含了用戶的基本信息(userId 和 username)。
OrderItem 類表示訂單項,包含了訂單項的基本信息(orderItemId、itemName 和 quantity)。
xml配置:當使用 MyBatis 的 XML 配置文件進行結果映射時,以下是 <association> 和 <collection> 元素的示例配置:
<resultMap id="orderResultMap" type="Order">
<id property="orderId" column="order_id" />
<result property="orderNumber" column="order_number" />
<association property="user" javaType="User">
<id property="userId" column="user_id" />
<result property="username" column="username" />
</association>
<collection property="orderItems" ofType="OrderItem">
<id property="orderItemId" column="item_id" />
<result property="itemName" column="item_name" />
<result property="quantity" column="quantity" />
</collection>
</resultMap>
<select id="getOrderById" resultMap="orderResultMap">
SELECT * FROM orders WHERE order_id = #{orderId}
</select>
使用 <association> 配置了 user 屬性的關聯(lián)關系。property 屬性指定了關聯(lián)屬性的名稱為 user,javaType 屬性指定了關聯(lián)屬性的類型為 User。在 <association> 元素內(nèi)部,使用 <id> 和 <result> 元素進行屬性映射的配置。
使用 <collection> 配置了 orderItems 屬性的關聯(lián)關系。property 屬性指定了關聯(lián)屬性的名稱為 orderItems,ofType 屬性指定了集合元素的類型為 OrderItem。在 <collection> 元素內(nèi)部,同樣使用 <id> 和 <result> 元素進行屬性映射的配置。
到此這篇關于mybatis中<association>和<collection>的使用與區(qū)別的文章就介紹到這了,更多相關mybatis <association>和<collection>內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- mybatis collection關聯(lián)查詢多個參數(shù)方式
- MyBatis使用嵌套查詢collection和association的實現(xiàn)
- mybatis?resultMap之collection聚集兩種實現(xiàn)方式
- MyBatis的collection和association的使用解讀
- Mybatis中一對多(collection)和一對一(association)的組合查詢使用
- Mybatis使用Collection屬性的示例代碼
- Mybatis的collection三層嵌套查詢方式(驗證通過)
- mybatis?collection和association的區(qū)別解析
- MyBatis中<collection>標簽的多種用法
相關文章
No ‘Access-Control-Allow-Origin‘ header is&nb
這篇文章主要介紹了No ‘Access-Control-Allow-Origin‘ header is present跨域及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
SpringBoot如何返回Json數(shù)據(jù)格式
這篇文章主要介紹了SpringBoot如何返回Json數(shù)據(jù)格式問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
微信小程序與AspNetCore SignalR聊天實例代碼
這篇文章主要介紹了微信小程序與AspNetCore SignalR聊天實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08

