collection和association
时间: 2023-04-22 16:00:14 浏览: 155
collection指的是一组对象或数据的集合,可以是数组、列表、集合等。在编程中,我们可以使用集合来存储和管理多个对象或数据。
association指的是两个或多个对象之间的关联关系。在面向对象编程中,我们可以使用关联关系来描述不同对象之间的联系,例如一对多、多对多等关系。这些关系可以通过属性、方法或其他方式来实现。
相关问题
collection和association区别
"Collection" 和 "association" 在面向对象编程中有着不同的含义。
"Collection" 指的是一组对象的集合,这些对象可能有相同的属性和行为。它可以是一个数组、列表或映射等数据结构。
"Association" 指的是两个类之间的关系,表示一个类的对象与另一个类的对象之间的关系。这种关系可能是一对一,一对多或多对多。
mybatis中collection和association的区别
MyBatis中collection和association都是用来处理关联关系的元素,但是它们的作用和用法有所不同。
1. collection:用于处理一对多的关联关系,即一个实体对象中包含多个子实体对象的情况。collection标签通常用于在一个实体类中包含一个List或Set类型的属性,该属性中包含多个子实体对象。示例:
```xml
<resultMap id="authorMap" type="Author">
<id property="id" column="author_id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<collection property="articles" ofType="Article">
<id property="id" column="article_id"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
</collection>
</resultMap>
```
2. association:用于处理一对一的关联关系,即一个实体对象中包含一个子实体对象的情况。association标签通常用于在一个实体类中包含一个其他实体类类型的属性。示例:
```xml
<resultMap id="articleMap" type="Article">
<id property="id" column="article_id"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<association property="author" javaType="Author">
<id property="id" column="author_id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
</association>
</resultMap>
```
总结:
collection和association都是用于处理关联关系的元素,但是collection用于处理一对多的关联关系,association用于处理一对一的关联关系。在具体使用时,需要根据实际情况选择合适的标签。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)