PageHelper分页插件的使用
大家好!今天写ssm项目实现分页的时候用到pageHelper分页插件,在使用过程中出现了一些错误,因此写篇随笔记录下整个过程
1.背景:在项目的开发的过程中,为了实现所有的功能。
2.目标:实现分页操作
3.出现问题:分页不成功
4.过程
1.当点击按钮的时候,发起查询请求同时带有两个参数(page=1,size=4,size代表显示的当前多少页page代表当前第几页)href="${pageContext.request.contextPath}/orders/findAll.do?page=1&size=4"> <iclass="fa fa-circle-o"></i> 订单管理
2.发起请求后到Controller层接收前端页面传过来的数据1,和4,@RequestParam(name = "page",required = true,defaultValue = "1")int page,@RequestParam(name = "size",required = true,defaultValue = "4")int size)
3.调用findAll(page.size)方法去数据库里面查询所有的数据
4.在service层使用PageHelper.startPage(page,size);就可以实现分页
5.然后在controller把返回来的值放到PageInfo对象里面
6同时用modelAndView.放到域中发到页面去,跳转逻辑视图
7.前端页面上显示使用循环把分页的数据显示,
8.分页也就是再发一次请求,同时用EL表达式带参数去查询(相关的代码在尾部会贴出来)
5.解决方案
1.本次的操作分页不成功主要在忘记在mybaitis配制文件中配置pageHelper的相关信息如图
<!-- 把交给IOC管理 SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 传入PageHelper的插件 -->
<property name="plugins">
<array>
<!-- 传入插件的对象 -->
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">oracle</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
2.同时要注意要在pom.xml中加入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
6.总结
1.代码不难考虑要全面。
2.记住时时清理缓存
3.必要时候可以重启环境(比较麻烦不建议)
4.不要浮躁,耐心检查错误
7.相关代码
1.前端显示代码
<c:forEach items="${pageInfo.list}" var="orders">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${orders.id }</td>
<td>${orders.orderNum }</td>
<td>${orders.product.productName }</td>
<td>${orders.product.productPrice }</td>
<td>${orders.orderTimeStr }</td>
<td class="text-center">${orders.orderStatusStr }</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs">订单</button>
<button type="button" class="btn bg-olive btn-xs" onclick="location.href='${pageContext.request.contextPath}/orders/findById.do?id=${orders.id}'">详情</button>
<button type="button" class="btn bg-olive btn-xs">编辑</button>
</td>
</tr>
</c:forEach>
2.前端分页代码
<div class="box-tools pull-right">
<ul class="pagination">
<li>
<a href="${pageContext.request.contextPath}/orders/findAll.do?page=1&size=${pageInfo.pageSize}" aria-label="Previous">首页</a>
</li>
<li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pageNum-1}&size=${pageInfo.pageSize}">上一页</a></li>
<c:forEach begin="1" end="${pageInfo.pages}" var="pageNum">
<li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageNum}&size=${pageInfo.pageSize}">${pageNum}</a></li>
</c:forEach>
<li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pageNum+1}&size=${pageInfo.pageSize}">下一页</a></li>
<li>
<a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pages}&size=${pageInfo.pageSize}" aria-label="Next">尾页</a>
</li>
</ul>
</div>
</div>
3.controller层代码
package com.busc.ssm.controller;
import com.github.pagehelper.PageInfo;
import com.itheima.ssm.domain.Orders;
import com.itheima.ssm.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/orders")
public class OrdersController {
@Autowired
private OrdersService ordersService;
//未分页的订单查询
/*@RequestMapping("/findAll.do")
public ModelAndView findAll()throws Exception{
ModelAndView mv = new ModelAndView();
List<Orders>ordersList=ordersService.findAll();
mv.addObject("ordersList",ordersList);
mv.setViewName("orders-list");//转到逻辑视图
return mv;
}*/
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")int page,@RequestParam(
name = "size",required = true,defaultValue = "4"
)int size)throws Exception{
ModelAndView mv = new ModelAndView();
List<Orders>ordersList=ordersService.findAll(page,size);
PageInfo pageInfo = new PageInfo(ordersList);
mv.addObject("pageInfo",pageInfo);
mv.setViewName("orders-page-list");
return mv;
}
}
4.service代码
package com.busc.ssm.service.impl;
import com.github.pagehelper.PageHelper;
import com.busc.ssm.dao.OrdersDao;
import com.busc.ssm.domain.Orders;
import com.busc.ssm.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional//事务
public class OrdersServiceImpl implements OrdersService {
//注入dao层
@Autowired
private OrdersDao ordersDao;
/*分页操作*/
@Override
public List<Orders> findAll(int page,int size) throws Exception {
PageHelper.startPage(page,size);
return ordersDao.findAll();
}
//未分页
/*@Override
public List<Orders> findAll() throws Exception {
return ordersDao.findAll();
}*/
}
2019-03-2621:33:09
作者:何秀好
PageHelper分页插件的使用的更多相关文章
- Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件
前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...
- SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页
SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...
- mybatis pagehelper分页插件使用
使用过mybatis的人都知道,mybatis本身就很小且简单,sql写在xml里,统一管理和优化.缺点当然也有,比如我们使用过程中,要使用到分页,如果用最原始的方式的话,1.查询分页数据,2.获取分 ...
- spring boot 整合pagehelper分页插件
Spring Boot 整合pagehelper分页插件 测试环境: spring boot 版本 2.0.0.M7 mybatis starter 版本 1.3.1 jdk 1.8 ------ ...
- SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件
原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
- Mybatis的PageHelper分页插件的PageInfo的属性参数,成员变量的解释,以及页面模板
作者:个人微信公众号:程序猿的月光宝盒 //当前页 private int pageNum; //每页的数量 private int pageSize; //当前页的数量 private int si ...
- 逆向工程文件example完美结合使用PageHelper分页插件及分页不成功原因
原生的mybatis需要手写sql语句,项目数据库表多了之后,可以让你写sql语句写到手软,于是mybatis官方提供了mybatis-generator:mybatis逆向工程代码生成工具,用于简化 ...
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
笔记 5.PageHelper分页插件使用 简介:讲解开源组件,mybaits分页插件的使用 1.引入依赖 <!-- 分页插件依赖 --> ...
随机推荐
- spring mybatis mysql 事务不起作用
之前框架事务应该是好的,不过这次做了些修改,不知如何竟然不好用了,整理了好半天,java框架配置就是吓人,有一点不熟悉的就可能暴露问题,好处是又深入的了解了配置原理. 开始以为是mysql不支持事务的 ...
- centos7 把终端显示改为英文/中文
把终端显示改为英文: 1.先备份语言配置文件 cp /etc/locale.conf /home/locale.conf.backup 2.打开配置文件 vim /etc/locale.conf 3. ...
- python学习第30天
tcp协议的socket server 并发效果验证客户端的合法性socket模块还有一些其他的方法
- syntax error near unexpected token `do(写的shell脚本出现格式问题)--->1.问题2.展示信息3.解决方案
1问题:Linux和windows下的回车换行符不兼容的问题 [root@node-01 script]# sh start_zk.sh art_zk.sh: line 3: syntax error ...
- Windows安装redis并将redis设置成服务
Redis 作为一种缓存工具,主要用于解决高并发的问题,在分布式系统中有着极其广泛的应用,Redis 本身是应用于 Linux/Unix 平台的(部署在服务器上边),官方并没有提供 Windows 平 ...
- 修改MySQL的数据目录
环境:CentOS Linux release 7.1.1503 (Core) 1. 安装MYSQL wget http://dev.mysql.com/get/mysql-community-rel ...
- 末学者笔记--apache编译安装及LAMP架构上线
apache介绍 一.Apache的三种工作模式 Apache一共有3种稳定的MPM模式(多进程处理模块),它们分别是prefork.worker.event.http-2.2版本的httpd默认的m ...
- PHP实现url参数组合字符串与数组相互转换
$data = array( 'name' => 'tom', 'sex' => 1, 'channel' => 'ty' ); 数组转url参数字符串 $queryStr = ht ...
- 高精度POJ1001
今天看到这道题了 poj1001 题目地址是http://bailian.openjudge.cn/practice/1001/ 英文看得懂,可是算法不明白,所以转别人的文章,留着给学生看看:乔高建( ...
- 树莓派B+使用入门&RPI库安装&wringPi库安装
最近看看试用一下树莓派进行一些开发操作,于是入手一块Raspberry Pi B+的板子来玩.由于没有显示器,没有备用的键盘和鼠标,所以想到用SSH来控制树莓派,刚开始还很担心已经安装好的操作系统到底 ...