欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot集成Jpa對數(shù)據(jù)進行排序、分頁、條件查詢和過濾操作

 更新時間:2023年05月13日 08:57:26   作者:張占嶺  
這篇文章主要介紹了SpringBoot集成Jpa對數(shù)據(jù)進行排序、分頁、條件查詢和過濾操作,主要使用Jpa連接數(shù)據(jù)庫對數(shù)據(jù)進行排序、分頁、條件查詢和過濾操作,需要的朋友可以參考下

之前介紹了SpringBoot集成Jpa的簡單使用,接下來介紹一下使用Jpa連接數(shù)據(jù)庫對數(shù)據(jù)進行排序、分頁、條件查詢和過濾操作。首先創(chuàng)建Springboot工程并已經(jīng)繼承JPA依賴,如果不知道可以查看我的另一篇文進行學習,這里不做介紹。參考文章地址

1、排序查詢

通過findAll方法的Sort類進行排序,根據(jù)實體類字段進行排序。descending降序,ascending升序,默認不填為ascending升序。

List<User> mapperAll = userMapper.findAll(Sort.by("id").descending()); 
mapperAll.forEach(System.out::println);

查詢結(jié)果:

Sort.by() 里面是一個可變參數(shù),可以傳入一個或多個值。

 //先根據(jù)狀態(tài)進行排序,然后在根據(jù)ID進行排序
List<User> statusAll = userMapper.findAll(Sort.by("status","id").descending());
statusAll.forEach(System.out::println);

設(shè)置第一個屬性降序,第二個屬性升序

 Sort sort = Sort.by("status").descending().and(Sort.by("id").ascending());
 List<User> diffOb= userMapper.findAll(sort);
 diffOb.forEach(System.out::println);

如果傳入的字段信息,實體類中沒有,代碼訪問會報錯。如:

List<User> exceptionAll = userMapper.findAll(Sort.by("en_name").descending());
exceptionAll.forEach(System.out::println);

會報找不到該字段信息異常:

org.springframework.data.mapping.PropertyReferenceException: No property en found for type User! Did you mean 'id'?

2、分頁查詢

JPA為我們提供了分頁的方法,我們可以查看接口集成的JpaRepository接口的關(guān)系圖。發(fā)現(xiàn)有一個PagingAndSortingRepository接口。

點擊該接口進行查看:

使用這個接口方法就可以實現(xiàn)分頁查詢了。我們發(fā)現(xiàn)這個方法需要傳入一個Pageable,點擊Pageable查看發(fā)現(xiàn)它也是一個接口我們點擊查看它的實現(xiàn)類。

使用PageRequest類就可以實現(xiàn)分頁了,PageRequest有個靜態(tài)的of方法,page參數(shù)為顯示當前頁數(shù),size為顯示當前顯示多少數(shù)據(jù).

//第一頁,顯示4條數(shù)據(jù)  0為一頁 1為二頁  輸入頁數(shù)大于總頁數(shù)則輸出內(nèi)容為空
        PageRequest request = PageRequest.of(0, 4); 
        Page<User> userPage = userMapper.findAll(request);
        System.out.println();
        System.out.println("總頁數(shù): "+userPage.getTotalPages()); //總頁數(shù)
        System.out.println("總條數(shù): "+userPage.getTotalElements()); //總條數(shù)
        System.out.println("查詢的數(shù)據(jù): "+userPage.getContent());//查詢的數(shù)據(jù)
        System.out.println("顯示條數(shù): "+userPage.getSize()); //顯示條數(shù)
        System.out.println("當前頁數(shù): "+Integer.valueOf(Integer.valueOf(userPage.getNumber())+1)); //當前頁數(shù)

查詢結(jié)果為:

還可以給分頁查詢進行排序,根據(jù)主鍵進行降序排序

PageRequest pageRequest = PageRequest.of(0, 4, Sort.by("id").descending());
        Page<User> orderPAge = userMapper.findAll(pageRequest);
        System.out.println("查詢的數(shù)據(jù): "+orderPAge.getContent());//查詢的數(shù)據(jù)
        orderPAge.getContent().forEach(System.out::println);

分頁進行多個字段的排序同一排序

PageRequest pageRequest1 = PageRequest.of(0, 4, Sort.Direction.DESC, "status","id");
        Page<User> orde = userMapper.findAll(pageRequest1);
        System.out.println("查詢的數(shù)據(jù): "+orde.getContent());//查詢的數(shù)據(jù)
        orde.getContent().forEach(System.out::println);

分頁進行多個字段的不同排序 根據(jù)status 升序,id降序排序

