使用Mybatis PageHelper分页插件

1、导入依赖包

<!-- 导入pagehelper分页组件包 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2</version>
</dependency>

2、在Mybatis配置文件中配置PageHelper分页插件

注意:Mybatis配置文件的顺序,顺序不对,会报错。

<plugins>
    <!-- 配置PageHelper分页插件 -->
    <!-- 配置PageInterceptor只适用于pagehelper5.0.0版本以上 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

3、编写后台代码

/**
 * @param pn 页码,默认是第一页
 * @param map 返回页面结果的集合,集合中保存了分页信息和客户查询的信息
 * @return 
 */
@RequestMapping("/emps")
public String getEmps(@RequestParam(value="pn", defaultValue="1")Integer pn, Map<String, Object> map){
    //参数1:第几页,参数2:每页有几条记录
    PageHelper.startPage(pn, 5);  
    List<Employee> emps = employeeService.getAll();
    //参数1:客户信息保存到分页对象中,参数2:连续显示的页码
    PageInfo<Employee> pageInfo = new PageInfo<Employee>(emps, 5);
    map.put("pageInfo", pageInfo);
    return "emp/list";	
}

4、编写前台代码

当前页码:${requestScope.pageInfo.pageNum }
总页码:${requestScope.pageInfo.pages }
总记录数:${requestScope.pageInfo.total }
连续显示的页码:${requestScope.pageInfo.navigatepageNums }
获取分页中的客户信息:${requestScope.pageInfo.list }


PageHelper分页原理实现

package com.itheima.vo;

import java.util.ArrayList;
import java.util.List;

import com.itheima.domain.Product;

public class PageBean<T> {
	//当前页
	private int currentPage;
	//当前页显示的条数
	private int currentCount;
	//总条数
	private int totalCount;
	//总页数
	private int totalPage;
	//每页显示的数据
	private List<T> productList = new ArrayList<T>();
	
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getCurrentCount() {
		return currentCount;
	}
	public void setCurrentCount(int currentCount) {
		this.currentCount = currentCount;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public List<T> getProductList() {
		return productList;
	}
	public void setProductList(List<T> productList) {
		this.productList = productList;
	}
	
}
int currentPage = 1;
int currentCount = 12;
int totalCount = 0;
ProductService service = new ProductService();
PageBean pageBean = new PageBean();
try {
	//1、当前页===页面参数
	pageBean.setCurrentPage(currentPage);
	//2、当前页显示的条数===默认参数
	pageBean.setCurrentCount(currentCount);
	//3、总条数===数据库查询
	pageBean.setTotalCount(totalCount);
	//4、总页数===Math.ceil(x)返回大于等于参数x的最小整数,即对浮点数向上取整
	int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
	pageBean.setTotalPage(totalPage);
	//5、每页显示的数据===公式:索引index = (当前页数-1)*每页显示的条数 
	int index = (currentPage-1)*currentCount;
	List<Product> productList = service.findProductListForPageBean(index,currentCount);
	pageBean.setProductList(productList);
} catch (Exception e) {
	// TODO: handle exception
}

request.setAttribute("pageBean", pageBean);

request.getRequestDispatcher("/product_list.jsp").forward(request, response);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值