mybatis中association和collection的使用與區(qū)別
在 MyBatis 中,<association>
和 <collection>
是用于配置結(jié)果映射中關(guān)聯(lián)關(guān)系的兩個(gè)元素。
<association>
用于配置一對一的關(guān)聯(lián)關(guān)系,表示兩個(gè)對象之間的關(guān)系是一對一的。例如,一個(gè)訂單對象關(guān)聯(lián)一個(gè)用戶對象,使用 <association>
進(jìn)行配置。
<collection>
用于配置一對多的關(guān)聯(lián)關(guān)系,表示一個(gè)對象關(guān)聯(lián)多個(gè)對象。例如,一個(gè)部門對象關(guān)聯(lián)多個(gè)員工對象,使用 <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>
用于配置單個(gè)屬性的關(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)一個(gè)單獨(dú)的查詢語句,用于獲取關(guān)聯(lián)對象的數(shù)據(jù)。<collection>
通常也對應(yīng)一個(gè)查詢語句,用于獲取關(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)一個(gè)出版社 } /** *出版社 */ @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)簽屬性基本相同,就多了一個(gè)ofType屬性。
實(shí)體類
/** *出版社 */ @Data public class Publisher { private String id; private String name; private String phone; private String address; // 書籍列表 List<Book> bookList;//一個(gè)出版社對應(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;//一個(gè)出版社對應(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>
示例
下面是一個(gè)示例的 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é)果映射時(shí),以下是 <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)查詢多個(gè)參數(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)文章
Springboot+Shiro記錄用戶登錄信息并獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot+Shiro記錄用戶登錄信息,并獲取當(dāng)前登錄用戶信息,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Java版仿QQ驗(yàn)證碼風(fēng)格圖片驗(yàn)證碼
這篇文章主要為大家分享了java圖片驗(yàn)證碼實(shí)例代碼,感興趣的小伙伴們可以參考一下2016-04-04Maven?Web項(xiàng)目使用Cargo插件實(shí)現(xiàn)自動(dòng)化部署的詳細(xì)步驟
cargo ,它是一組幫助用戶實(shí)現(xiàn)自動(dòng)化部署,操作Web容器的工具,并且?guī)缀踔С炙械腤eb容器,這篇文章主要介紹了Maven?Web項(xiàng)目使用Cargo實(shí)現(xiàn)自動(dòng)化部署,需要的朋友可以參考下2023-02-02快速解決springboot在yml配置了啟動(dòng)端口但啟動(dòng)還是8080問題
這篇文章主要介紹了快速解決springboot在yml配置了啟動(dòng)端口但啟動(dòng)還是8080問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03java Date裝成英文String后,無法再轉(zhuǎn)回Date的解決方案
本文介紹了java Date裝成英文String后,無法再轉(zhuǎn)回Date的解決方案。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01RocketMQ線程池創(chuàng)建實(shí)現(xiàn)原理詳解
這篇文章主要為大家介紹了RocketMQ線程池創(chuàng)建實(shí)現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Java后臺(tái)與微信小程序的數(shù)據(jù)交互實(shí)現(xiàn)
這篇文章主要介紹了Java后臺(tái)與微信小程序的數(shù)據(jù)交互實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12