javascript
Spring Data Solr教程:分页
在我的Spring Data Solr教程的較早部分中,我們實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的搜索功能,該功能用于搜索待辦事項(xiàng)的信息。 我們搜索功能的當(dāng)前實(shí)現(xiàn)將所有搜索結(jié)果顯示在一個(gè)頁(yè)面中。 對(duì)于大多數(shù)現(xiàn)實(shí)生活中的應(yīng)用程序而言,這不是可行的解決方案,因?yàn)樗阉鹘Y(jié)果的數(shù)量可能太大,以致搜索功能不再可用。
這篇博客文章描述了如何使用Spring Data Solr對(duì)查詢結(jié)果或搜索功能進(jìn)行分頁(yè),從而為我們提供了解決該問題的方法。
這篇博客文章分為五個(gè)部分:
- 第一部分描述了如何手動(dòng)請(qǐng)求正確的頁(yè)面,并討論了查詢方法的不同返回類型。
- 第二部分描述了如何通過向存儲(chǔ)庫(kù)中添加自定義方法來獲取搜索結(jié)果計(jì)數(shù)。
- 第三部分介紹了如何對(duì)查詢方法的搜索結(jié)果進(jìn)行分頁(yè)。
- 第四部分教我們?nèi)绾螌?duì)動(dòng)態(tài)查詢的搜索結(jié)果進(jìn)行分頁(yè)。
- 第五部分也是最后一部分描述了如何配置和使用一種稱為Web分頁(yè)的技術(shù)。
注意 :這些博客文章提供了其他信息,可幫助我們理解此博客文章中描述的概念:
- 使用Maven運(yùn)行Solr
- Spring Data Solr教程:Solr簡(jiǎn)介
- Spring Data Solr教程:配置
- Spring Data Solr教程CRUD(幾乎)
- Spring Data Solr教程:將自定義方法添加到單個(gè)存儲(chǔ)庫(kù)
- Spring Data Solr教程:動(dòng)態(tài)查詢
- Spring Data Solr教程:排序
讓我們開始吧。
理論紀(jì)要
在開始對(duì)示例應(yīng)用程序進(jìn)行修改之前,我們將簡(jiǎn)要介紹分頁(yè)背后的理論。 本節(jié)分為兩個(gè)小節(jié),如下所述:
- 第一部分描述了如何指定查詢的分頁(yè)選項(xiàng)。
- 第二部分描述查詢方法的不同返回類型。
讓我們繼續(xù)。
指定想要的頁(yè)面
使用的分頁(yè)選項(xiàng)是通過使用實(shí)現(xiàn)Pageable接口的PageRequest類指定的。
以下是典型的分頁(yè)要求:
- 獲取屬于單個(gè)頁(yè)面的查詢結(jié)果。
- 使用單個(gè)字段的值對(duì)查詢結(jié)果進(jìn)行排序時(shí),獲取屬于單個(gè)頁(yè)面的查詢結(jié)果。
- 使用多個(gè)字段的值對(duì)查詢結(jié)果進(jìn)行排序并且不同字段的排序順序相同時(shí),獲取屬于單個(gè)頁(yè)面的查詢結(jié)果。
- 使用多個(gè)字段的值對(duì)查詢結(jié)果進(jìn)行排序并且不同字段的排序順序不同時(shí),獲取屬于單個(gè)頁(yè)面的查詢結(jié)果。
讓我們找出如何創(chuàng)建滿足給定要求的PageRequest對(duì)象。
首先,我們必須創(chuàng)建一個(gè)PageRequest對(duì)象,該對(duì)象指定我們要獲取屬于單個(gè)頁(yè)面的查詢結(jié)果。 我們可以使用以下代碼創(chuàng)建PageRequest對(duì)象:
//Get the query results belonging to the first page when page size is 10. new PageRequest(0, 10)其次,我們必須創(chuàng)建一個(gè)PageRequest對(duì)象,該對(duì)象指定當(dāng)使用單個(gè)字段的值對(duì)查詢結(jié)果進(jìn)行排序時(shí),我們希望獲得屬于單個(gè)頁(yè)面的結(jié)果。 我們可以使用以下代碼創(chuàng)建PageRequest對(duì)象:
/Gets the query results belonging to the first page when page size is 10. //Query results are sorted in descending order by using id field. new PageRequest(0, 10 Sort.Direction.DESC, "id")第三,我們必須創(chuàng)建一個(gè)PageRequest對(duì)象,該對(duì)象指定當(dāng)使用多個(gè)字段對(duì)查詢結(jié)果進(jìn)行排序并且不同字段的排序順序相同時(shí),我們希望獲取屬于單個(gè)頁(yè)面的結(jié)果。 我們可以使用以下代碼創(chuàng)建PageRequest對(duì)象:
//Gets the query results belonging to the first page when page size is 10. //Query results are sorted in descending order by using id and description fields. new PageRequest(0, 10 Sort.Direction.DESC, "id", "description")第四,我們必須創(chuàng)建一個(gè)PageRequest對(duì)象,該對(duì)象指定當(dāng)使用多個(gè)字段對(duì)查詢結(jié)果進(jìn)行排序并且不同字段的排序順序不同時(shí),要獲取屬于單個(gè)頁(yè)面的查詢結(jié)果。 我們可以使用以下代碼創(chuàng)建該對(duì)象:
//Gets the query results belonging to the first page when page size is 10. //Query results are sorted in descending order order by using the description field //and in ascending order by using the id field. Sort sort = new Sort(Sort.Direction.DESC, "description").and(new Sort(Sort.Direction.ASC, "id")) new PageRequest(0, 10, sort);現(xiàn)在我們知道如何創(chuàng)建新的PageRequest對(duì)象。 讓我們繼續(xù)討論查詢方法的不同返回類型。
確定查詢方法的返回類型
當(dāng)查詢方法使用分頁(yè)時(shí),它可以具有兩種返回類型。 這些返回類型將在下面進(jìn)行描述(我們將假定模型類的名稱為TodoDocument ):
- 當(dāng)我們對(duì)分頁(yè)元數(shù)據(jù)感興趣時(shí),查詢方法的返回類型必須為Page <TodoDocument> (獲取有關(guān)Page接口的更多信息,該接口聲明用于獲取分頁(yè)元數(shù)據(jù)的方法)。
- 當(dāng)我們對(duì)分頁(yè)元數(shù)據(jù)不感興趣時(shí)??,查詢方法的返回類型應(yīng)為L(zhǎng)ist <TodoDocument> 。
獲取搜索結(jié)果計(jì)數(shù)
在開始對(duì)查詢的搜索結(jié)果進(jìn)行分頁(yè)之前,我們必須實(shí)現(xiàn)一個(gè)函數(shù),該函數(shù)用于獲取與給定搜索條件匹配的待辦事項(xiàng)條目數(shù)。 此數(shù)字是必需的,以便我們可以在前端實(shí)現(xiàn)分頁(yè)邏輯。
我們可以按照以下步驟實(shí)現(xiàn)此功能:
在以下小節(jié)中將更詳細(xì)地描述這些步驟。
向我們的存儲(chǔ)庫(kù)添加自定義方法
目前,如果不向存儲(chǔ)庫(kù)中添加自定義方法,就無法創(chuàng)建計(jì)數(shù)查詢。 我們可以按照以下步驟進(jìn)行操作:
讓我們繼續(xù)前進(jìn),找出實(shí)現(xiàn)方法。
創(chuàng)建自定義存儲(chǔ)庫(kù)界面
我們可以按照以下步驟創(chuàng)建自定義存儲(chǔ)庫(kù)接口:
CustomTodoDocumentRepository接口的源代碼如下所示:
public interface CustomTodoDocumentRepository {public long count(String searchTerm);//Other methods are omitted }實(shí)施自定義存儲(chǔ)庫(kù)界面
我們可以按照以下步驟實(shí)現(xiàn)自定義存儲(chǔ)庫(kù)接口:
讓我們仔細(xì)看一下count()方法的實(shí)現(xiàn)。 我們可以通過執(zhí)行以下步驟來實(shí)現(xiàn)此方法:
TodoDocumentRepositoryImpl類的源代碼如下所示:
import org.springframework.data.solr.core.SolrTemplate; import org.springframework.data.solr.core.query.Criteria; import org.springframework.data.solr.core.query.SimpleQuery; import org.springframework.stereotype.Repository;import javax.annotation.Resource;@Repository public class TodoDocumentRepositoryImpl implements CustomTodoDocumentRepository {@Resourceprivate SolrTemplate solrTemplate;@Overridepublic long count(String searchTerm) {String[] words = searchTerm.split(" ");Criteria conditions = createSearchConditions(words);SimpleQuery countQuery = new SimpleQuery(conditions);return solrTemplate.count(countQuery);}private Criteria createSearchConditions(String[] words) {Criteria conditions = null;for (String word: words) {if (conditions == null) {conditions = new Criteria("title").contains(word).or(new Criteria("description").contains(word));}else {conditions = conditions.or(new Criteria("title").contains(word)).or(new Criteria("description").contains(word));}}return conditions;}//Other methods are omitted. }修改實(shí)際存儲(chǔ)庫(kù)界面
通過擴(kuò)展CustomTodoRepositoryInterface,我們可以使自定義count()方法對(duì)我們的存儲(chǔ)庫(kù)用戶可見。 TodoDocumentRepository的源代碼如下所示:
public interface TodoDocumentRepository extends CustomTodoRepository, SolrCrudRepository<TodoDocument, String> {//Repository methods are omitted. }使用自定義存儲(chǔ)庫(kù)方法
我們可以按照以下步驟使用創(chuàng)建的存儲(chǔ)庫(kù)方法:
下面將更詳細(xì)地描述這些步驟。
注意 :我們還必須進(jìn)行其他更改,但是由于它們與Spring Data Solr不相關(guān),因此在此不再贅述。
修改服務(wù)接口
我們必須通過向其添加一個(gè)新的countSearchResults()方法來修改TodoIndexService接口。 此方法將使用的搜索詞作為方法參數(shù),并返回搜索結(jié)果計(jì)數(shù)。 TodoIndexService接口的源代碼如下所示:
public interface TodoIndexService {public long countSearchResults(String searchTerm);//Other methods are omitted. }實(shí)施修改后的接口
通過執(zhí)行以下步驟,我們可以實(shí)現(xiàn)countSearchResults()方法:
RepositoryTodoIndexService類的相關(guān)部分如下所示:
import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource; import java.util.ArrayList; import java.util.List;@Service public class RepositoryTodoIndexService implements TodoIndexService {@Resourceprivate TodoDocumentRepository repository;@Overridepublic long countSearchResults(String searchTerm) {return repository.count(searchTerm);}//Other methods are omitted. }分頁(yè)查詢方法的查詢結(jié)果
使用查詢方法創(chuàng)建查詢時(shí),可以按照以下步驟對(duì)查詢結(jié)果進(jìn)行分頁(yè):
讓我們開始吧。
修改存儲(chǔ)庫(kù)界面
我們可以通過向查詢方法添加Pageable參數(shù)來向我們的存儲(chǔ)庫(kù)添加分頁(yè)支持,該方法用于構(gòu)建執(zhí)行的查詢。 讓我們看一下查詢方法的聲明。
從方法名稱查詢生成
通過使用從方法名策略生成查詢來創(chuàng)建執(zhí)行的查詢時(shí),我們必須向TodoDocumentRepository接口的findByTitleContainsOrDescriptionContains()方法添加Pageable參數(shù)。 我們的存儲(chǔ)庫(kù)界面的這些源代碼如下所示:
import org.springframework.data.domain.Pageable; import org.springframework.data.solr.repository.SolrCrudRepository;import java.util.List;public interface TodoDocumentRepository extends CustomTodoDocumentRepository, SolrCrudRepository<TodoDocument, String> {public List<TodoDocument> findByTitleContainsOrDescriptionContains(String title, String description, Pageable page); }命名查詢
使用命名查詢時(shí),我們必須在TodoDocumentRepository接口的findByNamedQuery()方法中添加Pageable參數(shù)。 TodoDocumentRepository接口的源代碼如下所示:
import org.springframework.data.domain.Pageable; import org.springframework.data.solr.repository.Query; import org.springframework.data.solr.repository.SolrCrudRepository;import java.util.List;public interface TodoDocumentRepository extends CustomTodoDocumentRepository, SolrCrudRepository<TodoDocument, String> {@Query(name = "TodoDocument.findByNamedQuery")public List<TodoDocument> findByNamedQuery(String searchTerm, Pageable page); }@Query注釋
使用@Query批注創(chuàng)建執(zhí)行的查詢時(shí),我們必須在TodoDocumentRepository接口的findByQueryAnnotation()方法中添加Pageable參數(shù)。 我們的存儲(chǔ)庫(kù)界面的源代碼如下所示:
import org.springframework.data.domain.Pageable; import org.springframework.data.solr.repository.Query; import org.springframework.data.solr.repository.SolrCrudRepository;import java.util.List;public interface TodoDocumentRepository extends CustomTodoDocumentRepository, SolrCrudRepository<TodoDocument, String> {@Query("title:*?0* OR description:*?0*")public List<TodoDocument> findByQueryAnnotation(String searchTerm, Pageable page); }修改服務(wù)層
我們必須對(duì)示例應(yīng)用程序的服務(wù)層進(jìn)行以下修改:
注意 :我們還必須進(jìn)行其他更改,但是由于它們與Spring Data Solr不相關(guān),因此在此不再贅述。
TodoIndexService接口的源代碼如下所示:
import org.springframework.data.domain.Pageable; import java.util.List;public interface TodoIndexService {public List<TodoDocument> search(String searchTerm, Pageable page);//Other methods are omitted. }我們可以通過對(duì)RepositoryIndexService類的search()方法進(jìn)行以下更改來使用修改后的查詢方法:
讓我們來看一下search()方法的不同實(shí)現(xiàn)。
從方法名稱查詢生成
通過使用從方法名策略生成查詢來構(gòu)建查詢時(shí),可以使用TodoDocumentRepository接口的findByTitleContainsOrDescriptionContains()方法獲取屬于特定頁(yè)面的查詢結(jié)果。
RepositoryTodoIndexService類的相關(guān)部分如下所示:
import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource; import java.util.ArrayList; import java.util.List;@Service public class RepositoryTodoIndexService implements TodoIndexService {@Resourceprivate TodoDocumentRepository repository;@Overridepublic List<TodoDocument> search(String searchTerm, Pageable page) {return repository.findByTitleContainsOrDescriptionContains(searchTerm, searchTerm, page);}//Other methods are omitted }命名查詢
當(dāng)我們使用命名查詢來構(gòu)建執(zhí)行的查詢時(shí),可以使用TodoDocumentRepository接口的findByNamedQuery()方法獲取屬于特定頁(yè)面的搜索結(jié)果。
RepositoryTodoIndexService類的相關(guān)部分如下所示:
import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource; import java.util.ArrayList; import java.util.List;@Service public class RepositoryTodoIndexService implements TodoIndexService {@Resourceprivate TodoDocumentRepository repository;@Overridepublic List<TodoDocument> search(String searchTerm, Pageable page) {return repository.findByNamedQuery(searchTerm, page);}//Other methods are omitted }@Query注釋
使用@Query批注構(gòu)建查詢時(shí),可以通過調(diào)用TodoDocumentRepository接口的findByQueryAnnotation()方法來獲取屬于特定頁(yè)面的搜索結(jié)果。
RepositoryTodoIndexService類的源代碼如下所示:
import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource; import java.util.ArrayList; import java.util.List;@Service public class RepositoryTodoIndexService implements TodoIndexService {@Resourceprivate TodoDocumentRepository repository;@Overridepublic List<TodoDocument> search(String searchTerm, Pageable page) {return repository.findByQueryAnnotation(searchTerm, page);}//Other methods are omitted }分頁(yè)動(dòng)態(tài)查詢的查詢結(jié)果
我們可以按照以下步驟對(duì)動(dòng)態(tài)查詢的查詢結(jié)果進(jìn)行分頁(yè):
在以下小節(jié)中將更詳細(xì)地描述這些步驟。
更改自定義存儲(chǔ)庫(kù)
我們必須向我們的自定義存儲(chǔ)庫(kù)添加分頁(yè)支持。 我們可以按照以下步驟進(jìn)行操作:
讓我們繼續(xù)前進(jìn),找出實(shí)現(xiàn)方法。
更改自定義存儲(chǔ)庫(kù)界面
我們必須在CustomTodoDocumentRepository接口中聲明的search()方法中添加Pageable參數(shù)。 我們的自定義存儲(chǔ)庫(kù)界面的源代碼如下所示:
import org.springframework.data.domain.Pageable;import java.util.List;public interface CustomTodoDocumentRepository {public List<TodoDocument> search(String searchTerm, Pageable page);//Other methods are omitted. }實(shí)施自定義存儲(chǔ)庫(kù)方法
我們的下一步是向search()方法的實(shí)現(xiàn)中添加分頁(yè)支持。 通過執(zhí)行以下步驟,我們可以實(shí)現(xiàn)TodoDocumentRepositoryImpl類的search()方法:
TodoDocumentRepositoryImpl類的源代碼如下所示:
import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.solr.core.SolrTemplate; import org.springframework.data.solr.core.query.Criteria; import org.springframework.data.solr.core.query.SimpleQuery; import org.springframework.stereotype.Repository;import javax.annotation.Resource; import java.util.ArrayList; import java.util.List;@Repository public class TodoDocumentRepositoryImpl implements CustomTodoDocumentRepository {@Resourceprivate SolrTemplate solrTemplate;@Overridepublic List<TodoDocument> search(String searchTerm, Pageable page) {String[] words = searchTerm.split(" ");Criteria conditions = createSearchConditions(words);SimpleQuery search = new SimpleQuery(conditions);search.setPageRequest(page);Page results = solrTemplate.queryForPage(search, TodoDocument.class);return results.getContent();}private Criteria createSearchConditions(String[] words) {Criteria conditions = null;for (String word: words) {if (conditions == null) {conditions = new Criteria("title").contains(word).or(new Criteria("description").contains(word));}else {conditions = conditions.or(new Criteria("title").contains(word)).or(new Criteria("description").contains(word));}}return conditions;}//Other methods are omitted. }使用自定義存儲(chǔ)庫(kù)
在使用修改后的存儲(chǔ)庫(kù)方法之前,我們必須對(duì)示例應(yīng)用程序的服務(wù)層進(jìn)行以下更改:
下面將更詳細(xì)地描述這些步驟。
注意 :我們還必須進(jìn)行其他更改,但是由于它們與Spring Data Solr不相關(guān),因此在此不再贅述。
修改服務(wù)接口
我們必須向TodoIndexService接口的search()方法添加Pageable參數(shù)。 TodoIndexService的源代碼如下所示:
import org.springframework.data.domain.Pageable; import java.util.List;public interface TodoIndexService {public List<TodoDocument> search(String searchTerm, Pageable page);//Other methods are omitted. }實(shí)施服務(wù)接口
當(dāng)使用Spring Data Solr的標(biāo)準(zhǔn)API進(jìn)行構(gòu)建時(shí),我們可以通過調(diào)用自定義存儲(chǔ)庫(kù)的search()方法并將用戶搜索詞和Pageable對(duì)象作為方法參數(shù)來獲取查詢結(jié)果。
RepositoryTodoIndexService類的源代碼如下所示:
import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List;@Service public class RepositoryTodoIndexService implements TodoIndexService {@Resourceprivate TodoDocumentRepository repository;@Overridepublic List<TodoDocument> search(String searchTerm, Pageable page) {return repository.search(searchTerm, page);}//Other methods are omitted. }使用網(wǎng)頁(yè)分頁(yè)
一個(gè)問題仍然沒有答案。 問題是:
在何處指定用于對(duì)查詢的查詢結(jié)果進(jìn)行分頁(yè)的分頁(yè)選項(xiàng)?
我們將使用稱為Web pagination的技術(shù)來創(chuàng)建查詢的分頁(yè)選項(xiàng)。 此技術(shù)基于稱為PageableArgumentResolver的自定義參數(shù)解析器類。 此類解析來自HTTP請(qǐng)求的分頁(yè)信息,并使向控件方法添加Pageable方法參數(shù)成為可能。
本節(jié)描述了如何在示例應(yīng)用程序中配置和使用此技術(shù)。 它分為三個(gè)小節(jié):
- 第一部分描述了如何配置PageableArgumentResolver類。
- 第二小節(jié)介紹了如何使用它。
- 最后一個(gè)小節(jié)討論了Web分頁(yè)的利弊。
讓我們找出如何在示例應(yīng)用程序中使用此技術(shù)。
組態(tài)
本小節(jié)描述了如何配置PageableArgumentResolver類,該類將用于從HTTP請(qǐng)求中提取分頁(yè)選項(xiàng)。 讓我們找出如何通過使用Java配置和XML配置來做到這一點(diǎn)。
Java配置
我們可以通過對(duì)ExampleApplicationContext類進(jìn)行以下更改來添加自定義參數(shù)自變量解析器:
ExampleApplicationContext類的相關(guān)部分如下所示:
import org.springframework.data.web.PageableArgumentResolver; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.mvc.method.annotation.ServletWebArgumentResolverAdapter;import java.util.List;//Annotations are omitted. public class ExampleApplicationContext extends WebMvcConfigurerAdapter {@Overridepublic void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {PageableArgumentResolver pageableArgumentResolver = new PageableArgumentResolver();argumentResolvers.add(new ServletWebArgumentResolverAdapter(pageableArgumentResolver));}//Other methods are omitted. }XML配置
我們可以通過對(duì)exampleApplicationContext.xml文件進(jìn)行以下更改來配置自定義參數(shù)解析程序:
exampleApplicationContext.xml文件的相關(guān)部分如下所示:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"><mvc:annotation-driven><mvc:argument-resolvers><bean id="pageagleArgumentResolver" class="org.springframework.data.web.PageableArgumentResolver"/></mvc:argument-resolvers></mvc:annotation-driven><!-- Configuration is omitted. --> </beans>用法
使用前面介紹的方法之一配置PageableArgumentResolver類后,可以將Pageable方法參數(shù)添加到控制器方法中。 TodoController類的search()方法就是一個(gè)很好的例子。 其源代碼的相關(guān)部分如下所示:
import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*;import javax.annotation.Resource; import java.util.List;@Controller public class TodoController {//Fields are omitted.@RequestMapping(value = "/api/todo/search/{searchTerm}", method = RequestMethod.GET)@ResponseBodypublic List<TodoDTO> search(@PathVariable("searchTerm") String searchTerm, Pageable page) {//Implementation is omitted.}//Other methods are omitted. }但是,將Pageable參數(shù)添加到controller方法還不夠。 我們?nèi)匀槐仨殞⒎猪?yè)選項(xiàng)添加到HTTP請(qǐng)求。 這是通過在請(qǐng)求中添加特殊的請(qǐng)求參數(shù)來完成的。 這些請(qǐng)求參數(shù)描述如下:
- page.page request參數(shù)指定所請(qǐng)求的頁(yè)面。
- page.size請(qǐng)求參數(shù)指定頁(yè)面大小。
- page.sort請(qǐng)求參數(shù)指定用于對(duì)查詢結(jié)果進(jìn)行排序的屬性。
- page.sort.dir請(qǐng)求參數(shù)指定排序順序。
讓我們花點(diǎn)時(shí)間思考一下Web分頁(yè)的利弊。
利弊
在決定在我們的應(yīng)用程序中使用它之前,我們應(yīng)該意識(shí)到Web分頁(yè)的優(yōu)缺點(diǎn)。 讓我們找出它們是什么。
優(yōu)點(diǎn)
使用網(wǎng)頁(yè)分頁(yè)有一個(gè)主要好處:
將分頁(yè)選項(xiàng)從Web層轉(zhuǎn)移到存儲(chǔ)庫(kù)層是一件容易的事。 我們要做的就是配置自定義參數(shù)解析器,將Pageable參數(shù)添加到控制器方法,并使用特定的請(qǐng)求參數(shù)發(fā)送分頁(yè)選項(xiàng)。 這比處理代碼中的分頁(yè)選項(xiàng)和手動(dòng)創(chuàng)建PageRequest對(duì)象要簡(jiǎn)單得多。
缺點(diǎn)
下面介紹了使用Web分頁(yè)的缺點(diǎn):
- Web分頁(yè)在Web層和Spring Data之間創(chuàng)建依賴關(guān)系。 這意味著存儲(chǔ)庫(kù)層的實(shí)現(xiàn)細(xì)節(jié)會(huì)泄漏到應(yīng)用程序的上層。 盡管純粹主義者可能會(huì)聲稱這是一個(gè)巨大的錯(cuò)誤,但我不同意他們的觀點(diǎn)。 我認(rèn)為抽象應(yīng)該使我們的生活更輕松,而不是更艱難。 我們還必須記住, 泄漏抽象定律規(guī)定,所有非平凡抽象在某種程度上都是泄漏的。
- Web分頁(yè)的一個(gè)真正的缺點(diǎn)是,只有在使用單個(gè)字段對(duì)搜索結(jié)果進(jìn)行排序的情況下,我們才能使用它。 盡管對(duì)于大多數(shù)用例來說這是完全可以的,但在某些情況下這會(huì)成為問題。 如果發(fā)生這種情況,我們必須手動(dòng)處理分頁(yè)選項(xiàng)。
摘要
現(xiàn)在,我們已將分頁(yè)搜索結(jié)果添加到示例應(yīng)用程序中。 本教程教會(huì)了我們以下內(nèi)容:
- 我們學(xué)習(xí)了創(chuàng)建新的PageRequest對(duì)象。
- 我們了解到可以從兩個(gè)不同的選項(xiàng)中選擇查詢方法的返回類型。
- 我們學(xué)習(xí)了對(duì)查詢方法和動(dòng)態(tài)查詢的查詢結(jié)果進(jìn)行分頁(yè)。
- 我們知道如何使用網(wǎng)頁(yè)分頁(yè),并且知道它的優(yōu)缺點(diǎn)。
我的Spring Data Solr教程的下一部分描述了如何向所有Spring Data Solr存儲(chǔ)庫(kù)添加自定義方法。
PS此博客文章的示例應(yīng)用程序可在Github上獲得( 查詢方法和動(dòng)態(tài)查詢 )。
翻譯自: https://www.javacodegeeks.com/2013/05/spring-data-solr-tutorial-pagination.html
總結(jié)
以上是生活随笔為你收集整理的Spring Data Solr教程:分页的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 种子经营网上备案APP(种子经营网上备案
- 下一篇: Spring4有条件