前言

之前一篇文章介绍了《SpringBoot+Mybatis+MySql学习》的整合,这一片扩展一下Mybatis的分页插件-Mybatis-PageHelper。

新建项目

首先,pom文件中加入pagehelper依赖,完整pom代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.dalaoyang</groupId>
<artifactId>springboot_mybatis_pagehelper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springboot_mybatis_pagehelper</name>
<description>springboot_mybatis_pagehelper</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

配置文件增加PageHelper的配置,由于demo很简单,只用到了分页,所以没有增加其他配置,只设置了分页方言,完整代码如下:

##端口号
server.port=8888 ##日志级别
logging.level.com.dalaoyang.dao.UserMapper=debug ##数据库url
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
##数据库用户名
spring.datasource.username=root
##数据库密码
spring.datasource.password=root
##数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver #pagehelper分页插件配置
pagehelper.helperDialect=mysql

实体类User代码如下:

package com.dalaoyang.entity;

import org.apache.ibatis.type.Alias;

/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.entity
* @email 397600342@qq.com
* @date 2018/6/22
*/
@Alias("user")
public class User { private int id;
private String user_name;
private String user_password; public User(String user_name, String user_password) {
this.user_name = user_name;
this.user_password = user_password;
} public User(int id, String user_name, String user_password) {
this.id = id;
this.user_name = user_name;
this.user_password = user_password;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUser_name() {
return user_name;
} public void setUser_name(String user_name) {
this.user_name = user_name;
} public String getUser_password() {
return user_password;
} public void setUser_password(String user_password) {
this.user_password = user_password;
}
}

启动类代码如下:

package com.dalaoyang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class SpringbootMybatisPagehelperApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisPagehelperApplication.class, args);
}
}

新建一个UserMapper,之前介绍的整合mybatis是使用的mapper方式,本文选择使用注解方式,代码如下:

package com.dalaoyang.dao;

import com.dalaoyang.entity.User;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; /**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.dao
* @email 397600342@qq.com
* @date 2018/6/22
*/
@Mapper
public interface UserMapper { @Select("SELECT * FROM USER")
Page<User> getUserList();
}

还是一如既往的使用controller作为测试,代码如下:

package com.dalaoyang.controller;

import com.dalaoyang.dao.UserMapper;
import com.dalaoyang.entity.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.controller
* @email 397600342@qq.com
* @date 2018/6/22
*/
@RestController
public class UserController { @Autowired
UserMapper userMapper; //http://localhost:8888/getUserList?pageNum=1&pageSize=2
@RequestMapping("/getUserList")
public Page<User> getUserList(Integer pageNum, Integer pageSize){
PageHelper.startPage(pageNum, pageSize);
Page<User> userList= userMapper.getUserList();
return userList;
}
}

到这里项目就完全创建完成了。

测试

浏览器访问http://localhost:8888/getUserList?pageNum=1&pageSize=2,结果如下:

然后查看控制台如下:

可以看到sql已经进行分页了。

