日期:2014-05-16  浏览次数:20440 次

Spring和Morphia,MongoDB的简单封装以及自增ID的实现(转)

原文地址:http://www.oschina.net/code/snippet_98659_3681
一个对Spring和Morphia的简单封装,主要针对Mongo的自增ID实现以及方便与Spring的集成。
标签: Spring , mongodb , morphia , nosql
代码片段(15)
[代码] [Java]代码
01
package com.paojiao.nosql.mongo;
02

03
import com.google.code.morphia.annotations.Entity;
04
import com.google.code.morphia.annotations.Id;
05

06
import java.io.Serializable;
07

08
/**
09
* Created by IntelliJ IDEA.
10
* User: Arden
11
* Date: 11-3-26
12
* Time: 下午12:42
13
* To change this template use File | Settings | File Templates.
14
*/
15
@Entity(value = "sequence", noClassnameStored=true)
16
public class MongoSequence implements Serializable {
17
    @Id
18
    private String collName;
19

20
    private Long value;
21

22
    public MongoSequence(){
23
    }
24

25
    public MongoSequence(String collName) {
26
        this.collName = collName;
27
    }
28

29
    public String getCollName() {
30
        return collName;
31
    }
32

33
    public void setCollName(String collName) {
34
        this.collName = collName;
35
    }
36

37
    public Long getValue() {
38
        return value;
39
    }
40

41
    public void setValue(Long value) {
42
        this.value = value;
43
    }
44
}
[代码] [Java]代码
01
package com.paojiao.nosql.mongo;
02

03
import com.google.code.morphia.Datastore;
04
import com.google.code.morphia.Morphia;
05
import com.mongodb.Mongo;
06
import com.paojiao.session.Session;
07
import org.slf4j.Logger;
08
import org.slf4j.LoggerFactory;
09

10
import java.util.Map;
11

12
/**
13
* Created by IntelliJ IDEA.
14
* User: Arden
15
* Date: 11-3-26
16
* Time: 下午12:55
17
* To change this template use File | Settings | File Templates.
18
*/
19
public class MongoSequenceManager extends MongoDAO<MongoSequence, String> {
20
    // mongodb日志记录器
21
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
22
    // 序列映射
23
    private Map<String, Long> sequenceMap = null;
24

25
    public MongoSequenceManager(Datastore ds) {
26
        super(ds);
27
        this.entityClazz = MongoSequence.class;
28
    }
29

30
    public void setSequenceMap(Map<String, Long> sequenceMap) {
31
        this.sequenceMap = sequenceMap;
32
    }
33

34
    /**
35
     * 获得序列初始值
36
     * @param collName
37
     * @return
38
     */
39
    public Long getSequenceStartNumber(String collNam