mybatis中association和collection的使用与区别
作者:tanxinji
在 MyBatis 中,<association> 和 <collection> 是用于配置结果映射中关联关系的两个元素。
<association> 用于配置一对一的关联关系,表示两个对象之间的关系是一对一的。例如,一个订单对象关联一个用户对象,使用 <association> 进行配置。
<collection> 用于配置一对多的关联关系,表示一个对象关联多个对象。例如,一个部门对象关联多个员工对象,使用 <collection> 进行配置。
主要区别:
关联关系类型:
<association>表示一对一的关联关系,而<collection>表示一对多的关联关系。配置位置:
<association>和<collection>元素通常在<resultMap>中使用,用于定义结果映射规则。<association>用于配置单个属性的关联关系,而<collection>用于配置集合属性的关联关系。属性映射:
<association>使用<id>和<result>进行属性映射的配置,用于将关联对象的属性与查询结果进行映射。<collection>除了使用<id>和<result>进行属性映射外,还使用<association>进行嵌套的关联关系配置,用于定义集合元素对象内部的关联关系。查询语句:
<association>通常对应一个单独的查询语句,用于获取关联对象的数据。<collection>通常也对应一个查询语句,用于获取关联对象的集合数据。
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关联查询
<!--配置关联实体类-->
<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>
<!--关联查询-->
<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关联查询
<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属性,{参数名1=列名1,参数名2=列名2}
/**
*出版社
*/
@Data
public class Publisher {
private String id;
private String name;
private String phone;
private String address;
// 新增---状态
private String status;
// 书籍列表
List<Book> bookList;//一个出版社对应多本书
}/**
*书籍
*/
@Data
public class Book {
private String id;
private String name;
private String author;
private Double price;
// 新增---状态
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),以及关联的用户对象(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 属性的关联关系。property 属性指定了关联属性的名称为 user,javaType 属性指定了关联属性的类型为 User。在 <association> 元素内部,使用 <id> 和 <result> 元素进行属性映射的配置。
使用 <collection> 配置了 orderItems 属性的关联关系。property 属性指定了关联属性的名称为 orderItems,ofType 属性指定了集合元素的类型为 OrderItem。在 <collection> 元素内部,同样使用 <id> 和 <result> 元素进行属性映射的配置。
到此这篇关于mybatis中<association>和<collection>的使用与区别的文章就介绍到这了,更多相关mybatis <association>和<collection>内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- mybatis collection关联查询多个参数方式
- MyBatis使用嵌套查询collection和association的实现
- mybatis resultMap之collection聚集两种实现方式
- MyBatis的collection和association的使用解读
- Mybatis中一对多(collection)和一对一(association)的组合查询使用
- Mybatis使用Collection属性的示例代码
- Mybatis的collection三层嵌套查询方式(验证通过)
- mybatis collection和association的区别解析
- MyBatis中<collection>标签的多种用法
