之前一篇文章介绍了《SpringBoot+Mybatis+MySql学习》的整合,这一片扩展一下Mybatis的分页插件-Mybatis-PageHelper。
首先,pom文件中加入pagehelper依赖,完整pom代码如下:
4.0.0
com.dalaoyang
springboot_mybatis_pagehelper
0.0.1-SNAPSHOT
jar
springboot_mybatis_pagehelper
springboot_mybatis_pagehelper
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
org.springframework.boot
spring-boot-devtools
runtime
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
配置文件增加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 [email protected]
* @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 [email protected]
* @date 2018/6/22
*/
@Mapper
public interface UserMapper {
@Select("SELECT * FROM USER")
Page 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 [email protected]
* @date 2018/6/22
*/
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
//http://localhost:8888/getUserList?pageNum=1&pageSize=2
@RequestMapping("/getUserList")
public Page getUserList(Integer pageNum, Integer pageSize){
PageHelper.startPage(pageNum, pageSize);
Page 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 extends ArrayList 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 getResult() {
return this;
}
public int getPages() {
return this.pages;
}
public Page setPages(int pages) {
this.pages = pages;
return this;
}
public int getEndRow() {
return this.endRow;
}
public Page setEndRow(int endRow) {
this.endRow = endRow;
return this;
}
public int getPageNum() {
return this.pageNum;
}
public Page setPageNum(int pageNum) {
this.pageNum = this.reasonable != null && this.reasonable && pageNum <= 0 ? 1 : pageNum;
return this;
}
public int getPageSize() {
return this.pageSize;
}
public Page setPageSize(int pageSize) {
this.pageSize = pageSize;
return this;
}
public int getStartRow() {
return this.startRow;
}
public Page 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 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 setPageSizeZero(Boolean pageSizeZero) {
if (pageSizeZero != null) {
this.pageSizeZero = pageSizeZero;
}
return this;
}
public String getOrderBy() {
return this.orderBy;
}
public Page 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 setCount(boolean count) {
this.count = count;
return this;
}
public Page pageNum(int pageNum) {
this.pageNum = this.reasonable != null && this.reasonable && pageNum <= 0 ? 1 : pageNum;
return this;
}
public Page pageSize(int pageSize) {
this.pageSize = pageSize;
this.calculateStartAndEndRow();
return this;
}
public Page count(Boolean count) {
this.count = count;
return this;
}
public Page reasonable(Boolean reasonable) {
this.setReasonable(reasonable);
return this;
}
public Page pageSizeZero(Boolean pageSizeZero) {
this.setPageSizeZero(pageSizeZero);
return this;
}
public Page countColumn(String columnName) {
this.countColumn = columnName;
return this;
}
public PageInfo toPageInfo() {
PageInfo pageInfo = new PageInfo(this);
return pageInfo;
}
public PageSerializable toPageSerializable() {
PageSerializable serializable = new PageSerializable(this);
return serializable;
}
public Page doSelectPage(ISelect select) {
select.doSelect();
return this;
}
public PageInfo doSelectPageInfo(ISelect select) {
select.doSelect();
return this.toPageInfo();
}
public PageSerializable 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