Spring Data 学习笔记

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 方法名中支持的关键字

关键词示范等同于
AndfindByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2
OrfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
Is,EqualsfindByFirstname,findByFirstnameIs,findByFirstnameEquals… where x.firstname = ?1
BetweenfindByStartDateBetween… where x.startDate between ?1 and ?2
LessThanfindByAgeLessThan… where x.age < ?1
LessThanEqualfindByAgeLessThanEqual… where x.age <= ?1
GreaterThanfindByAgeGreaterThan… where x.age > ?1
GreaterThanEqualfindByAgeGreaterThanEqual… where x.age >= ?1
AfterfindByStartDateAfter… where x.startDate > ?1
BeforefindByStartDateBefore… where x.startDate < ?1
IsNullfindByAgeIsNull… where x.age is null
IsNotNull,NotNullfindByAge(Is)NotNull… where x.age not null
LikefindByFirstnameLike… where x.firstname like ?1
NotLikefindByFirstnameNotLike… where x.firstname not like ?1
StartingWithfindByFirstnameStartingWith… where x.firstname like ?1(附加参数绑定%)
EndingWithfindByFirstnameEndingWith… where x.firstname like ?1(与前置绑定的参数%)
ContainingfindByFirstnameContaining… where x.firstname like ?1(包含参数绑定%)
OrderByfindByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname desc
NotfindByLastnameNot… where x.lastname <> ?1
InfindByAgeIn(Collection ages)… where x.age in ?1
NotInfindByAgeNotIn(Collection ages)… where x.age not in ?1
TruefindByActiveTrue()… where x.active = true
FalsefindByActiveFalse()… where x.active = false
IgnoreCasefindByFirstnameIgnoreCase… 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值