亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring-Data-JPA注釋的setMaxResults?

Spring-Data-JPA注釋的setMaxResults?

www說 2019-08-08 11:11:26
Spring-Data-JPA注釋的setMaxResults?我正在嘗試將Spring-Data-JPA合并到我的項目中。令我困惑的一件事是如何通過注釋實現setMaxResults(n)?例如,我的代碼:public interface UserRepository extends CrudRepository<User , Long>{   @Query(value="From User u where u.otherObj = ?1 ")   public User findByOhterObj(OtherObj otherObj);}我只需one (and only one)要從otherObj 返回User,但我找不到注釋maxResults的方法。有人可以給我一個暗示嗎?(mysql抱怨:com.mysql.jdbc.JDBC4PreparedStatement@5add5415: select user0_.id as id100_, user0_.created as created100_ from User user0_ where user0_.id=2 limit ** NOT SPECIFIED **WARN  util.JDBCExceptionReporter - SQL Error: 0, SQLState: 07001ERROR util.JDBCExceptionReporter - No value specified for parameter 2)我找到了一個鏈接:https://jira.springsource.org/browse/DATAJPA-147,我試過但失敗了?,F在似乎不可能?為什么Spring-Data中沒有內置這么重要的功能?如果我手動實現此功能:public class UserRepositoryImpl implements UserRepository我必須實現大量的預定義方法CrudRepository,這將是可怕的。環境:spring-3.1,spring-data-jpa-1.0.3.RELEASE.jar,spring-data-commons-core-1.1.0.RELEASE.jar
查看完整描述

3 回答

?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

截至Spring Data JPA 1.7.0(Evans發布列車)。

您可以使用新引進的TopFirst允許您定義的查詢方法是這樣的關鍵字:

findTop10ByLastnameOrderByFirstnameAsc(String lastname);

Spring Data會自動將結果限制為您定義的數字(如果省略則默認為1)。請注意,結果的排序在這里變得相關(通過OrderBy示例中看到的子句或通過將Sort參數傳遞給方法)。閱讀有關Spring Data Evans發布系列的新功能文檔的博客文章中的更多內容。

對于以前的版本

為了只檢索數據片段,Spring Data使用分頁抽象,它Pageable在請求方面提供了一個接口Page,在結果方面也提供了抽象。所以你可以先開始吧

public interface UserRepository extends Repository<User, Long> {

  List<User> findByUsername(String username, Pageable pageable);}

并像這樣使用它:

Pageable topTen = new PageRequest(0, 10);List<User> result = repository.findByUsername("Matthews", topTen);

如果你需要知道結果的上下文(它實際上是哪一頁?它是第一個?總共有多少?),請使用Page返回類型:

public interface UserRepository extends Repository<User, Long> {

  Page<User> findByUsername(String username, Pageable pageable);}

然后客戶端代碼可以執行以下操作:

Pageable topTen = new PageRequest(0, 10);Page<User> result = repository.findByUsername("Matthews", topTen);Assert.assertThat(result.isFirstPage(), is(true));

并不是我們將觸發要執行的實際查詢的計數投影,以防您使用Page返回類型,因為我們需要找出總計有多少元素來計算元數據。除此之外,請確保您實際裝備了PageRequest分類信息以獲得穩定的結果。否則,您可能會觸發查詢兩次并獲得不同的結果,即使數據沒有在下面更改。


查看完整回答
反對 回復 2019-08-08
?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

如果您使用的是Java 8和Spring Data 1.7.0,則可以使用默認方法,如果要將@Query注釋與設置最大結果組合使用:

public interface UserRepository extends PagingAndSortingRepository<User,Long> {
  @Query("from User u where ...")
  List<User> findAllUsersWhereFoo(@Param("foo") Foo foo, Pageable pageable);

  default List<User> findTop10UsersWhereFoo(Foo foo) {
    return findAllUsersWhereFoo(foo, new PageRequest(0,10));
  }}


查看完整回答
反對 回復 2019-08-08
?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

有一種方法可以提供相當于“a by注釋的setMaxResults(n)”,如下所示:

public interface ISomething extends JpaRepository<XYZ, Long>{
    @Query("FROM XYZ a WHERE a.eventDateTime < :before ORDER BY a.eventDateTime DESC")
    List<XYZ> findXYZRecords(@Param("before") Date before, Pageable pageable);}

當將可分頁作為參數發送時,這應該可以解決問題。例如,要獲取前10條記錄,您需要將可分頁設置為此值:

new PageRequest(0, 10)


查看完整回答
反對 回復 2019-08-08
  • 3 回答
  • 0 關注
  • 1320 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號