SpringBoot整合mybatis-pagehelper 商城項目,layui分頁
開發過程中,經常用到分頁,這裡我們是用mybatis-pagehelper工具,進行分頁,用起來很方便。
使用layui等前端框架的時候,也可以使用這個插件。
商城項目怎麼使用
1. 引入分页插件依赖
<!--pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
2. 配置yml
# 分页插件配置
pagehelper:
helperDialect: mysql
supportMethodsArguments: true
3. 使用分页插件,在查询前使用分页插件,原理:统-拦截sql, 为其提供分页功能,serviceImpl方法
/**
*
* @param keywords
* @param sort
* @param page 當前頁
* @param pageSize 每頁顯示多少條數據
* @return
*/
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public PagedGridResult searhItems(String keywords, String sort, Integer page, Integer pageSize) {
Map<String, Object> map = new HashMap<>();
map.put("keywords", keywords);
map.put("sort", sort);
PageHelper.startPage(page, pageSize);
List<SearchItemsVO> list = itemsMapperCustom.searchItems(map);
return setterPagedGrid(list, page);
}
4. 分页数据封装到PagedGridResult. java传给前端,Controller方法
@ApiOperation(value = "搜索商品列表", notes = "搜索商品列表", httpMethod = "GET")
@GetMapping("/search")
public IMOOCJSONResult search(
@ApiParam(name = "keywords", value = "关键字", required = true)
@RequestParam String keywords,
@ApiParam(name = "sort", value = "排序", required = false)
@RequestParam String sort,
@ApiParam(name = "page", value = "查询下一页的第几页", required = false)
@RequestParam Integer page,
@ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
@RequestParam Integer pageSize) {
if (StringUtils.isBlank(keywords)) {
return IMOOCJSONResult.errorMsg(null);
}
if (page == null) {
page = 1;
}
if (pageSize == null) {
pageSize = PAGE_SIZE;
}
PagedGridResult grid = itemService.searhItems(keywords,
sort,
page,
pageSize);
return IMOOCJSONResult.ok(grid);
}
PagedGridResult工具類
/**
*
* @Title: PagedGridResult.java
* @Package com.imooc.utils
* @Description: 用来返回分页Grid的数据格式
* Copyright: Copyright (c) 2019
*/
public class PagedGridResult {
private int page; // 当前页数
private int total; // 总页数
private long records; // 总记录数
private List<?> rows; // 每行显示的内容
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public long getRecords() {
return records;
}
public void setRecords(long records) {
this.records = records;
}
public List<?> getRows() {
return rows;
}
public void setRows(List<?> rows) {
this.rows = rows;
}
}
返回的數據格式如下
layui怎麼使用呢?
假如是用Layui的話,更簡單了。代碼如下所示
controller層
/**
* 查詢所有
* @param equipClassName
* @param equipFactoryName
* @param page 當前頁
* @param limit 每頁需要展示的條數
* @return
*/
@GetMapping("/queryAll")
public LayuiResultUtil queryAll(String collectNumber,
String equipName,
String equipClassName,
String equipFactoryName,
Integer page,
Integer limit) {
List<EquipName> list = equipNameService.queryAll(page,limit,
collectNumber,equipName,
equipClassName,equipFactoryName);
// 查詢當前條件下的數量,用於計算分頁總數,layui會自動計算
Integer count = equipNameService.queryCount(collectNumber,equipName,equipClassName,equipFactoryName);
return LayuiResultUtil.ok(list,count);
}
serviceImpl方法
/**
* 分頁查詢
*
* @param page
* @param pageSize
* @param equipClassName
* @param equipFactoryName
* @return List<EquipName>
*/
@Override
public List<EquipName> queryAll(Integer page,
Integer pageSize,
String collectNumber,
String equipName,
String equipClassName,
String equipFactoryName) {
if (!StringUtils.isBlank(collectNumber)) {
collectNumber = collectNumber.trim();
}
if (!StringUtils.isBlank(equipName)) {
equipName = equipName.trim();
}
if (!StringUtils.isBlank(equipClassName)) {
equipClassName = equipClassName.trim();
}
if (!StringUtils.isBlank(equipFactoryName)) {
equipFactoryName = equipFactoryName.trim();
}
/**
* mybatis-pagehelper mybatis分页插件
* page: 第几页
* pageSize: 每页显示条数
*/
PageHelper.startPage(page, pageSize);
List<EquipName> list = equipNameMapper.queryAll(collectNumber,
equipName,
equipClassName,
equipFactoryName);
return list;
}
查詢數量的方法很簡單,一個簡單的數量查詢就可以了。記得查詢條件要和上面的方法相同
LayuiResultUtil工具類
因為Layui需要知道返回的所有分頁數據,以及當前條件下所有的數量,所以封裝了這個工具類,可以同時返回數據以及數量。
package com.msun.device.anaysis.local.util;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author an
* @version V1.0
* @Title: LAYUIResult.java
* @Description: 自定义响应数据结构
* 本类可提供给 H5/ios/安卓/公众号/小程序 使用
* 前端接受此类数据(json object)后,可自行根据业务去实现相关功能
* <p>
* 0:表示成功
* 500:表示错误,错误信息在msg字段中
* 501:bean验证错误,不管多少个错误都以map形式返回
* 502:拦截器拦截到用户token出错
* 555:异常抛出信息
* 556: 用户qq校验异常
* @Copyright: Copyright (c) 2020
* @Company: www.imooc.com
*/
public class LayuiResultUtil {
/**
* 定义jackson对象
*/
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 响应业务状态
*/
private Integer code;
/**
* 响应消息
*/
private String msg;
/**
* 数量
*/
private Integer count;
/**
* 响应中的数据
*/
private Object data;
/**
* 不使用
*/
@JsonIgnore
private String ok;
/**
* @param code
* @param msg
* @param count
* @param data
* @return
*/
public static LayuiResultUtil build(Integer code, String msg, Integer count, Object data) {
return new LayuiResultUtil(code, msg, count, data);
}
/**
* @param code
* @param msg
* @param count
* @param data
* @return
*/
public static LayuiResultUtil build(Integer code, String msg, Integer count, Object data, String ok) {
return new LayuiResultUtil(code, msg, count, data, ok);
}
/**
* @param data
* @param count
* @return
*/
public static LayuiResultUtil ok(Object data, Integer count) {
return new LayuiResultUtil(data, count);
}
/**
* @return
*/
public static LayuiResultUtil ok() {
return new LayuiResultUtil(null, 0);
}
/**
* @param msg
* @return
*/
public static LayuiResultUtil errorMsg(String msg) {
return new LayuiResultUtil(500, msg, 0, null);
}
/**
* @param data
* @return
*/
public static LayuiResultUtil errorMap(Object data) {
return new LayuiResultUtil(501, "error", 0, data);
}
/**
* @param msg
* @return
*/
public static LayuiResultUtil errorTokenMsg(String msg) {
return new LayuiResultUtil(502, msg, 0, null);
}
/**
* @param msg
* @return
*/
public static LayuiResultUtil errorException(String msg) {
return new LayuiResultUtil(555, msg, 0, null);
}
/**
* @param msg
* @return
*/
public static LayuiResultUtil errorUserQQ(String msg) {
return new LayuiResultUtil(556, msg, 0, null);
}
/**
*
*/
public LayuiResultUtil() {
}
/**
* @param code
* @param msg
* @param count
* @param data
*/
public LayuiResultUtil(Integer code, String msg, Integer count, Object data) {
this.code = code;
this.msg = msg;
this.count = count;
this.data = data;
}
/**
* @param code
* @param msg
* @param count
* @param data
* @param ok
*/
public LayuiResultUtil(Integer code, String msg, Integer count, Object data, String ok) {
this.code = code;
this.msg = msg;
this.count = count;
this.data = data;
this.ok = ok;
}
/**
* @param data
* @param count
*/
public LayuiResultUtil(Object data, Integer count) {
this.code = 0;
this.msg = "OK";
this.count = count;
this.data = data;
}
/**
* @return
*/
public Boolean isOK() {
return this.code == 0;
}
public Integer getcode() {
return code;
}
public void setcode(Integer code) {
this.code = code;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getOk() {
return ok;
}
public void setOk(String ok) {
this.ok = ok;
}
}
返回的數據格式如下