SpringBoot集成MyBatis的分页插件 PageHelper
首先说说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的更多相关文章
- SpringBoot集成MyBatis的分页插件PageHelper(回头草)
俗话说:好
- SpringBoot集成MyBatis的分页插件PageHelper
俗话说:好
- SpringBoot集成MyBatis的分页插件PageHelper--详细步骤
1.pom中添加依赖包 <!--pageHelper基本依赖 --> <dependency> <groupId>com.github.pagehelper< ...
- SpringBoot整合MyBatis的分页插件PageHelper
1.导入依赖(maven) <dependency> <groupId>com.github.pagehelper</groupId> <artifactId ...
- Mybatis的分页插件PageHelper
Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper 文档地址:http://git.oschina. ...
- Spring Boot系列教程八: Mybatis使用分页插件PageHelper
一.前言 上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper.在MyBatis中提供了拦截器接口,我们可以使用PageHelp ...
- Spring Boot系列教程十一: Mybatis使用分页插件PageHelper
一.前言 上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper.在MyBatis中提供了拦截器接口,我们可以使用PageHelp ...
- Mybatis之分页插件pagehelper的简单使用
最近从家里回来之后一直在想着减肥的事情,一个月都没更新博客了,今天下午没睡午觉就想着把mybatis的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目. 一.创建 ...
- Spring Boot集成MyBatis与分页插件
Maven依赖: <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>p ...
随机推荐
- 算法——001BitMap(位图)算法
哈希表在查找定位操作上具有O(1)的常量时间,常用于做性能优化,但是内存毕竟是有限的,当数据量太大时用哈希表就会内存溢出了.而考虑对这些大数据进行存盘分批处理又有IO上的开销,性能又不能满足要求.这个 ...
- adb相关指令 笔记
adb相关指令 笔记 1.adb devices 查看物理测试设备或模拟器的相关信息,有三个状态: (1)device 设备已连接到adb服务器上,但该状态并不代表设备已启动完毕可以进行操作: ( ...
- NodeJS-静态服务器
静态服务器 代码 const http = require('http') const chalk = require('chalk') const conf = require('./config/ ...
- Could not create local repository at /home/yizhenn/.m、IDEA倒入maven项目无法导报问题
问题描述: 用自己电脑新搭建环境,用idea倒入项目后发现无法倒入jar包,很少郁闷,折腾了很久,最终发现问题 settings文件中下面这个配置,需要是自己电脑的路径 <localReposi ...
- [python,2019-02-15] 最短回文串
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...
- [java,2019-01-15] word转pdf
word转pdf jar包 <dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j& ...
- 前端人员使用notepad++基本配置
截图说明: 以上截图说的很明确了,基本满足基本的开发. notepad++前端配置版支持Emmet快速完成:支持Explorer目录管理:支持cmd控制台命令:支持MarkDown编辑预览:其他想用的 ...
- Machine.config 文件中节点<machineKey>的强随机生成
Machine.config 文件中节点<machineKey>的强随机生成 <machineKey>这个节允许你设置用于加密数据和创建数字签名的服务器特定的密钥.ASP.NE ...
- (7/24) 插件配置之html文件的打包发布
从前面几节到现在,其实我们的项目结构是有问题的,因为我们直接把index.html文件放到了dist文件夹目录下.这肯定是不正确的,应该放到我们src目录下,然后打包到dist目录下,前面为了学习,才 ...
- SpringBoot整合StringData JPA
目录 SpringBoot整合StringData JPA application.yml User.class UserRepository.java UserController SpringBo ...