因为SpringBoot就是为了实现没有配置文件,因此之前手动在Mybatis中配置的PageHelper现在需要重新配置,而且配置方式与之前的SSM框架中还是有点点区别。

   首先需要在pom文件中加入

  1. <dependency>
  2. <groupId>com.github.pagehelper</groupId>
  3. <artifactId>pagehelper-spring-boot-starter</artifactId>
  4. <version>0.1.0</version>
  5. </dependency>

然后在SpringBoot的配置文件application.yml中加入如下配置:

  1. pagehelper:
  2. helperDialect: sqlserver
  3. reasonable: true
  4. supportMethodsArguments: true
  5. pageSizeZero: true
  6. params: count=countSql

目前Pagehelper插件支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页,不同数据库只需要修改helperDialect就行。

   java代码中的使用如下:

  1. PageHelper.startPage(page, rows);
  2. List<Map> list = testService.find();
  3. PageInfo<Map> pageInfo = new PageInfo<>(list);

第一行是设置页数和每页显示几条,插件会自动对接下来的sql语句加上分页方式。PageInfo中是分页的一些信息,包括总页数,当前页,总数据等。

访问数据库采用mybatis框架

1.添加pom文件依赖

  1. <!-- spring mvc支持 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- springboot整合mybatis -->
  7. <dependency>
  8. <groupId>org.mybatis.spring.boot</groupId>
  9. <artifactId>mybatis-spring-boot-starter</artifactId>
  10. <version>1.3.1</version>
  11. </dependency>
  12. <!-- springboot分页插件 -->
  13. <dependency>
  14. <groupId>com.github.pagehelper</groupId>
  15. <artifactId>pagehelper-spring-boot-starter</artifactId>
  16. <version>1.2.2</version>
  17. </dependency>
  18. <!-- 阿里巴巴druid数据库连接池 -->
  19. <dependency>
  20. <groupId>com.alibaba</groupId>
  21. <artifactId>druid</artifactId>
  22. <version>1.1.3</version>
  23. </dependency>
  24. <!-- mysql驱动 -->
  25. <dependency>
  26. <groupId>mysql</groupId>
  27. <artifactId>mysql-connector-java</artifactId>
  28. </dependency>

2.配置application.yml

  1. # 与mybatis整合
  2. mybatis:
  3. config-location: classpath:mybatis.xml
  4. mapper-locations:
  5. - classpath:mapper/*.xml
  6. # 分页配置
  7. pagehelper:
  8. helper-dialect: mysql
  9. reasonable: true
  10. support-methods-arguments: true
  11. params: count=countSql

3.service层中使用插件

  1. package com.ahut.serviceImpl;
  2. import java.util.List;
  3. import javax.servlet.ServletContext;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.transaction.annotation.Transactional;
  7. import org.springframework.web.context.ContextLoader;
  8. import com.ahut.entity.GoodsType;
  9. import com.ahut.mapper.GoodsTypeMapper;
  10. import com.ahut.service.GoodsTypeService;
  11. import com.github.pagehelper.PageHelper;
  12. /**
  13. *
  14. * @ClassName: GoodsTypeServiceImpl
  15. * @Description: 商品类型业务逻辑处理
  16. * @author cheng
  17. * @date 2017年7月17日 上午10:04:31
  18. */
  19. @Service
  20. @Transactional(rollbackFor = { RuntimeException.class, Exception.class })
  21. public class GoodsTypeServiceImpl implements GoodsTypeService {
  22. // 数据访问
  23. @Autowired
  24. private GoodsTypeMapper typeDao;
  25. /**
  26. *
  27. * @Title: getList
  28. * @Description: 从数据库中获取所有商品类型列表
  29. * @param pageNum 当前页
  30. * @param pageSize 当前页面展示数目
  31. * @return
  32. * @throws Exception
  33. */
  34. public List<GoodsType> getList(int pageNum, int pageSize) throws Exception {
  35. //使用分页插件,核心代码就这一行
  36. PageHelper.startPage(pageNum, pageSize);
  37. // 获取
  38. List<GoodsType> typeList = typeDao.getList();
  39. return typeList;
  40. }
  41. }

4.controller层代码

  1. package com.ahut.action;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import com.ahut.entity.GoodsType;
  7. import com.ahut.service.GoodsTypeService;
  8. /**
  9. *
  10. * @ClassName: GoodsTypeAction
  11. * @Description: 商品类型控制层
  12. * @author cheng
  13. * @date 2017年7月17日 上午11:09:47
  14. */
  15. @RestController // 等价于@Controller+@ResponseBody
  16. public class GoodsTypeAction {
  17. // 业务逻辑
  18. @Autowired
  19. private GoodsTypeService typeService;
  20. /**
  21. *
  22. * @Title: getGoodsTypeList
  23. * @Description: 获取商品类型列表
  24. * @return
  25. * @throws Exception
  26. */
  27. @RequestMapping(value = "/getGoodsTypeList")
  28. public List<GoodsType> getGoodsTypeList(int pageNum, int pageSize) throws Exception {
  29. // 调用业务逻辑,返回数据
  30. return typeService.getList(pageNum,pageSize);
  31. }
  32. }

