您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Spring Data JPA的介紹以及基礎(chǔ)用法”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
在昨天我們介紹了Spring Data JPA,以及基礎(chǔ)使用,今天我們講一下分頁。
首先,我們修改RaindropUserRepository類
import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import run.halo.app.model.entity.*; import java.util.List; public interface RaindropUserRepository extends JpaRepository<RaindropUser, Integer> { RaindropUser findByUsername(String userName); RaindropUser findBySexAndEmail(String sex, String email); //修改方法————添加Pageable類 @Query("select new run.halo.app.model.entity.RaindropUser(raindrop.id," + "raindrop.username,raindrop.email,raindrop.sex," + "raindrop.age) from RaindropUser raindrop where raindrop.age < ?1") List<RaindropUser> findByLtAge(Integer age, Pageable pageable); // @Query("select new run.halo.app.model.entity.RaindropUser(a.id,a.username,a.email,a.sex,a.age) from spring_data_jpa a where age > ?1") // RaindropUser findByRtAge(Integer age); }
然后我們修改測試方法:
@Test public void Test(){ // 設(shè)置頁碼為0,每頁顯示數(shù)量15 int page=0,size=15; // 設(shè)置排序方式 以id列降序排列,升序為Sort.Direction.ASC Sort sort = new Sort(Sort.Direction.DESC, "id"); // 設(shè)置Pageable變量 Pageable pageable = PageRequest.of(page, size, sort); List<RaindropUser> list = raindropUserRepository.findByLtAge(15, pageable); System.out.println("List: " + list.get(0)); }
查看控制臺打印信息:
Hibernate: select raindropus0_.id as col_0_0_, raindropus0_.username as col_1_0_, raindropus0_.email as col_2_0_, raindropus0_.sex as col_3_0_, raindropus0_.age as col_4_0_ from springdatajpa raindropus0_ where raindropus0_.age<? order by raindropus0_.id desc limit ? List: RaindropUser(id=1, username=Raindrop, email=123123@gmail.com, sex=man, age=12) Hibernate: select raindropus0_.id as id1_12_, raindropus0_.age as age2_12_, raindropus0_.email as email3_12_, raindropus0_.sex as sex4_12_, raindropus0_.username as username5_12_ from springdatajpa raindropus0_ where raindropus0_.sex=? and raindropus0_.email=? Hibernate: select raindropus0_.id as id1_12_, raindropus0_.age as age2_12_, raindropus0_.email as email3_12_, raindropus0_.sex as sex4_12_, raindropus0_.username as username5_12_ from springdatajpa raindropus0_ where raindropus0_.username=?
“Spring Data JPA的介紹以及基礎(chǔ)用法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。