PageHelper分页插件使用,以及SSM、Springboot使用分页失效问题解决

PageHelper分页插件使用,SSM,Springboot分页失效问题解决

返回导航页

SSM 框架

https://blog.csdn.net/hancoder/article/details/106922139
1、使用maven(这个神器一定要学会使用)

   <dependency>		**//引入jar包**
    <groupId>com.github.pagehelper</groupId>           
    <artifactId>pagehelper</artifactId>          
    <version>5.0.3</version>
   </dependency>
   <dependency>
    <groupId>com.github.jsqlparser</groupId>
    <artifactId>jsqlparser</artifactId>
    <version>0.9.5</version>
        </dependency>

2、在Spring-mybatis的配置文件中配置分页插件

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> 
 	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:/com/hbyg/mapper/*.xml"></property>
        
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <!--使用下面的方式配置参数,一行配置一个 -->
                        <value>
                            offsetAsPageNum=true
                            rowBoundsWithCount=true
                            pageSizeZero=true
                            reasonable=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean> 

3、在Controller层的结果中添加分页操作

   @RequestMapping("/getAllDep")
   public String GetAll(@RequestParam(value="pn", defaultValue="1")Integer pn,Model model){
   	//紧跟查询结果的分页情况(第几页, 每页多少记录)
   	PageHelper.startPage(pn, 2);
   	List<DepartInfo> dep=service1.getAllDep();
   	//骚操作: 将结果放入pageinfo中,这个pageinfo就有很多有用的参数
   	PageInfo<DepartInfo> info=new PageInfo<>(dep,5);	
   	
   		**//new PageInfo<>(dep)单页  连续5分页new PageInfo<>(dep,5);**
   		
   	System.out.println("当前页面"+info.getPageNum());
   	System.out.println("总页码"+info.getPages());
   	System.out.println("总记录数"+info.getTotal());
   	System.out.println("当前页有几个记录"+info.getSize());
   	System.out.println("当前页的pagesize"+info.getPageSize());
   	System.out.println("前一页"+info.getPrePage());
   	System.out.println("结果"+info.getList());
   	int[] nums=info.getNavigatepageNums();
   	model.addAttribute("info", info);
   	return "success";   //跳转到success.jsp 页面
   }
   ```
   ___4、JSP页面接收参数___
   ```
   
<table cellspacing="0" cellpadding="5" border="1">
   <tr>
   <th>id</th><th>Name</th><th>DESC</th><th>STATE</th>	
   </tr>
   <c:forEach items="${info.list}" var="de">
   <tr>
   	<td>${de.depId }</td>
   	<td>${de.depName }</td>
   	<td>${de.depDesc }</td>
   	<td>${de.depState }</td>
   </tr>
   </c:forEach>
   <tr>
   	<td colspan="4"> 
   	<a href="getAllDep?pn=1">首页</a>	<a href="getAllDep?pn=${info.prePage }">上一页</a>
   	  <c:forEach items="${info.navigatepageNums }" var="num">
   	  <c:if test="${num==info.pageNum }">
   	  【${num}】
   	  </c:if>
   	  <c:if test="${num !=info.pageNum }">
   	  <a href="getAllDep?pn=${num }">${num }</a>
   	  </c:if>
   	  
   	  </c:forEach>
   	<a href="getAllDep?pn=${info.nextPage }">下一页</a>  <a href="getAllDep?pn=${info.pages }"></a>
   	</td>
   </tr>
</table>

5、显示效果
前端显示效果

Springboot框架

引入pom依赖 -

<!-- 分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.3</version>
        </dependency>
        //这个依赖一定要引入,不然分页插件不管用,即使加了配置类也不管用
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.12</version>
        </dependency>

使用,一定要注意代码顺序,原因请看分页插件原理

PageHelper.startPage(currentPage, pageSize); // 1
List<BlogInfo> list = blogService.getBlogInfoListByCategoryName(typeName); // 2
PageInfo<BlogInfo> info = new PageInfo<>(list, 5);  //3

无需配置文件

//1 
Page<InvoiceBillInfoVo> page = new MyPage<>(billInfoPo.getPageIndex(), billInfoPo.getPageSize());

//2
List<InvoiceBillInfoVo> invoiceBillList = viVisitDataMapper.getInvoiceBillList(page,billInfoPo);

//3
 List<InvoiceBillInfoVo> getInvoiceBillList(@Param("page") Page page, @Param("model") InvoiceBillInfoPo billInfoPo);

//4
 <select id="getInvoiceBillList" resultMap="InvoiceBillInfoVoMap">
 select * from XXX 
 </select>



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
SSM使用PageHelper实现分页非常简单。首先,你需要在项目的pom.xml文件中引入PageHelper的依赖。然后,在你的Mapper接口中添加对应的方法,并在该方法上添加PageHelper分页注解。最后,你可以在Service层或Controller层中调用该方法并传入分页参数,即可实现分页。下面是一个示例代码: 1. 首先,在pom.xml文件中引入PageHelper的依赖: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency> ``` 2. 在Mapper接口中添加分页方法,并使用PageHelper的注解进行分页配置: ```java import com.github.pagehelper.Page; import org.apache.ibatis.annotations.Param; public interface YourMapper { // 使用PageHelper的注解进行分页配置 List<YourEntity> getYourList(@Param("pageNum") int pageNum, @Param("pageSize") int pageSize); } ``` 3. 在Service层或Controller层中调用分页方法并传入分页参数: ```java import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @Service public class YourService { @Autowired private YourMapper yourMapper; public PageInfo<YourEntity> getYourList(int pageNum, int pageSize) { // 设置分页参数 PageHelper.startPage(pageNum, pageSize); // 调用Mapper方法查询数据 List<YourEntity> list = yourMapper.getYourList(pageNum, pageSize); // 封装成PageInfo对象返回 PageInfo<YourEntity> pageInfo = new PageInfo<>(list); return pageInfo; } } ``` 通过以上三个步骤,你就可以在SSM使用PageHelper实现分页了。首先,在pom.xml文件中引入PageHelper的依赖,然后在Mapper接口中添加分页方法并使用PageHelper的注解进行分页配置,最后在Service层或Controller层中调用分页方法并传入分页参数,即可实现分页。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【分页查询】在SSM环境中使用PageHelper](https://blog.csdn.net/weixin_44757863/article/details/109728886)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SSM整合实例--(二)PageHelper分页操作](https://blog.csdn.net/Double____C/article/details/91048168)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码头薯条Pro

本文能帮到阁下,在下很开心!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值