美文网首页springboot
springboot整合mongodb

springboot整合mongodb

作者: jerrybw | 来源:发表于2017-08-02 11:26 被阅读1305次

pom.xml

        <!-- spring-boot-starter-data-mongodb -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

配置文件

#username和password在mongodb开启了验证后需要填写,否则只要ip:port/db即可
sprint.data.mongodb.uri:=mongodb://username:pwd@ip:port/db

实体类

@Document(collection = "T_conversation")
public class Conversation {

    @Id
    private String id;
    private String fromId;
    private String toId;
    private String type;
    private Integer timestamp;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFromId() {
        return fromId;
    }

    public void setFromId(String fromId) {
        this.fromId = fromId;
    }

    public String getToId() {
        return toId;
    }

    public void setToId(String toId) {
        this.toId = toId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Integer getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Integer timestamp) {
        this.timestamp = timestamp;
    }

    public Conversation() {

    }

    @Override
    public String toString() {
        return "Conversation{" +
                "id='" + id + '\'' +
                ", fromId='" + fromId + '\'' +
                ", toId='" + toId + '\'' +
                ", type='" + type + '\'' +
                ", timestamp=" + timestamp +
                '}';
    }
}

@Document(collection = "xxx")
实体类将会映射到数据库中名为xxx的collection中
此注解可不写,不写的话会在数据库中创建以类名小写为名的collection
@Id
标注在属性上,映射到collection的_id属性上mongodb自己维护此字段,生成

"_id" : ObjectId("598137f6d243573250a370ee")

这样的字段

DAO层

public interface ConversationRepository  extends MongoRepository<Conversation,String>{
}

只需要写一个空的interface继承MongoRepostitory就可以实现简单的增删改查
<xxx,yyy>xxx代表要操作的实体类,yyy代表实体类的主键类型

简单的增删改查操作

  ConversationRepository  conversationRepository  = new ConversationRepository ();
  conversationRepository.xxxx()

复杂查询

查询T_conversation中fromId,toId等于传入的值并且timestamp在begin和end之间的conversation

@Autowired
ConversationRepository conversationRepository;

@Autowired
private MongoTemplate mongoTemplate;

@GetMapping("conversation")
public String getConversationByFromIdAndToIdAndTimeInBeginAndEnd(Integer begin,Integer end,String fromId,String toId){
        BasicDBObject timestampGt = new BasicDBObject("timestamp",new BasicDBObject("$gt", begin));
        BasicDBObject timestampLt = new BasicDBObject("timestamp", new BasicDBObject("$lt", end));
        BasicDBObject fromIdObject = new BasicDBObject("fromId", fromId);
        BasicDBObject toIdObject = new BasicDBObject("toId", toId);
        BasicDBList andList = new BasicDBList();
        andList.add(timestampGt);
        andList.add(timestampLt);
        andList.add(fromIdObject);
        andList.add(toIdObject);
        BasicDBObject queryCondition = new BasicDBObject("$and",andList);
        List<Conversation> conversations = mongoTemplate.find(new BasicQuery(queryCondition), Conversation.class);
        StringBuffer result = new StringBuffer("");
        Gson gson = new Gson();
        String s = gson.toJson(conversations);
        return s;
}

相关文章

网友评论

    本文标题:springboot整合mongodb

    本文链接:https://www.haomeiwen.com/subject/ojyokxtx.html