- 点赞
- 赞
接下来实现读取数据并在网页上以列表形式显示的功能。首先添加一些测试数据:
List indexQueries = Arrays.asList(
new IndexQueryBuilder().withObject(new Blog(1, "Mary Jones", "Jane is an expert in her field", 80, parseDate("2019-06-21"))).build(),
new IndexQueryBuilder().withObject(new Blog(2, "Jane Smith", "I am starting to get the hang of this...", 0, parseDate("2019-06-20"))).build(),
new IndexQueryBuilder().withObject(new Blog(3, "John Smith", "The Query DSL is really powerful and flexible", 100, parseDate("2019-06-20"))).build(),
new IndexQueryBuilder().withObject(new Blog(4, "Mary Jones", "Still trying this out...", 0, parseDate("2019-06-20"))).build(),
new IndexQueryBuilder().withObject(new Blog(5, "Mary Jones", "However did I manage before Elasticsearch?", 200, parseDate("2019-06-19"))).build(),
new IndexQueryBuilder().withObject(new Blog(6, "Jane Smith", "I like to collect rock albums", 0, parseDate("2019-06-19"))).build(),
new IndexQueryBuilder().withObject(new Blog(7, "Douglas Fir", "I like to build cabinets", 50, parseDate("2019-06-19"))).build(),
new IndexQueryBuilder().withObject(new Blog(8, "John Smith", "I love to go rock climbing", 40, parseDate("2019-06-18"))).build(),
new IndexQueryBuilder().withObject(new Blog(9, "Mary Jones", "I am Mary Jones, welcome to my blog!", 500, parseDate("2019-06-17"))).build(),
new IndexQueryBuilder().withObject(new Blog(10, "Mary Jones", "My first blog entry", 400, parseDate("2019-06-17"))).build()
);
elasticsearchTemplate.bulkIndex(indexQueries);
读取全部数据用matchAllQuery查询,controller代码:
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@RequestMapping("/blogs")
public String query(Model model) {
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchAllQuery())
.build();
Page blogs = elasticsearchTemplate.queryForPage(searchQuery, Blog.class);
model.addAttribute("blogs", blogs.getContent());
return "index";
}
页面代码同使用关系型数据库的方式完全相同,主要代码:
效果如下:
继续添加按日期倒序排序功能,只需增加一行代码:
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchAllQuery())
.withSort(SortBuilders.fieldSort("date").order(SortOrder.DESC))
.build();
创建一个分页辅助类:
import org.springframework.data.domain.Page;
import java.util.List;
public class PageHelper {
private long total;
private List list;
private int pageNum;
private int pageSize;
private int pages;
private int previousPageNum;
private int nextPageNum;
private boolean previousPage;
private boolean nextPage;
private int[] navigatePageNums;
public PageHelper(Page page, int pageNum, int pageSize) {
this.list = page.getContent();
this.total = page.getTotalElements();
this.pageNum = pageNum;
this.pageSize = pageSize;
this.pages = page.getTotalPages();
this.previousPage = false;
this.nextPage = false;
int startNum = 1;
int endNum = pages;
int navigateNums = 10; //导航栏显示数字个数
if (pages <= navigateNums) {
} else if ((pageNum + (navigateNums + 1) / 2) > pages) {
startNum = pages - (navigateNums - 1);
endNum = pages;
} else if (pageNum <= (navigateNums + 1) / 2) {
endNum = navigateNums;
} else {
if (navigateNums % 2 == 0) {
startNum = pageNum - navigateNums / 2;
endNum = pageNum + (navigateNums - 1) / 2;
} else {
startNum = pageNum - navigateNums / 2;
endNum = pageNum + navigateNums / 2;
}
}
this.navigatePageNums = new int[endNum - startNum + 1];
for (int i = startNum; i <= endNum; i++) {
this.navigatePageNums[i - startNum] = i;
}
for (int i = 0; i < this.pages; ++i) {
this.navigatePageNums[i] = i + 1;
}
if (this.pageNum > 1) {
this.previousPage = true;
this.previousPageNum = this.pageNum - 1;
}
if (this.pageNum < this.pages) {
this.nextPage = true;
this.nextPageNum = this.pageNum + 1;
}
}
//getter/setter略
}
controller改造后如下:
@RequestMapping("/blogs")
public String query(Model model, Integer pageNum, Integer pageSize) {
if(pageNum == null || pageNum <= 0)
pageNum = 1;
if(pageSize == null || pageSize <= 0)
pageSize = 5;
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withPageable(new QPageRequest(pageNum - 1, pageSize))
.withQuery(QueryBuilders.matchAllQuery())
.withSort(SortBuilders.fieldSort("date").order(SortOrder.DESC))
.build();
Page blogs = elasticsearchTemplate.queryForPage(searchQuery, Blog.class);
model.addAttribute("blogs", blogs.getContent());
model.addAttribute("pageHelper", new PageHelper<>(blogs, pageNum, pageSize));
return "index";
}
创建一个展示层的分页组件,pagenation.html:
在列表页引用该组件
重新运行后在页面底部显示分页栏:
源码:
链接:https://pan.baidu.com/s/1CaQp5LrcWdLp0UXUPrPbKg
提取码:t5qj