5.测试

已知我数据库中有九条数据:

正常情况:

1.显示第一页或者第二页数据
请求url:

  1. http://localhost:8080/getGoodsTypeList?pageNum=1&pageSize=4
  • 1

返回数据:

  1. [
  2. {
  3. "typeId": "708cc61c6a9811e796dee09467355fab",
  4. "typeName": "全部",
  5. "createTime": 1500258859000,
  6. "updateTime": 1500621762000
  7. },
  8. {
  9. "typeId": "98f8a04e6a9811e796dee09467355fab",
  10. "typeName": "考研资料",
  11. "createTime": 1500258927000,
  12. "updateTime": null
  13. },
  14. {
  15. "typeId": "b720c87f6a9811e796dee09467355fab",
  16. "typeName": "交通工具",
  17. "createTime": 1500258978000,
  18. "updateTime": null
  19. },
  20. {
  21. "typeId": "cbe3c2326a9811e796dee09467355fab",
  22. "typeName": "生活用品",
  23. "createTime": 1500259013000,
  24. "updateTime": 1500626046000
  25. }
  26. ]

2.显示最后一页
请求url:

  1. http://localhost:8080/getGoodsTypeList?pageNum=3&pageSize=4
  • 1

返回数据:

  1. [
  2. {
  3. "typeId": "d992195f6df111e7bab4e09467355fab",
  4. "typeName": "测试2改变了",
  5. "createTime": 1501145516000,
  6. "updateTime": 1500716178000
  7. }
  8. ]

不正常情况:
1.显示的页数小于第一页(显示第一页数据)

pageNumber <= 0

请求url:

  1. http://localhost:8080/getGoodsTypeList?pageNum=0&pageSize=4
  • 1

返回数据:

  1. [
  2. {
  3. "typeId": "708cc61c6a9811e796dee09467355fab",
  4. "typeName": "全部",
  5. "createTime": 1500258859000,
  6. "updateTime": 1500621762000
  7. },
  8. {
  9. "typeId": "98f8a04e6a9811e796dee09467355fab",
  10. "typeName": "考研资料",
  11. "createTime": 1500258927000,
  12. "updateTime": null
  13. },
  14. {
  15. "typeId": "b720c87f6a9811e796dee09467355fab",
  16. "typeName": "交通工具",
  17. "createTime": 1500258978000,
  18. "updateTime": null
  19. },
  20. {
  21. "typeId": "cbe3c2326a9811e796dee09467355fab",
  22. "typeName": "生活用品",
  23. "createTime": 1500259013000,
  24. "updateTime": 1500626046000
  25. }
  26. ]

结论:当请求页数小于第一页时,显示第一页数据

2.显示的页数大于最后一页(显示最后一页数据)
pageNum > 最后一页

请求url:

  1. http://localhost:8080/getGoodsTypeList?pageNum=4&pageSize=4
  • 1

