Spring Data 學習筆記

Spring Data

1.Spring Data JPA

未完待續。

2.Spring Data Mongodb

2.1 方法映射

支持方法映射,根據方法規則生成SQL進行查詢,方法命名規則參見4;

Flux<StudentEntity> findByNameAndSex(String name, String sex);

2.2 聚合框架支持

Spring Data MongoDB支持在2.2版中引入MongoDB的聚合框架。

有關詳細信息,請參閱MongoDB的聚合框架和其他數據聚合工具的完整參考文檔。

在Spring數據MongoDB中的聚合框架的支持是基於以下關鍵抽象:Aggregation,AggregationOperation,和AggregationResults。
Aggregation表示MongoDB aggregate操作,並保存聚合管道指令的描述。通過調用類的相應newAggregation(…)靜態工廠方法來創建聚合Aggregation,該方法採用列表AggregateOperation和可選的輸入類。
實際的聚合操作由the的aggregate方法執行,該方法MongoTemplate將所需的輸出類作爲參數。
AggregationOperation表示MongoDB聚合管道操作,並描述了應在此聚合步驟中執行的處理。雖然您可以手動創建AggregationOperation,但我們建議使用Aggregate類提供的靜態工廠方法來構造AggregateOperation。
AggregationResults是聚合操作結果的容器。它提供對原始聚合結果的訪問,Document以映射對象的形式和有關聚合的其他信息。
以下清單顯示了使用Spring Data MongoDB對MongoDB聚合框架的支持的規範示例:

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

Aggregation agg = newAggregation(
    pipelineOP1(),
    pipelineOP2(),
    pipelineOPn()
);

AggregationResults<OutputType> results = mongoTemplate.aggregate(agg, "INPUT_COLLECTION_NAME", OutputType.class);
List<OutputType> mappedResult = results.getMappedResults();


Spring Data MongoDB當前支持的聚合操作

TypedAggregation<Student> aggregation = Aggregation.newAggregation(Student.class, match(new Criteria()
                .andOperator(Criteria.where("orga").is(org)
                        .and("enable").is(true)
                        .and("user").is(userId)
                        .and("resour").is(eventId)
                        .and("").is("Access"))));
        return mongoTemplate.aggregate(aggregation, "students", Student.class);

3.Spring Data Redis

未完待續

4.Spring Data 方法名中支持的關鍵字

關鍵詞 示範 等同於
And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
Is,Equals findByFirstname,findByFirstnameIs,findByFirstnameEquals … where x.firstname = ?1
Between findByStartDateBetween … where x.startDate between ?1 and ?2
LessThan findByAgeLessThan … where x.age < ?1
LessThanEqual findByAgeLessThanEqual … where x.age <= ?1
GreaterThan findByAgeGreaterThan … where x.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual … where x.age >= ?1
After findByStartDateAfter … where x.startDate > ?1
Before findByStartDateBefore … where x.startDate < ?1
IsNull findByAgeIsNull … where x.age is null
IsNotNull,NotNull findByAge(Is)NotNull … where x.age not null
Like findByFirstnameLike … where x.firstname like ?1
NotLike findByFirstnameNotLike … where x.firstname not like ?1
StartingWith findByFirstnameStartingWith … where x.firstname like ?1(附加參數綁定%)
EndingWith findByFirstnameEndingWith … where x.firstname like ?1(與前置綁定的參數%)
Containing findByFirstnameContaining … where x.firstname like ?1(包含參數綁定%)
OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
Not findByLastnameNot … where x.lastname <> ?1
In findByAgeIn(Collection ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection ages) … where x.age not in ?1
True findByActiveTrue() … where x.active = true
False findByActiveFalse() … where x.active = false
IgnoreCase findByFirstnameIgnoreCase … where UPPER(x.firstame) = UPPER(?1)

https://docs.spring.io/spring-data/mongodb/docs/2.1.9.RELEASE/reference/html/#mongo.aggregation.supported-aggregation-operations

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章