MyBatis-Plus的分页查询集合列表操作

本文介绍了如何在MyBatis-Plus中利用Stream进行分页查询和集合操作,展示了核心的CURD逻辑,适用于Java和Spring Boot项目。
摘要由CSDN通过智能技术生成

mybats-plus 一套标准多表操作CURD使用stream分割

核心代码逻辑

		//模拟分页数据
		Integer total=8;	
        Integer size=3;
        Integer current=1;

        PageModel<BannerInfo> page = new PageModel<>(req.getPageNum(),req.getPageSize());

        //模拟mp查询数据库查出来的集合
        IPage<BannerInfo> listByPage = bannerInfoServic
batis-plus是Mybatis的增强工具,在分页查询方面提供了很好的支持。下面是mybatis-plus分页递归查询的步骤: 1.在实体类添加@TableField注解,指定父节点id的字段名和子节点集合的字段名。 2.在Mapper接口添加递归查询方法,使用@Select注解指定SQL语句,使用@Param注解指定参数。 3.在XML文件编写递归查询SQL语句,使用WITH RECURSIVE关键字实现递归查询,使用UNION ALL关键字将多个查询结果合并。 4.在Service层调用递归查询方法,使用Page对象实现分页查询。 下面是mybatis-plus分页递归查询的示例代码: 1.实体类: ```java @Data @TableName("tree") public class Tree { @TableId(type = IdType.AUTO) private Integer id; private String name; @TableField("parent_id") private Integer parentId; @TableField(exist = false) private List<Tree> children; } ``` 2.Mapper接口: ```java public interface TreeMapper extends BaseMapper<Tree> { @Select("WITH RECURSIVE cte(id, name, parent_id) AS (SELECT id, name, parent_id FROM tree WHERE id = #{id} UNION ALL SELECT t.id, t.name, t.parent_id FROM tree t JOIN cte ON t.parent_id = cte.id) SELECT * FROM cte") List<Tree> selectChildrenById(@Param("id") Integer id, Page<Tree> page); } ``` 3.XML文件: ```xml <select id="selectChildrenById" resultMap="BaseResultMap"> WITH RECURSIVE cte(id, name, parent_id) AS ( SELECT id, name, parent_id FROM tree WHERE id = #{id} UNION ALL SELECT t.id, t.name, t.parent_id FROM tree t JOIN cte ON t.parent_id = cte.id ) SELECT * FROM cte LIMIT #{page.offset}, #{page.size} </select> ``` 4.Service层: ```java @Service public class TreeService extends ServiceImpl<TreeMapper, Tree> { public IPage<Tree> selectChildrenById(Integer id, Page<Tree> page) { List<Tree> list = baseMapper.selectChildrenById(id, page); page.setRecords(list); return page; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值