返回数据:

  1. [
  2. {
  3. "typeId": "d992195f6df111e7bab4e09467355fab",
  4. "typeName": "测试2改变了",
  5. "createTime": 1501145516000,
  6. "updateTime": 1500716178000
  7. }

Springboot整合pagehelper分页

 

一、添加依赖

查找maven中pagehelper的版本

在pom中添加依赖

  1. <dependency>
  2. <groupId>com.github.pagehelper</groupId>
  3. <artifactId>pagehelper-spring-boot-starter</artifactId>
  4. <version>1.2.2</version>
  5. </dependency>

二、使用

网络上很多文章都会说需要在application.properties进行配置

其实完全不需要,默认的设置就已经满足大部分需要了

直接使用即可

  1. @RequestMapping(value = "getApps.do")
  2. public String getApps(Apps apps) {
  3. PageHelper.startPage(apps.getPageNum(), apps.getPageSize());
  4. ArrayList<Apps> appsList = appsService.getApps(apps);
  5. PageInfo<Apps> appsPageInfo = new PageInfo<>(appsList);
  6. return JSON.toJSONString(appsPageInfo);
  7. }

PageHelper.startPage(需要显示的第几个页面,每个页面显示的数量);

下一行紧跟查询语句,不可以写其他的,否则没有效果。

  1. PageHelper.startPage(apps.getPageNum(), apps.getPageSize());
  2. ArrayList<Apps> appsList = appsService.getApps(apps);

这样只起到了分页效果,对总页面数之类的没有详细信息

如果对页面数量等有需求,则需要加上下面这行

  1. PageInfo<T> appsPageInfo = new PageInfo<>(appsList);
  1. 这样就满足了全部的分页要求

  1.  

SpringBoot使用PageHelper进行分页的更多相关文章

  1. SpringBoot+Mybatis+PageHelper实现分页

    SpringBoot+Mybatis+PageHelper实现分页 mybatis自己没有分页功能,我们可以通过PageHelper工具来实现分页,非常简单方便 第一步:添加依赖 <depend ...

  2. spring-boot + mybatis +pagehelper 使用分页

    转自:https://segmentfault.com/a/1190000015668715?utm_medium=referral&utm_source=tuicool 最近自己搭建一个sp ...

  3. SpringBoot+Mybatis+PageHelper简化分页实现

    前言 经过一段时间的测试和修改PageHelper插件逐渐走到了让我觉得靠谱的时候,它功能的就是简化分页的实现,让分页不需要麻烦的多写很多重复的代码. 已经加入我的github模版中:https:// ...

  4. Springboot 使用pageHelper实现分页查询

    本文链接:https://blog.csdn.net/qq_35387940/article/details/91530234

  5. SpringBoot整合Mybatis关于分页查询的方法

    最近公司在用到SpringBoot整合Mybatis时当web端页面数据增多时需要使用分页查询以方便来展示数据.本人对分页查询进行了一些步骤的总结,希望能够帮助到有需要的博友.如有更好的方式,也希望评 ...

  6. Spring boot入门(三):SpringBoot集成结合AdminLTE(Freemarker),利用generate自动生成代码,利用DataTable和PageHelper进行分页显示

    关于SpringBoot和PageHelper,前篇博客已经介绍过Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件,前篇博客大致讲述了S ...

  7. springboot+thymeleaf+pageHelper带条件分页查询

    html层 <div> <a class="num"><b th:text="'共 '+ ${result.resultMap['pages ...

  8. SpringBoot集成MyBatis的分页插件 PageHelper

    首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而 ...

  9. spring-boot | 整合通用Mabatis 分页插件PageHelper

    Mybatis通用Mapper介绍 Mybatis 通用 Mapper 极其方便的使用 Mybatis 单表的增删改查,支持单表操作,不支持通用的多表联合查询 优点: 通用 Mapper 可以极大的方 ...

随机推荐

  1. unity发布安卓lua路径不存在问题

    项目用的是xlua 采用自定义加载方式 使用File去读取路径下的文件,lua文件本来放在了StreamingAssets路径下 PC运行无问题,发布安卓后,居然提示路径不存在. 查了下资料后发现,F ...

  2. 进入保护模式(一)——《x86汇编语言:从实模式到保护模式》读书笔记12

    之前已经做了一些理论上的铺垫,这次我们就可以看代码了. 一.代码清单 ;代码清单11-1 ;文件名:c11_mbr.asm ;文件说明:硬盘主引导扇区代码 ;创建日期:2011-5-16 19:54 ...

  3. Composite Design Pattern in Java--转

    https://dzone.com/articles/composite-design-pattern-in-java-1 The composite pattern is meant to &quo ...

  4. vue-cli 3.x安装配置步骤详细说明

      一.vue-cli 3.x简单介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统:是一个类似于 create-react-app 的可以用例命令行快速配置和生成一个 vue 项 ...

  5. Ubuntu系统修改Python软链接

    1.查看使用的版本 python --version 2.查看当前所使用版本的位置 which python 3.如果第二步结果是 /usr/bin/python 则直接删除即可 sudo rm /u ...

  6. 【linux相识相知】独立硬盘冗余阵列-RAID

    独立硬盘冗余阵列(RAID,Redundant Array of Independant Disks),旧称为廉价磁盘冗余阵列(Redundant Array of Inexpensive Disks ...

  7. jqGrid -treeGrid 按需加载

    Load Rows On Demand (AJAX) 参考:http://www.guriddo.net/demo/treegridjs/

  8. C# 获取字符串长度

    int leng = System.Text.Encoding.Default.GetBytes(attachfileId2).Length;

  9. 再写一篇ubuntu服务器的环境配置文

    三年前写过一篇,但是环境和三年前比已经发生了比较大的变化,于是重新写一篇,自己以后再次配置也比较方便.我个人而言并没有觉得centos比ubuntu好用多少,所以继续选用ubuntu. 一.硬盘分区  ...

  10. PCA 降维算法详解 以及代码示例

    转载地址:http://blog.csdn.net/watkinsong/article/details/38536463 1. 前言 PCA : principal component analys ...