MongoTemplate 教程系列(一)

3,060 阅读2分钟

第一章:Document定义

  • 注解说明:

@Id - 文档的唯一标识,在mongodb中为ObjectId,它是唯一的,通过时间戳+机器标识+进程ID+自增计数器(确保同一秒内产生的Id不会冲突)构成。

源码:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface Id {
}

@Document - 把一个java类声明为mongodb的文档,可以通过collection参数指定这个类对应的文档。

源码:

@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Document {
   String collection() default "";

   String language() default "";
}

@DBRef - 声明类似于关系数据库的关联关系。ps:暂不支持级联的保存功能,当你在本实例中修改了DERef对象里面的值时,单独保存本实例并不能保存DERef引用的对象,它要另外保存,如下面例子的Person和Account。

源码:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
@Reference
public @interface DBRef {
   String db() default "";

   boolean lazy() default false;
}

@Indexed - 声明该字段需要索引,建索引可以大大的提高查询效率。

源码:

@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Indexed {
   boolean unique() default false;

   IndexDirection direction() default IndexDirection.ASCENDING;

   boolean sparse() default false;

   boolean dropDups() default false;

   String name() default "";

  boolean useGeneratedName() default false;

   /** @deprecated */
   @Deprecated
   String collection() default "";

   boolean background() default false;

   int expireAfterSeconds() default -1;
}

@CompoundIndex - 复合索引的声明,建复合索引可以有效地提高多字段的查询效率。

源码:

@Target({ElementType.TYPE})
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface CompoundIndex {
   String def() default "";

   /** @deprecated */
   @Deprecated
   IndexDirection direction() default IndexDirection.ASCENDING;

   boolean unique() default false;

   boolean sparse() default false;

   boolean dropDups() default false;

   String name() default "";

   boolean useGeneratedName() default false;

   /** @deprecated */
   @Deprecated
   String collection() default "";

   boolean background() default false;
}

@GeoSpatialIndexed - 声明该字段为地理信息的索引。

源码:

@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface GeoSpatialIndexed {
   String name() default "";

   boolean useGeneratedName() default false;

   /** @deprecated */
   @Deprecated
   String collection() default "";

   int min() default -180;

   int max() default 180;

   int bits() default 26;

   GeoSpatialIndexType type() default GeoSpatialIndexType.GEO_2D;

   double bucketSize() default 1.0D;

   String additionalField() default "";
}

@Transient - 映射忽略的字段,该字段不会保存到mongodb。

源码:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface Transient {
}

@PersistenceConstructor - 声明构造函数,作用是把从数据库取出的数据实例化为对象。该构造函数传入的值为从DBObject中取出的数据

源码:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.CONSTRUCTOR})
public @interface PersistenceConstructor {
}


  • Example:

public class Item implements Serializable{

    /** 项目名称 **/
    private String item_name;

    /** 分数 **/
    private double score;

    /** 单位 **/
    private String unit;
    
    //省略 get() set()方法
@Document(collection = "pt")
public class PT implements Serializable {

    @Id
    private String id;

    /**学生id **/
    private String student_Id;

    /** 学生姓名 **/
    private String student_name;

    /** 学校id **/
    private String school_id;

    /** 学校名称 **/
    private String school_name;

    /** 年级id **/
    private String grade_id;

    /** 年级名称 **/
    private String grade_name;

    /** 班级id **/
    private String class_id;

    /** 班级名称 **/
    private String class_name;

    /** 批次 **/
    private String batch;

    /** 测试时间 **/
    private Date test_time;

    /** 项目 **/
    private List<Item> items;
    
    //省略 get() set()方法

实际效果: