首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而不为?通常引入它们只需三步骤,不管是Spring集成还是SpringBoot集成都是老套路,我就分开总结了,望各位笑纳。

Spring集成PageHelper:

第一步:pom文件引入依赖

1 <!-- mybatis的分页插件 -->
2 <dependency>
3 <groupId>com.github.pagehelper</groupId>
4 <artifactId>pagehelper</artifactId>
5 </dependency>

第二步:MyBatis的核心配置文件中引入配置项

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE configuration
3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
5
6 <configuration>
7 <!-- 【mybatis的核心配置文件】 -->
8
9 <!-- 批量设置别名(可以不配) 作用:就是在mapper.xml文件中直接写类名,也可以不用写全路径名。 -->
10 <typeAliases>
11 <package name="cn.e3mall.manager.po" />
12 </typeAliases>
13
14 <!-- 配置mybatis的分页插件PageHelper -->
15 <plugins>
16 <!-- com.github.pagehelper为PageHelper类所在包名 -->
17 <plugin interceptor="com.github.pagehelper.PageHelper">
18 <!-- 设置数据库类型Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
19 <property name="dialect" value="mysql"/>
20 </plugin>
21 </plugins>
22
23 </configuration>

第三步:业务逻辑实现分页功能,我们只需将当前查询的页数page和每页显示的总条数rows传进去,然后Pagehelper已经帮我们分好数据了,只需在web层获取数据即可。

 1 //分页查询商品列表:
2 @Override
3 public DatagridResult itemList(Integer page, Integer rows) {
4 //为了程序的严谨性,判断非空:
5 if(page == null){
6 page = 1; //设置默认当前页
7 }
8 if(page <= 0){
9 page = 1;
10 }
11 if(rows == null){
12 rows = 30; //设置默认每页显示的商品数(因为jsp页面上默认写的就是30条)
13 }
14
15 //1、设置分页信息,包括当前页数和每页显示的总计数
16 PageHelper.startPage(page, rows);
17 //2、执行查询
18 TbItemExample example = new TbItemExample();
19 List<TbItem> list = tbItemMapper.selectByExample(example);
20 //3、获取分页查询后的数据
21 PageInfo<TbItem> pageInfo = new PageInfo<>(list);
22 //4、封装需要返回的分页实体
23 DatagridResult result = new DatagridResult();
24 //设置获取到的总记录数total:
25 result.setTotal(pageInfo.getTotal());
26 //设置数据集合rows:
27 result.setRows(list);
28
29 return result;
30 }

springboot集成PageHelper:

第一步:pom文件还是需要引入依赖

1 <dependency>
2 <groupId>com.github.pagehelper</groupId>
3 <artifactId>pagehelper</artifactId>
4 <version>4.1.6</version>
5 </dependency>

第二步:这次直接是在项目的入口类application.java中直接设置PageHelper插件即可

 1     //配置mybatis的分页插件pageHelper
2 @Bean
3 public PageHelper pageHelper(){
4 PageHelper pageHelper = new PageHelper();
5 Properties properties = new Properties();
6 properties.setProperty("offsetAsPageNum","true");
7 properties.setProperty("rowBoundsWithCount","true");
8 properties.setProperty("reasonable","true");
9 properties.setProperty("dialect","mysql"); //配置mysql数据库的方言
10 pageHelper.setProperties(properties);
11 return pageHelper;
12 }

第三步:同理,使用插件实现分页功能,方式还是一样,只需将当前查询的页数和每页显示的条数穿进去即可,直接源码

这是需要用到的分页实体,各位可以直接享用。

 1 package com.zxz.utils;
2
3 /**
4 * 分页bean
5 */
6
7 import java.util.List;
8
9 public class PageBean<T> {
10 // 当前页
11 private Integer currentPage = 1;
12 // 每页显示的总条数
13 private Integer pageSize = 10;
14 // 总条数
15 private Integer totalNum;
16 // 是否有下一页
17 private Integer isMore;
18 // 总页数
19 private Integer totalPage;
20 // 开始索引
21 private Integer startIndex;
22 // 分页结果
23 private List<T> items;
24
25 public PageBean() {
26 super();
27 }
28
29 public PageBean(Integer currentPage, Integer pageSize, Integer totalNum) {
30 super();
31 this.currentPage = currentPage;
32 this.pageSize = pageSize;
33 this.totalNum = totalNum;
34 this.totalPage = (this.totalNum+this.pageSize-1)/this.pageSize;
35 this.startIndex = (this.currentPage-1)*this.pageSize;
36 this.isMore = this.currentPage >= this.totalPage?0:1;
37 }
38
39 public Integer getCurrentPage() {
40 return currentPage;
41 }
42
43 public void setCurrentPage(Integer currentPage) {
44 this.currentPage = currentPage;
45 }
46
47 public Integer getPageSize() {
48 return pageSize;
49 }
50
51 public void setPageSize(Integer pageSize) {
52 this.pageSize = pageSize;
53 }
54
55 public Integer getTotalNum() {
56 return totalNum;
57 }
58
59 public void setTotalNum(Integer totalNum) {
60 this.totalNum = totalNum;
61 }
62
63 public Integer getIsMore() {
64 return isMore;
65 }
66
67 public void setIsMore(Integer isMore) {
68 this.isMore = isMore;
69 }
70
71 public Integer getTotalPage() {
72 return totalPage;
73 }
74
75 public void setTotalPage(Integer totalPage) {
76 this.totalPage = totalPage;
77 }
78
79 public Integer getStartIndex() {
80 return startIndex;
81 }
82
83 public void setStartIndex(Integer startIndex) {
84 this.startIndex = startIndex;
85 }
86
87 public List<T> getItems() {
88 return items;
89 }
90
91 public void setItems(List<T> items) {
92 this.items = items;
93 }
94 }

