大部分后台管理项目,用的最多的展现方式是列表显示,所以我把列表查询中的分页插件拿出来写一下,方便以后写代码的时候直接复制粘贴。在我们最熟悉的springboot微服务开发中,持久型框架mybatis使用PageHelper还是比较方便的。下面我们来看一下分页插件PageHelper的简单使用方法。
1.首先在pom.xml中pageHelper引入
代码语言:javascript复制<dependencies>
<!-- 分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<!-- 引入mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- 引入mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
2.配置application.yml文件
代码语言:javascript复制#数据库链接
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver #com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/hzc_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: root
tomcat:
initial-size: 5
max-active: 30
#配置mybatis
mybatis:
mapperLocations: classpath:mybatis/mapper*.xml
typeAliasesPackage: com.ten.cn.*.model
#配置pageHelper
pageHelper:
reasonable: "true"
supportMethodsArguments: "true"
returnPageInfo: check
params: count=countSql
3.建立一个分页传输数据的实体,用来传输当前页和分页条数
代码语言:javascript复制package com.ten.ms.cn.sys.bean;
import com.ten.ms.cn.sys.model.User;
/***
* 用户分页实体
* @author:lcj
*/
public class UserPageInvo extends User {
/**
* 页数
*/
private Integer page;
/**
* 条数
*/
private Integer rows;
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getRows() {
return rows;
}
public void setRows(Integer rows) {
this.rows = rows;
}
}
5.创建一个分页配置方法
代码语言:javascript复制package com.ten.ms.cn.common.config;
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
/***
* 分页配置
* @author:lcj
*/
@Configuration
public class PageHelperConfig {
/**
* 配置mybatis的分页插件pageHelper
*
* @return
*/
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "true");
properties.setProperty("dialect", "postgresql");//配置postgresql数据库的方言支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库
pageHelper.setProperties(properties);
return pageHelper;
}
}
6.创建一个返回实体
代码语言:javascript复制package com.ten.ms.cn.common.bean;
/**
* 全局统一json返回值接口
*
* @author user
*/
public class ResultInfo {
/**
* 错误编号
*/
private String errorCode = "0";
private String code = "200";
/**
* 错误信息
*/
private String errorMessage;
/**
* 包装实体
*/
private Object data;
/**
* 返回dataGrid的总页数
*/
private long total;
public ResultInfo() {
}
/**
* 返回分页数据数据
*/
private Object rows;
public ResultInfo(String errorCode, String errorMessage, Object data, long total, Object rows) {
this.errorCode = errorCode;
this.errorMessage = errorMessage;
this.data = data;
this.total = total;
this.rows = rows;
}
public ResultInfo(String errorCode, String errorMessage, Object data) {
this.errorCode = errorCode;
this.errorMessage = errorMessage;
this.data = data;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
if ((int) total == 0) {
total = 1;
}
this.total = total;
}
public Object getRows() {
return rows;
}
public void setRows(Object rows) {
this.rows = rows;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
7.简单的分页实例
代码语言:javascript复制@RequestMapping(value = "/getUserList", method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public ResultInfo getUserList(@RequestBody SysUserPageInvo sysUserPageInvo) {
// 定义传参
int page = sysUserPageInvo != null && sysUserPageInvo.getPage() != null ? sysUserPageInvo.getPage() : 0;// 当前页数
int rows = sysUserPageInvo != null && sysUserPageInvo.getRows() != null ? sysUserPageInvo.getRows() : 10;//一页多少条数据
ResultInfo result = new ResultInfo();
PageHelper.startPage(page, rows);
// 获取数据
List<SysUserOut> list = this.sysUserService.getUserList(sysUserPageInvo);
PageInfo pageInfo = new PageInfo(list);
// 当前分页数据
Page pages = (Page) list;
// 总共多少条数据
result.setTotal(pageInfo.getTotal());
result.setRows(pages);
return result;
}
总结
PageHelper的使用大概就是这样返回给前台的数据是当前分页的list列表和数据的总数。同样如果后台返回固定的json格式数据,前台自动加载到列表控件中。后续我会分享更多的技术相关的内容,请大家多多关注。