## Jpa內置查詢
jpa內置多種動態查詢,常用的有:Example實例查詢、Specification復雜查詢。
使用文章推薦:https://blog.csdn.net/qq_30054997/article/details/79420141
## QuerySpec查詢
QuerySpec是基于Specification復雜查詢封裝的一套查詢工具,語法類似于Example實例查詢,dao層需要繼承JpaSpecificationExecutor\<T>接口。示例如下:
```java
public Page<User> getPageList(User user, Integer pageIndex, Integer pageSize) {
// 創建分頁對象
Sort sort = new Sort(Sort.Direction.ASC, "createDate");
PageRequest page = PageRequest.of(pageIndex-1, pageSize, sort);
QuerySpec querySpec = QuerySpec.matching()
.withMatcher("nickname", QuerySpec.LIKE)
.withIgnorePaths("password");
return userRepository.findAll(QuerySpec.of(user, querySpec), page);
}
```
1. static matching() --------------------------------------------------創建QuerySpec實例
2. withMatcher("字段名", 查詢規則) -----------------------------添加查詢匹配器
3. withMatcherIn("字段名", 值列表) ------------------------------添加In查詢方式匹配器
4. withMatcherBetween("字段名", 值1, 值2) -------------------添加Between查詢方式匹配器
5. withIgnorePaths("忽略字段"...) ---------------------------------設置需要忽略查詢的字段
6. static of(實體對象 ,QuerySpec實例) --------------------------獲取Specification復雜查詢實例
**注意**:QuerySpec查詢不支持關聯的字段,必須忽略關聯的字段!