分页功能源码(web层和service层)。

 1     @Override
2 public List<Item> findItemByPage(int currentPage,int pageSize) {
3 //设置分页信息,分别是当前页数和每页显示的总记录数【记住:必须在mapper接口中的方法执行之前设置该分页信息】
4 PageHelper.startPage(currentPage, pageSize);
5
6 List<Item> allItems = itemMapper.findAll(); //全部商品
7 int countNums = itemMapper.countItem(); //总记录数
8 PageBean<Item> pageData = new PageBean<>(currentPage, pageSize, countNums);
9 pageData.setItems(allItems);
10 return pageData.getItems();
11 }
 1 /**
2 * 商品分页功能(集成mybatis的分页插件pageHelper实现)
3 *
4 * @param currentPage :当前页数
5 * @param pageSize :每页显示的总记录数
6 * @return
7 */
8 @RequestMapping("/itemsPage")
9 @ResponseBody
10 public List<Item> itemsPage(int currentPage,int pageSize){
11 return itemService.findItemByPage(currentPage, pageSize);
12 }

SpringBoot集成MyBatis的分页插件 PageHelper的更多相关文章

  1. SpringBoot集成MyBatis的分页插件PageHelper(回头草)

    俗话说:好

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

    俗话说:好

  3. SpringBoot集成MyBatis的分页插件PageHelper--详细步骤

    1.pom中添加依赖包 <!--pageHelper基本依赖 --> <dependency> <groupId>com.github.pagehelper< ...

  4. SpringBoot整合MyBatis的分页插件PageHelper

    1.导入依赖(maven) <dependency> <groupId>com.github.pagehelper</groupId> <artifactId ...

  5. Mybatis的分页插件PageHelper

    Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper  文档地址:http://git.oschina. ...

  6. Spring Boot系列教程八: Mybatis使用分页插件PageHelper

    一.前言 上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper.在MyBatis中提供了拦截器接口,我们可以使用PageHelp ...

  7. Spring Boot系列教程十一: Mybatis使用分页插件PageHelper

    一.前言 上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper.在MyBatis中提供了拦截器接口,我们可以使用PageHelp ...

  8. Mybatis之分页插件pagehelper的简单使用

    最近从家里回来之后一直在想着减肥的事情,一个月都没更新博客了,今天下午没睡午觉就想着把mybatis的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目. 一.创建 ...

  9. Spring Boot集成MyBatis与分页插件

    Maven依赖: <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>p ...

随机推荐

  1. Zabbix调优不完全指南(https://www.jianshu.com/p/2d911d55448f)

    从学习搭建zabbix到完成各类监控.调优.二次开发已经过去了两年,期间通过QQ学习群.zabbix官方社区.各个技术博客整理学习了不少关于各种报错的处理方法,现在将常见的一些报错处理方法整理出来分享 ...

  2. dns相关

    一 用于dns, whois相关的网站 1 http://viewdns.info/iphistory 2 http://bgp.he.net/ 3 https://whois.aliyun.com/ ...

  3. spark2.1源码分析2:从SparkPi分析一个job的执行

    从SparkPi的一个行动操作入手,选择Run–Debug SparkPi进入调试: F8:Step Over F7:Step Into 右键Run to Cursor Ctrl+B 查看定义 导航– ...

  4. MacBook Home End

    For the Home command, press down the Fn + Left Arrow keystroke combination. For the End command, pre ...

  5. 来源于知乎专栏:https://zhuanlan.zhihu.com/p/29619457

    1. 校验数字的表达式 1 数字:^[0-9]*$ 2 n位的数字:^\d{n}$ 3 至少n位的数字:^\d{n,}$ 4 m-n位的数字:^\d{m,n}$ 5 零和非零开头的数字:^(0|[1- ...

  6. java面向对象的思想(java三大特性)

    用通俗易懂的语言来理解java面向对象的思想 大家都知道,java是面向对象的编程,掌握面向对象的编程思想是掌握java编程语言的核心,但是很多人在面向对象方面都存在或多或少的误区,有的是刚学完C语言 ...

  7. QTP - 描述性编程

    描述性编程: 1.QTP的描述性编程能够摆脱测试对象库的限制,编写出更为复杂.适应能力更强的测试脚本. 2.即不需要在仓库晨定义,也能访问和操作实际对象. 3.用描述性编程编写的测试脚本在运行时,QT ...

  8. 基础Gan代码解析

    initializer总结: #f.constant_initializer(value) 将变量初始化为给定的常量,初始化一切所提供的值. #tf.random_normal_initializer ...

  9. JHipster简介

    JHipster简介 JHipster或者称Java Hipster,是一个应用代码产生器,能够创建Spring Boot + AngularJS的应用.开源项目地址:JHipster/Github. ...

  10. Python第8天

    zip() 拉链方法 max(字典) 默认比较字典的key,不同类型的数据不能比较,只要可以被for迭代即可 利用zip与max(字典)共同使用 ord() — chr()    ascii码表数字与 ...