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

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