大家好!今天写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分页插件的使用的更多相关文章

  1. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  2. SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页

    SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...

  3. mybatis pagehelper分页插件使用

    使用过mybatis的人都知道,mybatis本身就很小且简单,sql写在xml里,统一管理和优化.缺点当然也有,比如我们使用过程中,要使用到分页,如果用最原始的方式的话,1.查询分页数据,2.获取分 ...

  4. spring boot 整合pagehelper分页插件

    Spring Boot 整合pagehelper分页插件 测试环境: spring boot  版本 2.0.0.M7 mybatis starter 版本  1.3.1 jdk 1.8 ------ ...

  5. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

  6. 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法

    spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...

  7. Mybatis的PageHelper分页插件的PageInfo的属性参数,成员变量的解释,以及页面模板

    作者:个人微信公众号:程序猿的月光宝盒 //当前页 private int pageNum; //每页的数量 private int pageSize; //当前页的数量 private int si ...

  8. 逆向工程文件example完美结合使用PageHelper分页插件及分页不成功原因

    原生的mybatis需要手写sql语句,项目数据库表多了之后,可以让你写sql语句写到手软,于是mybatis官方提供了mybatis-generator:mybatis逆向工程代码生成工具,用于简化 ...

  9. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用

    笔记 5.PageHelper分页插件使用     简介:讲解开源组件,mybaits分页插件的使用 1.引入依赖             <!-- 分页插件依赖 -->          ...

随机推荐

  1. java学习笔记11-static关键字

    如果在类中使用static关键字创建方法,这种方法称为类方法,可以在这个类中直接引用.而不是用static创建的方法.这种方法称为对象方法(实例方法),需要创建对象后才能使用. package les ...

  2. UGUI打字机效果文本组件

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...

  3. expect 批量自动部署ssh 免密登陆 之 三

    #!/bin/expect -- ########################################## zhichao.hu #Push the id.pas.pub public k ...

  4. ubuntu 18.04 配置远程ssh/远程ftp/远程vnc登陆

    18.04相比过去采用了新的桌面,配置环境稍微有一些不同了. 首先是远程登录,windows用Tera Trem连接,ip地址得自己根据实际情况来. ubuntu上,sudo apt-get inst ...

  5. C语言--第0份作业

        对网络专业的了解 •网络专业是现如今比较难学的科目,要学习利用计算机对网络进行管理开发. •网络专业对于思维的要求比较高,需要我们有一定的数学思维. •网络专业的人才比较缺失,只要我们学的足够 ...

  6. 04-HTTP协议和静态Web服务器

    一.HTTP协议(HyperText Transfer Protocol)     超文本传输协议,超文本是超级文本的缩写,是指超越文本限制或者超链接,比如:图片.音乐.视频.超链接等等都属于超文本. ...

  7. 洛谷 P1414 又是毕业季II

    题目链接 https://www.luogu.org/problemnew/show/P1414 题目背景 “叮铃铃铃”,随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离 ...

  8. 【玩转开源】基于Docker搭建Bug管理系统 MantisBT

    环境Ubuntu18.04 + Docker 1. Docker Hub 链接:https://hub.docker.com/r/vimagick/mantisbt 这里直接使用docker命令的方式 ...

  9. C# 百钱买百鸡

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  10. 烽火2640路由器命令行手册-11-IP语音配置命令

    IP语音配置命令 目  录 第1章 配置拨号对命令... 1 1.1 配置拨号对命令... 1 1.1.1 dial-peer voice. 1 1.1.2 application. 2 1.1.3 ...