spring boot 使用分页插件pagehelper

spring boot 使用分页插件pagehelper

一.引入依赖
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper-spring-boot-starter</artifactId>
	<version>1.2.3</version>
</dependency>
二.以查询到的一个list集合为例
//mapper接口
@Select("select * from `order` where uId=#{uId}")
@Results({@Result(column="uId",property = "user",
                  one=@One(select="cn.bongbb.mapper.UserMapper.getUserById")),
          @Result(column="gId",property ="good",
                  one=@One(select="cn.bongbb.mapper.GoodMapper.getGoodById"))})
    public List<Order> selectOrderByUId(Integer uId);

//service层
public List<Order> selectOrderByUId(Integer uId, Integer pageNum){
        //开启分页插件,pageNum:当前页码  设置每页记录为10条
        PageHelper.startPage(pageNum,10);
        List<Order> orders = orderMapper.selectOrderByUId(uId);
        return orders;
}

//controller层

@GetMapping("/user/order/{pageNum}")
public String userOrder(Model model, HttpSession session, 
                        @PathVariable("pageNum") Integer pageNum){
        User user = (User)session.getAttribute("user");
        List<Order> orders = orderService.selectOrderByUId(user.getuId(),pageNum);

        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        orderPageInfo.setPageNum(pageNum);
        model.addAttribute("orders",orders);
        model.addAttribute("orderPageInfo",orderPageInfo);

        return "user/orderList";
    }

//thymeleaf
<div>
	<span>
		<a th:href="@{/user/order/1}">首页</a>
    </span>

    <span>
       <a th:href="${orderPageInfo.isIsFirstPage()==true}?
           @{/user/order/1}:@{/user/order/}+${orderPageInfo.getPageNum()-1}">上一页</a>
    </span>

     <span th:text="${orderPageInfo.getPageNum()}+'/'+
         ${orderPageInfo.getPages()}">
     </span>

     <span>
         <a th:href="${orderPageInfo.isIsLastPage()==true}?	
         	@{/user/order/}+${orderPageInfo.getPages()}:
            @{/user/order/}+${orderPageInfo.getPageNum()+1}">下一页
         </a>
     </span>

     <span>
         <a th:href="@{/user/order/}+${orderPageInfo.getPages()}">尾页</a>
     </span>
</div>
三.效果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值