mongodb分页查询

1.分页实现类

SpringDataPageable.java

package com.ceair.dao;

import java.io.Serializable;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

public class SpringDataPageable implements Serializable, Pageable {
	private static final long serialVersionUID = 1;
	// 当前页
	private Integer pagenumber = 1;
	// 当前页面条数
	private Integer pagesize = 1000;
	// 排序条件
	private Sort sort;

	// 当前页面
	@Override
	public int getPageNumber() {
		return getPagenumber();
	}

	// 每一页显示的条数
	@Override
	public int getPageSize() {
		return getPagesize();
	}

	// 第二页所需要增加的数量
	@Override
	public int getOffset() {
		return (getPagenumber() - 1) * getPagesize();
	}

	@Override
	public Sort getSort() {
		return sort;
	}

	public Integer getPagenumber() {
		return pagenumber;
	}

	public void setPagenumber(Integer pagenumber) {
		this.pagenumber = pagenumber;
	}

	public Integer getPagesize() {
		return pagesize;
	}

	public void setPagesize(Integer pagesize) {
		this.pagesize = pagesize;
	}

	public void setSort(Sort sort) {
		this.sort = sort;
	}

	@Override
	public Pageable first() {
		return null;
	}

	@Override
	public boolean hasPrevious() {
		return false;
	}

	@Override
	public Pageable next() {
		return null;
	}

	@Override
	public Pageable previousOrFirst() {
		return null;
	}
}

PageDao.java

package com.ceair.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query;

/**
 * Title: 分页 
* Description:
* * @author: wangzs
* @date: 2017年5月10日 */ public interface PageDao { /** * @param pageNum * 开始页 * @param pagesize * 每页大小 * @param query * 查询条件 * @param sort * 排序条件 * @param collections * 集合名字 * @return */ public Page paginationQuery(Integer pageNum, Integer pagesize, Query query, Sort sort, Class collections); }

PageDaoImpl.java

package com.ceair.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

/**
 * Title: 分页 impl 
* Description:
* * @author: wangzs
* @date: 2017年5月10日 */ public class PageDaoImpl implements PageDao { @Autowired private MongoTemplate mongoTemplate; /** * @param pageNum * 开始页 * @param pagesize * 每页大小 * @param query * 查询条件 * @param sort * 排序条件 * @param collections * 集合名字 * @return */ @Override public Page paginationQuery(Integer pageNum, Integer pagesize, Query query, Sort sort, Class collections) { SpringDataPageable pageable = new SpringDataPageable(); // 开始页 pageable.setPagenumber(pageNum); // 每页条数 pageable.setPagesize(pagesize); // 排序 pageable.setSort(sort); // 查询出一共的条数 Long count = mongoTemplate.count(query, collections); // 查询 List list = mongoTemplate.find(query.with(pageable), collections); // 将集合与分页结果封装 Page pagelist = new PageImpl(list, pageable, count); return pagelist; } }

2.测试实体bean

IndividualTemp.java

package com.ceair.bean;

import java.util.Date;

public class IndividualTemp {

	public IndividualTemp() {
		super();
	}

	public IndividualTemp(String partyId, boolean etermToTsdata, boolean tsdataToCmp, Date lastUpdateTime) {
		super();
		this.partyId = partyId;
		this.etermToTsdata = etermToTsdata;
		this.tsdataToCmp = tsdataToCmp;
		this.lastUpdateTime = lastUpdateTime;
	}

	private String partyId;
	private boolean etermToTsdata;// 根据黑屏刷新tsdata数据标识
	private boolean tsdataToCmp;// 根据tsdata刷新cmp行程标识
	private Date lastUpdateTime;

	public String getPartyId() {
		return partyId;
	}

	public void setPartyId(String partyId) {
		this.partyId = partyId;
	}

	public boolean isEtermToTsdata() {
		return etermToTsdata;
	}

	public void setEtermToTsdata(boolean etermToTsdata) {
		this.etermToTsdata = etermToTsdata;
	}

	public boolean isTsdataToCmp() {
		return tsdataToCmp;
	}

	public void setTsdataToCmp(boolean tsdataToCmp) {
		this.tsdataToCmp = tsdataToCmp;
	}

	public Date getLastUpdateTime() {
		return lastUpdateTime;
	}

	public void setLastUpdateTime(Date lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}

	@Override
	public String toString() {
		return "IndividualTemp [partyId=" + partyId + ", etermToTsdata=" + etermToTsdata + ", tsdataToCmp=" + tsdataToCmp + ", lastUpdateTime="
				+ lastUpdateTime + "]";
	}

}

3.测试类

IndividualTempPageTest.java

package com.ceair.data;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.ceair.bean.Individual;
import com.ceair.bean.IndividualTemp;
import com.ceair.dao.PageDao;

/**
 * Title: IndividualTemp分页测试 
* Description:
* * @author: wangzs
* @date: 2017年5月10日 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { // "classpath*:beans/applicationContext-*.xml", // "classpath*:spring/applicationContext-*.xml"// }) public class IndividualTempPageTest { private static final Logger logger = LoggerFactory.getLogger(IndividualTempPageTest.class); @Autowired private PageDao pageDao; @Test public void testCopyIndividual() { Query query = new Query(); List orders = new ArrayList(); // 排序 orders.add(new Order(Direction.ASC, "partyId")); Sort sort = new Sort(orders); for (int i = 1; i < 1500; i++) { Page pages = pageDao.paginationQuery(i, 10, query, sort, IndividualTemp.class); for (IndividualTemp temp : pages) { System.out.println(temp); } System.out.println("page=" + i); logger.info("page=" + i); } } }


你可能感兴趣的:(数据库)