然后回头看controller的方法,返回的Page对象中包含了很多关于分页的参数等数据,下面是Page的代码,具体使用可以查看一下:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package com.github.pagehelper; import java.io.Closeable;
import java.util.ArrayList;
import java.util.List; public class Page<E> extends ArrayList<E> implements Closeable {
private static final long serialVersionUID = 1L;
private int pageNum;
private int pageSize;
private int startRow;
private int endRow;
private long total;
private int pages;
private boolean count;
private Boolean reasonable;
private Boolean pageSizeZero;
private String countColumn;
private String orderBy;
private boolean orderByOnly; public Page() {
this.count = true;
} public Page(int pageNum, int pageSize) {
this(pageNum, pageSize, true, (Boolean)null);
} public Page(int pageNum, int pageSize, boolean count) {
this(pageNum, pageSize, count, (Boolean)null);
} private Page(int pageNum, int pageSize, boolean count, Boolean reasonable) {
super(0);
this.count = true;
if (pageNum == 1 && pageSize == 2147483647) {
this.pageSizeZero = true;
pageSize = 0;
} this.pageNum = pageNum;
this.pageSize = pageSize;
this.count = count;
this.calculateStartAndEndRow();
this.setReasonable(reasonable);
} public Page(int[] rowBounds, boolean count) {
super(0);
this.count = true;
if (rowBounds[0] == 0 && rowBounds[1] == 2147483647) {
this.pageSizeZero = true;
this.pageSize = 0;
} else {
this.pageSize = rowBounds[1];
this.pageNum = rowBounds[1] != 0 ? (int)Math.ceil(((double)rowBounds[0] + (double)rowBounds[1]) / (double)rowBounds[1]) : 0;
} this.startRow = rowBounds[0];
this.count = count;
this.endRow = this.startRow + rowBounds[1];
} public List<E> getResult() {
return this;
} public int getPages() {
return this.pages;
} public Page<E> setPages(int pages) {
this.pages = pages;
return this;
} public int getEndRow() {
return this.endRow;
} public Page<E> setEndRow(int endRow) {
this.endRow = endRow;
return this;
} public int getPageNum() {
return this.pageNum;
} public Page<E> setPageNum(int pageNum) {
this.pageNum = this.reasonable != null && this.reasonable && pageNum <= 0 ? 1 : pageNum;
return this;
} public int getPageSize() {
return this.pageSize;
} public Page<E> setPageSize(int pageSize) {
this.pageSize = pageSize;
return this;
} public int getStartRow() {
return this.startRow;
} public Page<E> setStartRow(int startRow) {
this.startRow = startRow;
return this;
} public long getTotal() {
return this.total;
} public void setTotal(long total) {
this.total = total;
if (total == -1L) {
this.pages = 1;
} else {
if (this.pageSize > 0) {
this.pages = (int)(total / (long)this.pageSize + (long)(total % (long)this.pageSize == 0L ? 0 : 1));
} else {
this.pages = 0;
} if (this.reasonable != null && this.reasonable && this.pageNum > this.pages) {
this.pageNum = this.pages;
this.calculateStartAndEndRow();
} }
} public Boolean getReasonable() {
return this.reasonable;
} public Page<E> setReasonable(Boolean reasonable) {
if (reasonable == null) {
return this;
} else {
this.reasonable = reasonable;
if (this.reasonable && this.pageNum <= 0) {
this.pageNum = 1;
this.calculateStartAndEndRow();
} return this;
}
} public Boolean getPageSizeZero() {
return this.pageSizeZero;
} public Page<E> setPageSizeZero(Boolean pageSizeZero) {
if (pageSizeZero != null) {
this.pageSizeZero = pageSizeZero;
} return this;
} public String getOrderBy() {
return this.orderBy;
} public <E> Page<E> setOrderBy(String orderBy) {
this.orderBy = orderBy;
return this;
} public boolean isOrderByOnly() {
return this.orderByOnly;
} public void setOrderByOnly(boolean orderByOnly) {
this.orderByOnly = orderByOnly;
} private void calculateStartAndEndRow() {
this.startRow = this.pageNum > 0 ? (this.pageNum - 1) * this.pageSize : 0;
this.endRow = this.startRow + this.pageSize * (this.pageNum > 0 ? 1 : 0);
} public boolean isCount() {
return this.count;
} public Page<E> setCount(boolean count) {
this.count = count;
return this;
} public Page<E> pageNum(int pageNum) {
this.pageNum = this.reasonable != null && this.reasonable && pageNum <= 0 ? 1 : pageNum;
return this;
} public Page<E> pageSize(int pageSize) {
this.pageSize = pageSize;
this.calculateStartAndEndRow();
return this;
} public Page<E> count(Boolean count) {
this.count = count;
return this;
} public Page<E> reasonable(Boolean reasonable) {
this.setReasonable(reasonable);
return this;
} public Page<E> pageSizeZero(Boolean pageSizeZero) {
this.setPageSizeZero(pageSizeZero);
return this;
} public Page<E> countColumn(String columnName) {
this.countColumn = columnName;
return this;
} public PageInfo<E> toPageInfo() {
PageInfo<E> pageInfo = new PageInfo(this);
return pageInfo;
} public PageSerializable<E> toPageSerializable() {
PageSerializable<E> serializable = new PageSerializable(this);
return serializable;
} public <E> Page<E> doSelectPage(ISelect select) {
select.doSelect();
return this;
} public <E> PageInfo<E> doSelectPageInfo(ISelect select) {
select.doSelect();
return this.toPageInfo();
} public <E> PageSerializable<E> doSelectPageSerializable(ISelect select) {
select.doSelect();
return this.toPageSerializable();
} public long doCount(ISelect select) {
this.pageSizeZero = true;
this.pageSize = 0;
select.doSelect();
return this.total;
} public String getCountColumn() {
return this.countColumn;
} public void setCountColumn(String countColumn) {
this.countColumn = countColumn;
} public String toString() {
return "Page{count=" + this.count + ", pageNum=" + this.pageNum + ", pageSize=" + this.pageSize + ", startRow=" + this.startRow + ", endRow=" + this.endRow + ", total=" + this.total + ", pages=" + this.pages + ", reasonable=" + this.reasonable + ", pageSizeZero=" + this.pageSizeZero + '}' + super.toString();
} public void close() {
PageHelper.clearPage();
}
}

