大家好!今天写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. 泛型约束new()的使用

    下面泛型约束代码,where字句后面有new()约束,T类型必须有公有的无参的构造函数. private T InternalCreate<T>() where T : IObjectWi ...

  2. Codeforces Round #352 (Div. 2) (A-D)

    672A Summer Camp 题意: 1-n数字连成一个字符串, 给定n , 输出字符串的第n个字符.n 很小, 可以直接暴力. Code: #include <bits/stdc++.h& ...

  3. Linux Shell下”>/dev/null 2>&1“相关知识说明

    0:表示键盘输入(stdin)1:表示标准输出(stdout),系统默认是1 2:表示错误输出(stderr) command >/dev/null 2>&1 &  == ...

  4. QT删除非空文件夹

    int choose; choose = QMessageBox::warning(NULL,"warning","确定删除该文件?",QMessageBox: ...

  5. jQuery基础操作

    1.jQuery的介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库.jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行Ajax交 ...

  6. python读取uti-8格式ini配置文件出现UnicodeDecodeError: 'gbk' codec can't decode byte 0xba in position 367: illegal multibyte sequence错误解决方法

    出现这种错误只需要在read下添加encoding='utf-8' 如: from configparser import ConfigParser cf = ConfigParser() cf.re ...

  7. spring-第一章-基本用法

    一,简介 spring是一个开源框架 是一个非常强大的容器框架,分离项目组件之间的依赖关系,由spring容器管理整个项目的资源和配置; 通常我们可以称spring是容器大管家(项目大管家) 功能: ...

  8. Model类代码生成器

    using Humanizer; using System; using System.Collections.Generic; using System.Data; using System.Dat ...

  9. VS 中 无法嵌入互操作类型“……”,请改用适用的接口的解决方法

    在引用COM组件的时候,出现了无法嵌入互操作类型"--",请改用适用的接口的错误提示. 选中项目中引入的dll,鼠标右键,选择属性,把"嵌入互操作类型"设置为F ...

  10. RESTful-2一分钟理解什么是REST和RESTful

    从事web开发工作有一小段时间,REST风格的接口,这样的词汇总是出现在耳边,然后又没有完全的理解,您是不是有和我相同的疑问呢?那我们一起来一探究竟吧! 就是用URL定位资源,用HTTP描述操作. 知 ...