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

實(shí)體類
/**
*書籍
*/
@Data
public class Book {
private String id;
private String name;
private String author;
private Double price;
//出版社
private Publisher pub;//一本書對應(yīng)一個出版社
}
/**
*出版社
*/
@Data
public class Publisher {
private String id;
private String name;
private String phone;
private String address;
}XML關(guān)聯(lián)查詢
<!--配置關(guān)聯(lián)實(shí)體類-->
<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>
<!--關(guān)聯(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標(biāo)簽
<collection>和<association>標(biāo)簽屬性基本相同,就多了一個ofType屬性。

實(shí)體類
/**
*出版社
*/
@Data
public class Publisher {
private String id;
private String name;
private String phone;
private String address;
// 書籍列表
List<Book> bookList;//一個出版社對應(yīng)多本書
}/**
*書籍
*/
@Data
public class Book {
private String id;
private String name;
private String author;
private Double price;
}XML關(guān)聯(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標(biāo)簽的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;//一個出版社對應(yīng)多本書
}/**
*書籍
*/
@Data
public class Book {
private String id;
private String name;
private String author;
private Double price;
// 新增---狀態(tài)
private String status;
}<!--修改collection標(biāo)簽的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 實(shí)體類,用于表示訂單(Order)、用戶(User)和訂單項(xiàng)(OrderItem)的關(guān)系:
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),以及關(guān)聯(lián)的用戶對象(user)和訂單項(xiàng)對象集合(orderItems)。
User 類表示用戶,包含了用戶的基本信息(userId 和 username)。
OrderItem 類表示訂單項(xiàng),包含了訂單項(xiàng)的基本信息(orderItemId、itemName 和 quantity)。
xml配置:當(dāng)使用 MyBatis 的 XML 配置文件進(jìn)行結(jié)果映射時,以下是 <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 屬性的關(guān)聯(lián)關(guān)系。property 屬性指定了關(guān)聯(lián)屬性的名稱為 user,javaType 屬性指定了關(guān)聯(lián)屬性的類型為 User。在 <association> 元素內(nèi)部,使用 <id> 和 <result> 元素進(jìn)行屬性映射的配置。
使用 <collection> 配置了 orderItems 屬性的關(guān)聯(lián)關(guān)系。property 屬性指定了關(guān)聯(lián)屬性的名稱為 orderItems,ofType 屬性指定了集合元素的類型為 OrderItem。在 <collection> 元素內(nèi)部,同樣使用 <id> 和 <result> 元素進(jìn)行屬性映射的配置。
到此這篇關(guān)于mybatis中<association>和<collection>的使用與區(qū)別的文章就介紹到這了,更多相關(guān)mybatis <association>和<collection>內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- mybatis collection關(guān)聯(lián)查詢多個參數(shù)方式
- MyBatis使用嵌套查詢collection和association的實(shí)現(xiàn)
- mybatis?resultMap之collection聚集兩種實(shí)現(xiàn)方式
- MyBatis的collection和association的使用解讀
- Mybatis中一對多(collection)和一對一(association)的組合查詢使用
- Mybatis使用Collection屬性的示例代碼
- Mybatis的collection三層嵌套查詢方式(驗(yàn)證通過)
- mybatis?collection和association的區(qū)別解析
- MyBatis中<collection>標(biāo)簽的多種用法
相關(guān)文章
selenium高效應(yīng)對Web頁面元素刷新的實(shí)例講解
今天小編就為大家分享一篇selenium高效應(yīng)對Web頁面元素刷新的實(shí)例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
No ‘Access-Control-Allow-Origin‘ header is&nb
這篇文章主要介紹了No ‘Access-Control-Allow-Origin‘ header is present跨域及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Springboot配置AOP的注解切點(diǎn)失效解決方案
本文探討了在Spring框架中使用AOP攔截靜態(tài)方法調(diào)用的實(shí)現(xiàn)細(xì)節(jié),重點(diǎn)在于ApplicationContext和ApplicationContextAware接口的使用差異,通過對比兩種獲取Bean的方式,最終解決了切點(diǎn)無法被攔截的問題2026-06-06
SpringBoot如何返回Json數(shù)據(jù)格式
這篇文章主要介紹了SpringBoot如何返回Json數(shù)據(jù)格式問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
微信小程序與AspNetCore SignalR聊天實(shí)例代碼
這篇文章主要介紹了微信小程序與AspNetCore SignalR聊天實(shí)例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08

