MongoTemplate如何从实体类中获取collectionName数据库集合名
在按照 springboot整合mongodb 写好一个基础的 MongoTemplate 使用示例之后,我很好奇数据到底写到 MongoDB 的哪个“集合” 中?
比如,我们在我们项目中调用 findAll 方法来查询 MongoDB 中的数据:
public List<Student> findAll() {
return mongoTemplate.findAll(Student.class);
}
而跟踪一下 MongoTemplate#findAll
的源码:
MongoTemplate#getCollectionName
获取集合名称的源码:
成员变量 operations 是类型为 EntityOperations 的实例对象。EntityOperations#determineCollectionName
的源码:
这边有两个问题:
- getRequiredPersistentEntity 返回的对象是什么类型;
- 该类型的 getCollection 返回的值是怎么创建的?
上面这两个问题的答案都在 org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity
的构造函数中。
BasicMongoPersistentEntity
构造函数源代码如下:
public BasicMongoPersistentEntity(TypeInformation<T> typeInformation) {
super(typeInformation, MongoPersistentPropertyComparator.INSTANCE);
Class<?> rawType = typeInformation.getType();
String fallback = MongoCollectionUtils.getPreferredCollectionName(rawType); // 根据实体类类型生成“集合”名称
if (this.isAnnotationPresent(Document.class)) {
Document document = this.getRequiredAnnotation(Document.class);
this.collection = StringUtils.hasText(document.collection()) ? document.collection() : fallback;
this.language = StringUtils.hasText(document.language()) ? document.language() : "";
this.expression = detectExpression(document.collection());
this.collation = document.collation();
this.collationExpression = detectExpression(document.collation());
} else {
this.collection = fallback;
this.language = "";
this.expression = null;
this.collation = null;
this.collationExpression = null;
}
this.shardKey = detectShardKey();
}
@Document指定collection
根据上面的注解,如果实体类包含 @Document
注解,且指定了 collection
属性:
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.List;
@Data
@Document(collection = "t_student")
public class Student{
private String id;
private String name;
private Integer age;
private Integer sex;
private Integer height;
private List<Hobby> hobbies;
}
比如上述代码这样写,那么集合名称取值为 t_student
。
默认collection命名规则——实体类名+首字母小写
如果没有特别指定,那么就会用 MongoCollectionUtils.getPreferredCollectionName(rawType)
返回的字符串:
首先 getSimpleName,那么原来实体类 org.coderead.entity.Student 将返回字符串 Student。
org.springframework.util.StringUtils#uncapitalize
作用就是首字母小写!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2021-05-06 Hyper-V安装Ubuntu遇到的问题
2021-05-06 CentOS7系统DVD镜像下载
2021-05-06 用Java过滤器实现跨域资源共享 CORS