其他

关于更多Mybatis-PageHelper配置及介绍可以查看下面网站:

https://gitee.com/free/Mybatis_PageHelper

https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md

源码下载 :大老杨码云

个人网站:https://www.dalaoyang.cn

关注作者公众号

SpringBoot使用Mybatis-PageHelper的更多相关文章

  1. springboot整合mybatis+pageHelper

    springboot整合mybatis+pageHelper 〇.搭建sporingboot环境,已经整合mybatis环境,本篇主要是添加pageHelper工具 一.添加依赖 <!-- 分页 ...

  2. SpringBoot 整合Mybatis + PageHelper 实现分页

    前言: 现在公司大多数都实现了前后端分离,前端使用Vue.React.AngularJS 等框架,不用完全依赖后端.但是如果对于比较小型的项目,没必要前后端分离,而SpringBoot也基本抛弃了Js ...

  3. Springboot集成Mybatis+PageHelper

    1.Springboot项目引入mysql和mybatis的依赖: <dependency> <groupId>org.mybatis.spring.boot</grou ...

  4. springboot+mybatis+pagehelper

    springboot+mybatis+pagehelper整合 springboot   版本2.1.2.RELEASE mybatis  版本3.5 pagehelper 版本5.18 支持在map ...

  5. SpringBoot+Mybatis+PageHelper实现分页

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

  6. SpringBoot集成Mybatis并具有分页功能PageHelper

    SpringBoot集成Mybatis并具有分页功能PageHelper   环境:IDEA编译工具   第一步:生成测试的数据库表和数据   SET FOREIGN_KEY_CHECKS=0;   ...

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

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

  8. SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例

    SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例 这是一个简单的SpringBoot整合实例 这里是项目的结构目录 首先是pom.xml ...

  9. Springboot+Mybatis+Pagehelper+Aop动态配置Oracle、Mysql数据源

      本文链接:https://blog.csdn.net/wjy511295494/article/details/78825890 Springboot+Mybatis+Pagehelper+Aop ...

  10. 后端分页神器,mybatis pagehelper 在SSM与springboot项目中的使用

    mybatis pagehelper想必大家都耳熟能详了,是java后端用于做分页查询时一款非常好用的分页插件,同时也被人们称为mybatis三剑客之一,下面 就给大家讲讲如何在SSM项目和sprin ...

随机推荐

  1. algorithm的基本注意事项

    find(): 返还指向该迭代器的指针,找不到返还last:lnlt find(lnlt first,lnlt last ,const T&val);范围[first,last); list: ...

  2. LeetCode(83): 删除排序链表中的重复元素

    Easy! 题目描述: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: 输入: 1->1-&g ...

  3. The import util cannot be resolved

    代码: 明显的错误: 应改成 import java.util.*; 没有理解java的基本概念

  4. jquery checkbox勾选/取消勾选只能操作一次的诡异问题

    第一次执行,没问题,但第二次执行就有问题了,选择不了 解决办法:把attr()换成prop() $("#CheckedAll").click(function () { if ($ ...

  5. AI学习吧-结算中心

    结算中心流程 在结算中心中,主要是对用户添加到购物车商品的结算,由于用户可能添加了多个课程,但是,结算时会选择性的进行支付.在结算时会选中课程id,和对应的价格策略.在后台,首先会对用户进行校验,验证 ...

  6. SQL Server等待

    等待大概分为3类:资源等待.队列等待.外部等待 过滤掉系统相关的等待类型的语句.(查看常用的等待信息) SELECT wait_type , signal_wait_time_ms , wait_ti ...

  7. RazorEngine.Templating MVC中View当模板

    最近在做一个生成JSON的功能,比较笨的办法就是把需要的数据拆分开,保存到数据库,在从数据库中取出来进行拼接.这种方法比较笨,代码就不贴了. 需要注意拼接的时的转义字符: "\"s ...

  8. Android.os.SystemClock

    https://www.linuxidc.com/Linux/2011-11/48325p2.htm 文档中对System.currentTimeMillis()进行了相应的描述,就是说它不适合用在需 ...

  9. [转] Javascript 原型链

    1. 类 在C或者Java里,int a;定义了一个int类型的变量a.其中int是类型的名字,a是具体的变量. Javascript 模仿自 Java, 有一部分面向对象编程的部分.在面向对象的编程 ...

  10. cookie实现刷新不变化树形菜单

    通过设置cookie来保存树形菜单的状态,在页面加载时重新读取cookie来设置菜单. 菜单的HTML结构: <div class="treemenu"> <ul ...