Spring Data
1.Spring Data JPA
JPA是一个规范,用于操作各种数据库。
2.Spring Data Mongodb
Spring Data对Mongodb的支持。
2.1 方法映射
支持方法映射,根据方法规则生成SQL进行查询,方法命名规则参见4;
Flux<StudentEntity> findByNameAndSex(String name, String sex);
2.2 聚合操作支持
Spring Data MongoDB支持在2.2版中引入MongoDB的聚合框架。
在Spring数据MongoDB中的聚合框架的支持是基于以下关键抽象:Aggregation,AggregationOperation,和AggregationResults。
- 1 Aggregation
表示MongoDB aggregate操作,并保存聚合管道指令的描述。
通过调用类的相应newAggregation(…)静态工厂方法来创建聚合Aggregation,
该方法采用列表AggregateOperation和可选的输入类。
实际的聚合操作由the的aggregate方法执行,该方法MongoTemplate将所需的输出类作为参数。
- 2 AggregationOperation
表示MongoDB聚合管道操作,并描述了应在此聚合步骤中执行的处理。
虽然您可以手动创建AggregationOperation,但我们建议使用Aggregate类提供的静态工厂方法来构造AggregateOperation。
- 3 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);
这两个是一样的:
public Flux<Student> findRoleByNameAndPermissions(List<String> names) {
Query query = query(where("科目").is("语文").and("name").in(names));
Flux<RoleEntity> roles = mongoTemplate.find(query, Student.class, "students");
return roles;
}
public Flux<Student> findCatalogs1(List<String> roleNames) {
TypedAggregation<Student> aggregation = Aggregation.newAggregation(Student.class,
match(new Criteria().andOperator(Criteria.where("name").in("names").and("科目").is("语文"))));
return mongoTemplate.aggregate(aggregation, "students", Student.class);
}
//or查询
Query query = query(where("name").is(name)
.and("age").is(age)
.orOperator(where("teacher").is("校长"),
where("mother").is("teacher")));
return mongoTemplate.find(query, Student.class, "students");
3.Spring Data Redis
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) |