EF分页查询

为什么需要分页?

一、数据方面的原因

大量查询的数据耗时比较严重。

二、增强用户使用体验需求

用户更方便的查询和展示他所需要的数据。

常见分页方式:传统分页方式和下拉式分页方式。

采用传统的分页方式,可以明确的获取数据信息,如有多少条数据,分多少页显示。

采用下拉式分页方式,一般无法获取明确的数据数量相关的信息,但是在分页操作以后,仍然可以看到之前查询的数据。

常见的分页实现方式

使用subList()实现分页。
1
List subList(int fromIndex,int toIndex)

返回列表中指定的fromIndex(包含)和 toIndex (不包括)之间的部分视图。

使用SQL语句实现分页
1
利用数据库自带的分页语法,使用分页语句,获取分页数据(例如mysql数据库使用limit关键字,oracle中使用rownum关键字等)

Mysql

select * from students limit0,10   从第0条开始查,一共查询10条记录。

1
使用hibernate框架进行分页。
1
创建Query或者Criteria对象,查询时,设置firstResult(从第几条开始查)和maxResults(查询几条记录)属性。

String hql =from Student”;

Query q = session.createQuery(hql);

q.setFirstResult(0);

q.setMaxResults(10);

List l = q.list();

实现方式

优点

缺点

使用场景

subList

简单、易用

效率低

无法按需批量获取数据

SQL语句

简单、直接、效率高

数据库兼容性差

不要求数据库兼容

Hibernate框架

面向对象,兼容性强

复杂查询性能低

兼容不同数据库

使用sublist实现分页。
1
创建model层

学生对象 Student类

public class Student implements Serializable{
   
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2448260736229612919L;//序列化id
	private int id;//学生记录的id
	private String stuName;//姓名
	private int age;//年龄
	private int gender;//性别
	private String address;//地址
	public Student(){
   
		super();
	}
	public Student(int id, String stuName, int age, int gender, String address) {
   
		super();
		this.id = id;
		this.stuName = stuName;
		this.age = age;
		this.gender = gender;
		this.address = address;
	}
	/*
	 * 构造函数,将查询到的Map类型的数据构造成学生对象
	 */
	public Student(Map<String,Object> map){
   
		this.id = (int)map.get("id");
		this.stuName = (String)map.get("stu_name");
		this.age = (int)map.get("age");
		this.gender = (int)map.get("gender");
		this.address = (String)map.get("address");
	}
}

分页对象 Pager类

public class Pager<T> implements Serializable{
   
	/**
	 * 序列化id
	 */
	private static final long serialVersionUID = 7569566861340703188L;
	private int pageSize;//每页显示多少条记录
	private int currentPage;//当前第几页数据
	private int totalRecord;//一共多少条记录
	private List<T> dataList;//要显示的数据
	private int totalPage;//总页数
	public Pager() {
   
		super();
	}
	public Pager(int pageSize, int currentPage, int totalRecord,
			int totalPage,List<T> dataList) {
   
		super();
		this.pageSize = pageSize;
		this.currentPage = currentPage;
		this.totalRecord = totalRecord;
		this.totalPage = totalPage;
		this.dataList = dataList;
	}
	public Pager(int pageNum,int pageSize,List<T> sourceList){
   
		if(sourceList.size() ==0 ||sourceList == null){
   
			return;
		}
		//总记录条数
		this.totalRecord = sourceList.size();
		//每页显示多少条记录
		this.pageSize = pageSize;
		//获取总页数
		this.totalPage = this.totalRecord /this.pageSize;
		if(this.totalRecord % this.pageSize != 0){
   
			this.totalPage = this.totalPage +1;
		}
		//当前第几页数据
		if(this.totalPage < pageNum){
   
			this.currentPage = this.totalPage;
		}else{
   
			this.currentPage = pageNum;
		}
		
		//起始索引
		int fromIndex = this.pageSize*(this.currentPage-1);
		//结束索引
		int toIndex;
		if(this.pageSize * this.currentPage >this.totalRecord){
   
			toIndex = this.totalRecord;
		}else{
   
			toIndex = 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值