springboot +mybatis+pageHelper 整合

  • Post author:
  • Post category:其他


目前Pagehelper插件支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页,不同数据库只需要修改helperDialect就行。

1.pom.xml文件配置jar包

        <!--分页插件-->
         <dependency>
		    <groupId>com.github.pagehelper</groupId>
		    <artifactId>pagehelper-spring-boot-starter</artifactId>
		    <version>1.2.3</version>
		</dependency>

2.application.properties 文件配置

#分页属性
pagehelper.reasonable=false
pagehelper.supportMethodsArguments=true
pagehelper.returnPageInfo=check
pagehelper.params=count=countSql
pagehelper.autoRuntimeDialect=true

autoRuntimeDialect 自动获取当前运行的数据库类型

3.  封装pagevo 实体类,包含分页所需所有属性

package com.citydo.vo;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import com.github.pagehelper.Page;

public class PageVo<T> implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// 当前页
	private int pageNum = 1;
	// 每页的数量
	private int pageSize = 5;
	// 总记录数
	private long total;
	// 总页数
	private int pages;
	// 结果集
	private List<T> list;
	// 是否为第一页
	private boolean isFirstPage = false;
	// 是否为最后一页
	private boolean isLastPage = false;

	public PageVo() {
	}

	/**
	 * 包装Page对象
	 *
	 * @param list
	 */
	public PageVo(List<T> list) {
		if (list instanceof Page) {
			Page page = (Page) list;
			this.pageNum = page.getPageNum();
			this.pageSize = page.getPageSize();

			this.pages = page.getPages();
			this.list = page;
			this.total = page.getTotal();
		} else if (list instanceof Collection) {
			this.pageNum = 1;
			this.pageSize = list.size();

			this.pages = 1;
			this.list = list;
			this.total = list.size();
		}
		if (list instanceof Collection) {
			// 判断页面边界
			judgePageBoudary();
		}
	}

	/**
	 * 判定页面边界
	 */
	private void judgePageBoudary() {
		isFirstPage = pageNum == 1;
		isLastPage = pageNum == pages;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public boolean isIsFirstPage() {
		return isFirstPage;
	}

	public void setIsFirstPage(boolean isFirstPage) {
		this.isFirstPage = isFirstPage;
	}

	public boolean isIsLastPage() {
		return isLastPage;
	}

	public void setIsLastPage(boolean isLastPage) {
		this.isLastPage = isLastPage;
	}

	@Override
	public String toString() {
		final StringBuffer sb = new StringBuffer("PageVo{");
		sb.append("pageNum=").append(pageNum);
		sb.append(", pageSize=").append(pageSize);
		sb.append(", total=").append(total);
		sb.append(", pages=").append(pages);
		sb.append(", list=").append(list);
		sb.append(", isFirstPage=").append(isFirstPage);
		sb.append(", isLastPage=").append(isLastPage);
		sb.append(", navigatepageNums=");
		sb.append('}');
		return sb.toString();
	}

}

4.业务实体对象继承pagevo对象

public class NewsInfoTableVo extends PageVo<Object>

5.分页传入当前页数,每页的size

获取Page对象,再转成PageVO对象

/**
	 * 查询列表
	 */
	@Override
	public JsonResult queryNewsInfoTable(NewsInfoTableVo vo) {
		String serverUrl = "";
		PageHelper.startPage(vo.getPageNum(), vo.getPageSize());

		Page<NewsInfoTableVo> infoTable = newsInfoTableMapper.queryNewsInfoTable(vo);
		PageVo<NewsInfoTableVo> pageVo = new PageVo<>(infoTable);
		List<NewsInfoTableVo> list = pageVo.getList();
		for (NewsInfoTableVo newsInfoTableVo : list) {
			SystemFileTableEntity tableEntity = newsInfoTableMapper.getFilePathByNewsId(newsInfoTableVo.getNewsId());
			if (tableEntity != null) {
				String fileSavename = tableEntity.getFileSavename();
				serverUrl = fileReadPath + "/" + fileSavename;
			}
			newsInfoTableVo.setFileUrl(serverUrl);
		}
		return JsonResult.success("查询成功", pageVo);
	}



版权声明:本文为yss1019原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。