網站首頁 編程語言 正文
????????有時我們在查詢某個實體的時候,給定的條件是不固定的,這時就需要動態構建相應的查詢語句,在Spring Data JPA中可以通過JpaSpecificationExecutor接口查詢。相比JPQL,其優勢是類型安全,更加的面向對象。
JpaSpecificationExecutor接口的方法有:
Optional<T> findOne(@Nullable Specification<T> spec);
List<T> findAll(@Nullable Specification<T> spec);
Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);
List<T> findAll(@Nullable Specification<T> spec, Sort sort);
long count(@Nullable Specification<T> spec);
boolean exists(Specification<T> spec);
核心參數是?Specification接口 可以采用內部類的方式或lambda表達式來創建。
自定義查詢條件
? ? ? ? ? ? 1.實現Specification接口(提供泛型:查詢的對象類型)
? ? ? ? ? ? ? 2.實現toPredicate方法構造查詢條件)
? ? ? ? ?? ? ? 3.需要借助方法參數中的兩個參數(
? ? ? ? ?? ? ? ? ?root:獲取需要查詢的對象屬性
? ? ? ? ?? ? ? ? ?CriteriaBuilder:構造查詢條件的,內部封裝了很多的查詢條件(模糊匹配,精準匹配))
1. Optional<T> findOne(@Nullable Specification<T> spec); 條件查詢
@SpringBootTest
public class Tes4 {
@Autowired
private CustomerDao dao;
@Test //one
public void show1(){
Specification<Customer> sp = (Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb)->{
Path<Object> custName = root.get("custName");
Predicate predicate = cb.equal(custName, "小王");
return predicate;
};
Optional<Customer> one = dao.findOne(sp);
System.out.println(one);
}
2.List<T> findAll(@Nullable Specification<T> spec);
@Test //多條件查詢
public void show2(){
Specification<Customer> sp = ( root, query, cb)->{
Path<Object> custName = root.get("custName");
Path<Object> custAddress = root.get("custAddress");
Predicate p1 = cb.equal(custName, "小王");
Predicate p2 = cb.equal(custAddress, "吉祥村");
Predicate predicate = cb.or(p1, p2);
return predicate;
};
dao.findAll(sp).stream().forEach(System.out::println);
}
3.模糊查詢
@Test //模糊
public void show3(){
Specification<Customer> sp = ( root, query, cb)->{
Path<Object> custName = root.get("custName");
Predicate predicate = cb.like(custName.as(String.class), "%老%");
return predicate;
};
dao.findAll(sp).stream().forEach(System.out::println);
}
4.? ?List<T> findAll(@Nullable Specification<T> spec, Sort sort);
@Test //排序
public void show4(){
Specification<Customer> sp = ( root, query, cb)->{
Path<Object> custName = root.get("custName");
Predicate predicate = cb.like(custName.as(String.class), "%老%");
return predicate;
};
Sort sort = Sort.by(Sort.Direction.DESC, "custId");
dao.findAll(sp,sort).stream().forEach(System.out::println);
}
5.? ? Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);
@Test //分頁
public void show5(){
Specification<Customer> sp = ( root, query, cb)->{
Path<Object> custName = root.get("custName");
Predicate predicate = cb.like(custName.as(String.class), "%老%");
return null;
};
PageRequest page = PageRequest.of(1, 2);
Page<Customer> list = dao.findAll(sp, page);
list.getContent().stream().forEach(System.out::println);//查詢結果
System.out.println(list.getTotalElements()+"總數");
System.out.println(list.getTotalPages()+"總頁數");
}
? ? ? ? ?
原文鏈接:https://blog.csdn.net/weixin_71419462/article/details/126128497
相關推薦
- 2022-05-28 基于ASP.NET實現驗證碼生成詳解_實用技巧
- 2022-10-19 C++線程安全容器stack和queue的使用詳細介紹_C 語言
- 2022-10-31 Kotlin集合List?Set?Map使用介紹詳解_Android
- 2022-10-24 C語言詳解分析進程控制中進程終止的實現_C 語言
- 2023-05-08 Android開發多手指觸控事件處理_Android
- 2022-05-20 Springboot讀取application配置文件中的自定義配置的幾種方式
- 2022-10-12 C#設計模式之建造者模式生成器模式示例詳解_C#教程
- 2022-09-08 Python如何將list中的string轉換為int_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支