支持方法映射,根据方法规则生成SQL进行查询,方法命名规则参见4;
Flux<StudentEntity> findByNameAndSex(String name, String sex);
Spring Data MongoDB支持在2.2版中引入MongoDB的聚合框架。
在Spring数据MongoDB中的聚合框架的支持是基于以下关键抽象:Aggregation,AggregationOperation,和AggregationResults。
表示MongoDB aggregate操作,并保存聚合管道指令的描述。
通过调用类的相应newAggregation(…)静态工厂方法来创建聚合Aggregation,
该方法采用列表AggregateOperation和可选的输入类。
实际的聚合操作由the的aggregate方法执行,该方法MongoTemplate将所需的输出类作为参数。
表示MongoDB聚合管道操作,并描述了应在此聚合步骤中执行的处理。
虽然您可以手动创建AggregationOperation,但我们建议使用Aggregate类提供的静态工厂方法来构造AggregateOperation。
聚合操作结果的容器。它提供对原始聚合结果的访问,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);
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");
Spring Data对Redis的支持。
关键词 | 示范 | 等同于 |
---|
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
//匹配对应postId,星级在1-5之间的数据,求平均值
TypedAggregation<Reply> aggregation = Aggregation.newAggregation(
本内容只是为了介绍mongodb最基础的使用以及配置,作为一个知名的数据库,其存在相当多的高级用法,展开来介绍内容会相当多,当然本人并非相关领域的大神,下面内容只不过整理了自己日常使用的一些积累。是对自己经验的积累,也希望能帮助后来的同学
本内容也是我尝...
由于Spring boot安全漏洞,须将项目中Spring boot升级至2.3.4版本,2.3.4版本集成了spring-data-mongodb 3.x版本,项目中原spring-data-mongodb 2.x版本被替换成spring-data-mongodb 3.x版本,须进行兼容性适配。
spring-data-mongodb从2.x升级至3.x版本部分api变化见官网项目链接
适配过程中发现,项目中存在大量aggregate group操作,例如:
TypedAggregatio
protected Long getCountByTime(Long startTime, Long endTime) {
Criteria criteria = Criteria.where("uptime").gte(startTime).lte(endTime);
AggregationOptions.Builder builder = new AggregationOptions.Builder().al..
Spring Data概述
SpringData是SpringSource下的一个子项目,旨在简化对于数据库或者数据存储的持久化操作,包括存储和访问,并且不拘泥于SQL,它也支持NoSQL数据库如MongoDB。
Spring data下包含多个子项目如
Spring Data JPA
Spring Data MongoDB
Spring Data Redis
Spring Data Solr...
mongoTemplate.save(new Role("zhang1",5));
mongoTemplate.save(new Role("zhang2",6));
mongoTemplate.save(new Role("zhang3",12));
mongoTemplate.save(new Role("zhang4...
用Mongo的聚合组件Aggregation要用到两个方法skip和limit。skip设置起点(分页的时候不包含起点,从起点的下一行开始),limit设置条数。如:
Aggregation.skip(10),
Aggregation.limit(20)的意思是(10,20)第10条以后取20条数据。
Criteria criteria= new Criteria();
### 回答1:
很高兴听到您对Spring Boot 2的学习感兴趣。Spring Boot是一个基于Spring框架的快速开发应用程序的工具,它提供了一种简单的方式来创建和配置Spring应用程序。以下是一些学习Spring Boot 2的笔记:
1. Spring Boot 2的新特性:Spring Boot 2相对于Spring Boot 1.x版本来说,有许多新的特性和改进。例如,它支持Java 9和10,提供了更好的响应式编程支持,以及更好的安全性和监控功能。
2. Spring Boot 2的核心组件:Spring Boot 2的核心组件包括Spring Framework、Spring MVC、Spring Data和Spring Security等。这些组件提供了一系列的功能和工具,使得开发人员可以更加轻松地构建和管理应用程序。
3. Spring Boot 2的配置:Spring Boot 2的配置非常简单,它提供了一种基于注解的方式来配置应用程序。开发人员可以使用@Configuration和@Bean注解来定义配置类和Bean。
4. Spring Boot 2的启动器:Spring Boot 2提供了一系列的启动器,这些启动器可以帮助开发人员快速地集成各种常用的框架和库,例如Spring Data JPA、Spring Security、Thymeleaf等。
5. Spring Boot 2的测试:Spring Boot 2提供了一系列的测试工具,例如Spring Boot Test、Mockito、JUnit等。这些工具可以帮助开发人员编写高质量的单元测试和集成测试。
希望这些笔记对您学习Spring Boot 2有所帮助。如果您有任何问题或疑问,请随时联系我。
### 回答2:
Spring Boot 是一个非常流行的开源框架,它的目的是使 Spring 应用程序的开发过程更加简单、快速和高效。Spring Boot 2 是 Spring Boot 框架的第二个版本,已经成为了当今业界最流行的 Java 开发框架之一。
Spring Boot 2 的新特性:
1. 支持 JDK 9。Spring Boot 2 已经支持 JDK 9 所带来的新特性和功能。
2. 基于 Reactive Streams 的编程模型。Spring Boot 2 增加了对 Reactive Streams 的支持,可以开发响应式应用程序,从而提高了系统的吞吐量和性能。
3. 异步服务器支持。Spring Boot 2 已经支持多种异步服务器,包括 Netty、Undertow 和 Tomcat。
4. 更全面的 Actuator 组件。Actuator 是 Spring Boot 的监控和管理组件,Spring Boot 2 在 Actuator 组件上增加了更多的指标、健康检查和应用程序信息。
5. 更好的自定义配置。Spring Boot 2 简化了自定义配置的流程,使得用户可以更快速地配置应用程序。
学习 Spring Boot 2 的步骤如下:
1. 掌握 Spring 基础知识。学习 Spring Boot 2 前需要掌握 Spring MVC、Spring Data 等相关的知识。
2. 下载安装 Spring Boot。Spring Boot 2 可以在官网上下载。
3. 学习 Spring Boot 核心组件。包括 Spring IoC、Spring AOP、Spring MVC 等核心组件。
4. 开发一个 Spring Boot 应用程序。可以从一个简单的 Hello World 开始,逐渐增加功能,学习和理解 Spring Boot 的各种特性和功能。
5. 掌握 Spring Boot 的自动配置。Spring Boot 的自动配置可以大大减少开发人员的工作量,学习和掌握自动配置非常重要。
总之,学习 Spring Boot 2 需要不断地实践和探索,只有通过实际的开发经验才能真正掌握和学会这个框架。
### 回答3:
Spring Boot是一款基于Spring框架的快速应用开发框架。在应用开发的过程中,Spring Boot可以自动配置一个相对完整的Spring应用程序,从而大大减少了开发者的工作量,提高了开发效率。显然,它的学习是很有必要的。
Spring Boot 2.x版本相比于1.x版本在很多方面都进行了升级和改进。在学习的过程中,需要关注的重点在以下几点:
1. 新建Spring Boot项目
Spring Boot提供了Spring Initializr来快速创建新的Spring Boot项目。在构建项目的过程中,我们可以自定义项目的基本信息、项目类型、依赖关系等,可以根据需求灵活选择。
2. Spring Boot自动配置
Spring Boot借助于自动配置功能,可以为开发者免去大量的配置工作。Spring Boot把一些常用的配置提取为Starter,开发者只需要引入这些Starter即可实现自动配置,大大降低了开发成本。
3. Spring Boot集成
Spring Boot集成了众多流行的框架,如MyBatis、Hibernate、JPA、Thymeleaf等,只需要简单的配置就可以实现对这些框架的集成,使得在开发中更加方便。
4. Spring Boot监控管理
Spring Boot通过Actuator提供了一系列配置和管理服务,可以实现对Spring Boot应用程序的监控和管理。开发者可以通过这些服务来监控应用程序的运行状态、访问量、资源使用情况等。
5. Spring Boot测试
Spring Boot天生适合进行单元测试和集成测试,可以使用JUnit、Mockito、Spring Test等框架进行测试。同样可以利用Spring Boot提供的Test Starter进行自动配置,减少测试的开发成本。
以上就是Spring Boot 2的一些学习笔记,它的结构简洁,代码清晰,便于开发者使用。无疑,Spring Boot 2是一个非常适合作为开发者日常工作的框架。
OpenCsv CsvRequiredFieldEmptyException: Number of data fields does not match number of headers.
蓬莱阁-阁主: