spring-data-mongodb 注解详解

spring-data-mongodb 常用的注解

@Document

org.springframework.data.mongodb.core.mapping.Document.class
把一个java类声明为mongodb的文档,可以通过collection参数指定这个类对应的文档。
@Document(collection="mongodb 对应 collection 名")      

// 若未加 @Document ,该 bean save 到 mongo 的 user collection
// 若添加 @Document ,则 save 到 reUser collection
@Document(collection="reUser") 
public class User{
}

@Indexed

org.springframework.data.mongodb.core.index.Indexed.class
声明该字段需要索引,建索引可以大大的提高查询效率。

@CompoundIndex

org.springframework.data.mongodb.core.index.CompoundIndex.class
复合索引的声明,建复合索引可以有效地提高多字段的查询效率。

@Field

org.springframework.data.mongodb.core.mapping.Field.class
给映射存储到 mongodb 的字段取别名
在 java bean 中字段名为 firstName,存储到 mongo 中 key 为 fName
    @Field("fName")
    private String firstName;   

@Id

org.springframework.data.annotation.Id.class
文档的唯一标识,在mongodb中为ObjectId,它是唯一的

@Transient

org.springframework.data.annotation.Transient.class
默认情况下所有的私有字段都映射到文档,该注解标识的字段从存储在数据库中的字段列中排除(即该字段不保存到 mongodb)

猜你喜欢

转载自blog.csdn.net/justdoit_potato/article/details/81007008