欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis中association和collection的使用與區(qū)別

 更新時(shí)間:2024年01月19日 11:23:55   作者:tanxinji  
在 MyBatis 中,<association>?和?<collection>?是用于配置結(jié)果映射中關(guān)聯(lián)關(guān)系的兩個(gè)元素,本文主要介紹了mybatis中<association>和<collection>的使用與區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下

在 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)的基本信息(orderItemIditemName 和 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)屬性的名稱為 orderItemsofType 屬性指定了集合元素的類型為 OrderItem。在 <collection> 元素內(nèi)部,同樣使用 <id> 和 <result> 元素進(jìn)行屬性映射的配置。

到此這篇關(guān)于mybatis中<association>和<collection>的使用與區(qū)別的文章就介紹到這了,更多相關(guān)mybatis <association>和<collection>內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論