前言:
我们在项目中经常使用pagehelper进行分页,那么我们pagehelper到底是怎么用的呢?现在就项目中用到的pagehelper进行以下介绍。
内容:
1、 首先 在pom文件中引入依赖
<!--添加pageHelper分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.0-beta</version>
</dependency>
2、在spring-mybaits中配置 <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:/mapper/*.xml"></property>
<!-- 分页查询(mybatis 3.2.8以上版本) -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>
3、分页实现的代码
package 你自己的包名;
public class PaginationContext {
// 定义threadLocal变量:pageNum和pageSize
// 通过Filter 赋值
private static ThreadLocal<Integer> pageNum = new ThreadLocal<Integer>(); // 第几页
private static ThreadLocal<Integer> pageSize = new ThreadLocal<Integer>(); // 每页记录条数
/*
* pageNum :get、set、remove
*/
public static int getPageNum() {
Integer pn = pageNum.get();
if (pn == null) {
return 1;
}
return pn;
}
public static void setPageNum(int pageNumValue) {
pageNum.set(pageNumValue);
}
public static void removePageNum() {
pageNum.remove();
}
/*
* pageSize :get、set、remove
*/
public static int getPageSize() {
Integer ps = pageSize.get();
if (ps == null) {
return 1;
}
return ps;
}
public static void setPageSize(int pageSizeValue) {
pageSize.set(pageSizeValue);
}
public static void removePageSize() {
pageSize.remove();
}
}
4、设置一个pagebean用来存放取到页数,大小等信息package com.cn.echuxianshengshop.common;
import com.github.pagehelper.Page;
import java.io.Serializable;
import java.util.List;
public class PageBean<T> implements Serializable {
private static final long serialVersionUID = 8656597559014685635L;
private long total; //总记录数
private List<T> list; //结果集
private int pageNum; // 第几页
private int pageSize; // 每页记录数
private int pages; // 总页数
private int size; // 当前页的数量 <= pageSize,该属性来自ArrayList的size属性
/**
* 包装Page对象,因为直接返回Page对象,在JSON处理以及其他情况下会被当成List来处理,
* 而出现一些问题。
*
* @param list page结果
*/
public PageBean(List<T> list) {
if (list instanceof Page) {
Page<T> page = (Page<T>) list;
this.pageNum = page.getPageNum();
this.pageSize = page.getPageSize();
this.total = page.getTotal();
this.pages = page.getPages();
this.list = page;
this.size = page.size();
}
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
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 int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
5、在代码中的使用
@Override
public PageBean<TProductmanager> findAllProduct(TProductmanagerExample example) {
PageHelper.startPage(PaginationContext.getPageNum(), PaginationContext.getPageSize());
List<TProductmanager> productmanagers = tProductmanagerDao.selectByExample(example);
return new PageBean(productmanagers);
}
总结:
pagehelper我们经常使用。至此pagehelper的使用分享完毕。献给各位小主。以后不用再发愁啦~