MyBatis映射之association和collection详解
·
一、引言
一直对association和collection有点混淆,现整理一篇文章,用于加强记忆。
二、association
association用于一对一、多对一场景使用。
现在有2个表book表、bookshelf书架表。
| BOOK | ||
| 字段名称 | 类型 | 备注 |
| id | int | 主键 |
| name | varchar | 书名 |
| type | int | 类型 |
| shelf_id | int | 书架id |
| Book_shelf | ||
| 字段名称 | 类型 | 备注 |
| id | int | 主键 |
| number | varchar | 书架编号 |
| num | int | 可存放数量 |
现有需求:查询根据书籍id查询书籍信息和所在书架编号。
PoJo
public class Book{
private Integer id;
private String name;
private String type;
private Integer shelfId;
private BookShelf bookShelfDto;
}
public class BookShelf {
private Integer id;
private String number;
private String num;
}
mapper
<resultMap id="bookResultMap" type="com.abc.Book">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="type" column="type"/>
<!--关联属性-->
<association property="bookShelfDto" javaType="com.abc.BookShelf">
<id property="id" column="shelf_id"/>
<result property="number" column="number"/>
<result property="num" column="num"/>
</association>
</resultMap>
<select id="getBookInfo" resultMap="bookResultMap">
select book.id,book.name,book.type,book.shelf_id,shelf.number,shelf.num
from book left join book_shelf shelf on book.shelf_id = shelf.id
where book.id = #{id}
</select>
三、collection
应用场景为一对多关系,即实体里放集合。
表不变
现有需求:根据书架ID查询书架信息及书架存放的书籍信息。
POJO
public class Book{
private Integer id;
private String name;
private String type;
private Integer shelfId;
}
public class BookShelf {
private Integer id;
private String number;
private String num;
private List<Book> bookList;
}
<resultMap id="bookShelfResultMap" type="com.abc.BookShelf">
<id property="id" column="shelf_id"/>
<result property="number" column="number"/>
<result property="num" column="num"/>
<!--关联属性-->
<collection property="bookList" javaType="com.abc.Book">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="type" column="type"/>
</collection>
</resultMap>
<select id="getBookShelfInfo" resultMap="bookShelfResultMap">
select book.id,book.name,book.type,book.shelf_id,shelf.number,shelf.num
from book left join book_shelf shelf on book.shelf_id = shelf.id
where shelf.id = #{id}
</select>
Mapper.java
BookShelf getBookShelfInfo(Integer id);
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)