分页对象,我们看一下常见的分页
csdn博客中的分页
博客园博客的分页
开源中国博客分页
基本的分页也就是下面几个字段
当前页(currentPage)
每页显示的记录数(pageSize)
总记录数(totalCount)
扫描二维码关注公众号,回复:
2812045 查看本文章
返回的数据(datas)
分页对象,参考http://www.devnote.cn/article/42.html
import java.util.List;
import java.util.Map;
/**
* 分页对象
*
* @author 程高伟
* @time 2017年5月21日下午8:35:01
*/
public class Page<T> {
/** 每页显示记录数默认为10条 */
public static final int DEFAULT_PAGE_SIZE = 10;
/** 当前页码, 从1开始计 */
private int currentPage;
/** 每页记录数 */
private int pageSize;
/** 总记录数 */
private long totalCount;
/** 查询条件 */
private Map<String, Object> conditions;
/** 当前页数据 */
private List<?> datas;
public Page() {
// 默认构造函数
currentPage = 1;
pageSize = DEFAULT_PAGE_SIZE;
}
/** 获取当前页码 */
public int getCurrentPage() {
return currentPage;
}
/** 设置当前页码 */
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/** 获取每页显示记录数 */
public int getPageSize() {
return pageSize;
}
/** 设置每页显示记录数 */
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/** 获取查询参数 */
public Map<String, Object> getConditions() {
return conditions;
}
/** 设置查询参数 */
public void setConditions(Map<String, Object> conditions) {
this.conditions = conditions;
}
/** 获取当前页数据 */
public List<?> getDatas() {
return datas;
}
/** 设置当前页数据 */
public void setDatas(List<?> datas) {
this.datas = datas;
}
/** 获取总记录数 */
public long getTotalCount() {
return totalCount;
}
/** 设置总记录数 */
public void setTotalCount(long totalCount) {
this.totalCount = totalCount;
}
/** 获取总页数 */
public long getTotalPages() {
if (datas == null || datas.isEmpty())
return 0;
long totalPages = totalCount / pageSize;
if (totalCount % pageSize != 0) {
totalPages++;
}
return totalPages;
}
/** 获取从第几条数据开始查询 */
public long getStart() {
return (currentPage - 1) * pageSize;
}
/** 判断是否还有前一页 */
public boolean getHasPrevious() {
return currentPage == 1 ? false : true;
}
/** 判断是否还有后一页 */
public boolean getHasNext() {
return (getTotalPages() != 0 && getTotalPages() != currentPage) ? true : false;
}
}
分页查询方法
public static <T> Page<T> getByPage(Page<T> page) {
SolrQuery query = new SolrQuery();
// 发布时间降序排列
query.setSort("createTime", ORDER.desc);
// 开始页
query.setStart((int) page.getStart());
// 每页显示条数
query.setRows(page.getPageSize());
if (StringUtils.isBlank(page.getConditions().get("keywords").toString())) {
page.getConditions().put("keywords", "*");
query.setHighlight(false);// 开启高亮组件
}else{
// 设置高亮
query.setHighlight(true);// 开启高亮组件
query.addHighlightField("title");// 高亮字段
query.addHighlightField("content");// 高亮字段
query.setHighlightSimplePre("<font color='red'>");// 标记,高亮关键字前缀
query.setHighlightSimplePost("</font>");// 后缀
query.setHighlight(true).setHighlightSnippets(1);
// 获取高亮分片数,一般搜索词可能分布在文章中的不同位置,其所在一定长度的语句即为一个片段,默认为1,但根据业务需要有时候需要多取出几个分片。
query.setHighlightFragsize(100);// 每个分片的最大长度,默认为100。适当设置此值,如果太小,高亮的标题可能会显不全;设置太大,摘要可能会太长。
}
// 关键字
String keywords =page.getConditions().get("keywords").toString();
query.setQuery("title:" + keywords + "or content:" + keywords + "or author:" + keywords);
try {
QueryResponse response = client.query(query);
List<ArticleSolr> articleList = response.getBeans(ArticleSolr.class);
SolrDocumentList docs = response.getResults();
if(query.getHighlight()){
// 获取所有高亮的字段
Map<String, Map<String, List<String>>> highlightMap = response.getHighlighting();
for (int i = 0; i < articleList.size(); ++i) {
String id = articleList.get(i).getId();
if (highlightMap.get(id) != null && highlightMap.get(id).get("title") != null) {
articleList.get(i).setTitle(highlightMap.get(id).get("title").get(0));
}
if (highlightMap.get(id) != null && highlightMap.get(id).get("content") != null) {
articleList.get(i).setContent(highlightMap.get(id).get("content").get(0));
}
}
}
page.setDatas(articleList);
page.setTotalCount(docs.getNumFound());
} catch (Exception e) {
logger.error("从solr根据Page查询分页文档时遇到错误", e);
}
return page;
}
分页测试用例
@Test
public void testPage() {
Page<?> page = new Page<>();
Map<String, Object> conditions = new HashMap<String, Object>();
conditions.put("keywords", "博客");// 指定关键字keywords
page.setConditions(conditions);
page.setPageSize(10);
page.setCurrentPage(19);
Page<?> result = SolrUtil.getByPage(page);
System.out.println("总记录数" + result.getTotalCount());
System.out.println("每页显示记录数" + result.getPageSize());
System.out.println("总页数" + result.getTotalPages());
System.out.println("当前页码" + result.getCurrentPage());
System.out.println("从第几条数据开始查询" + result.getStart());
System.out.println("当前页数据" + result.getDatas().size());
System.out.println("有上一页" + result.getHasNext());
System.out.println("有下一页" + result.getHasNext());
Iterator<?> iter = result.getDatas().iterator();
while (iter.hasNext()) {
ArticleSolr article = (ArticleSolr) iter.next();
System.out.println(article);
}
}