PageRequest of = PageRequest.of(0, 4, Sort.by("status").ascending().and(Sort.by("id").descending()));
        Page<User> ord = userMapper.findAll(of);
        ord.getContent().forEach(System.out::println);

查看查詢輸出sql結(jié)果:

3、條件查詢

下面我們來看使用JPA進行條件查詢。在JPA中JPA使用findBy方法自定義查詢。也可以使用findAllBy。這兩個沒有區(qū)別實際上還是使用的finBy...進行查詢的。

//根據(jù)賬號名稱進行查詢,有信息放回該條數(shù)據(jù),沒有查詢到則放回null,如果查詢多條數(shù)據(jù)則會報錯
        User user=userMapper.findByAccount("hibernateTest");
//Dao層
       User findByAccount(String account);

如果查詢多條會報錯javax.persistence.NonUniqueResultException: query did not return a unique result: 4,把接收參數(shù)改為List,就可以查詢多條數(shù)據(jù)了。

 List<User> findByAccount(String account);

findBy后面還可以支持多種關(guān)鍵詞進行查詢:

And:等價于SQL中的 and 關(guān)鍵字, findByAccountAndPassword(String account, String password);
And:等價于SQL中的 and 關(guān)鍵字,findByAccountAndPassword(String account, String password);
Or:等價于SQL中的 or 關(guān)鍵字, findByAccountOrName(String account, String name);
Between:等價于SQL中的 between 關(guān)鍵字,findByIdBetween(int min, int max);
LessThan:等價于 SQL 中的 "<",findByIdLessThan(int id);
GreaterThan:等價于 SQL 中的">",findByIdGreaterThan(int id);
IsNull:等價于 SQL 中的 "is null",findByNameIsNull();
IsNotNull:等價于 SQL 中的 "is not null",findByNameIsNotNull();NotNull:與 IsNotNull 等價;
Like:等價于 SQL 中的 "like",findByNameLike(String name);這樣傳值需要加 %name%,可以使用Containing,這個方法會在字符串兩邊都加上%,
除此之外還有StartingWith 和EndingWith,分別為 ?%,%?
NotLike:等價于 SQL 中的 "not like",findByNameNotLike(String name);傳值需要加 %name%
Not:等價于 SQL 中的 "!=",findByUsernameNot(String user);
OrderBy:等價于 SQL 中的 "order by",findByNameOrderByStatusAsc(String name);
In:等價于 SQL 中的 "in",findByIdIn(Collection userIdList) ,方法的參數(shù)可以是 Collection 類型,也可以是數(shù)組或者不定長參數(shù);
NotIn:等價于 SQL 中的 "not in",findByNameNotIn(Collection userList) ,方法的參數(shù)可以是 Collection 類型,也可以是數(shù)組或者不定長參數(shù);
這里介紹一下模糊查詢使用方法:

List<User> userList2=userMapper.findByNameLike("%hibernateTest%");
userList2.forEach(System.out::println);
List<User> userList3=userMapper.findByNameContaining("hibernateTest");
userList3.forEach(System.out::println);
//dao層
List<User> findByNameLike(String name);
List<User> findByNameContaining(String name);

上面的findBy規(guī)則介紹完畢后,接下來我們結(jié)合上面的分頁和排序,查詢出過濾后的數(shù)據(jù)。條件+分頁+排序查詢使用:

Page<User> userPage = userMapper.findByNameLike("%hibernateJPa%", PageRequest.of(0, 2, Sort.by("id").descending()));
        System.out.println("總頁數(shù): "+userPage.getTotalPages()); //總頁數(shù)
        System.out.println("總條數(shù): "+userPage.getTotalElements()); //總條數(shù)
        System.out.println("查詢的數(shù)據(jù): "+userPage.getContent());//查詢的數(shù)據(jù)
        System.out.println("顯示條數(shù): "+userPage.getSize()); //顯示條數(shù)
        System.out.println("當前頁數(shù): "+Integer.valueOf(Integer.valueOf(userPage.getNumber())+1)); //當前頁數(shù)
//dao層
Page<User> findByNameLike(String name, Pageable pageable);

查詢結(jié)果:

使用JPA進行查詢、排序和分頁我們就介紹完畢了。關(guān)于JPA后面還有復雜的sql條件查詢,需要繼承JpaSpecificationExecutor接口。JpaSpecificationExecutor接口方法不多但是很好用,推薦大家有時間可以學習一下。

到此這篇關(guān)于SpringBoot集成Jpa對數(shù)據(jù)進行排序、分頁、條件查詢和過濾操作的文章就介紹到這了,更多相關(guān)SpringBoot集成Jpa數(shù)據(jù)分頁查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論