mybatisplus <collection>
时间: 2023-11-25 17:46:58 浏览: 33
MybatisPlus中的<collection>标签用于实现一对多关联查询。它可以将一个实体类中的一个List类型的属性与另一个实体类中的多条记录进行关联,实现一对多的查询。在使用<collection>标签时,需要指定关联的属性名、关联的实体类、关联的查询语句等信息。同时,还需要在实体类中定义对应的List类型的属性,并在mapper.xml文件中编写对应的查询语句。如果在使用MybatisPlus进行关联查询时出现了问题,可以通过查看日志来进行排查。
相关问题
mybatisplus使用collection
MybatisPlus是Mybatis的增强工具,在Mybatis的基础上增加了很多实用的功能。在MybatisPlus中,使用collection标签可以实现对集合类型的处理。
例如,我们有一个Order实体类,其中有一个List类型的属性items,我们可以使用MybatisPlus的collection标签来处理:
```xml
<select id="getOrder" resultMap="orderMap">
select * from order where id=#{id}
</select>
<resultMap id="orderMap" type="Order">
<id column="id" property="id"/>
<result column="order_no" property="orderNo"/>
<collection property="items" ofType="Item">
<id column="item_id" property="id"/>
<result column="item_name" property="name"/>
<result column="item_price" property="price"/>
</collection>
</resultMap>
```
在上面的例子中,我们使用collection标签将Order实体类中的items属性映射为一个List<Item>类型的集合。在collection标签中,我们需要指定集合元素的类型(ofType属性),并且使用id和result标签来映射集合元素中的属性。
这样,当我们查询Order实体类时,MybatisPlus会自动将查询结果中的items字段转换为一个List<Item>类型的集合,并将集合中的元素映射为Item实体类对象。
IPage<VideoFaa> rows = this.getBaseMapper().getVideoList(page);(IPage<Video>) rows.getRecords().stream().map(item->{return Video}).collect(Collectors.toList());报java.util.ArrayList cannot be cast to com.baomidou.mybatisplus.core.metadata.IPage
这个错误的原因是你将一个 `ArrayList` 强制转换成了 `IPage`。在这行代码 `(IPage<Video>) rows.getRecords().stream().map(item->{return Video}).collect(Collectors.toList())` 中,你使用了 `collect(Collectors.toList())` 将 `Stream` 转换成了 `ArrayList`。你应该使用 `collect(Collectors.toCollection(ArrayList::new))` 将其转换成 `ArrayList`,然后再将其包装成 `IPage` 对象。
下面是修改后的代码示例:
```
List<Video> videoList = rows.getRecords().stream().map(item -> { return Video }).collect(Collectors.toCollection(ArrayList::new));
IPage<Video> videoPage = new Page<>(page.getCurrent(), page.getSize(), rows.getTotal());
videoPage.setRecords(videoList);
```
这里我们首先将 `Stream` 转换成了 `ArrayList`,然后手动创建了一个新的 `Page` 对象,并将 `ArrayList` 赋值给 `Page` 的 `records` 属性,最后将 `Page` 对象返回。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)