Spring Boot 使用NoSQL之 MongoDB

weixin_34320159發表於2017-03-20

上篇介紹Spring Boot 連線Redis,本章開始介紹使用MongoDB, 在Spring Boot中使用MongoDB一樣的簡單與高效,下文開始演示。(坑以挖深,放心跳)

訪問MongoDB

  • 引入依賴(POM)
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
  • 啟動本地MongoDB
    1 . 官網下載最新社群版URL,:
    https://www.mongodb.com/download-center?jmp=nav

請根據自身的系統環境下載,如下圖:


5224684-86ca0a91c1e4a8a0.png
Paste_Image.png

安裝後啟動服務如下:

5224684-cb50ac30fa75edc7.png
Paste_Image.png

Spring Boot的預設配置,application.properties配置引數:
** Mongo 3.0版以下**的配置

spring.data.mongodb.host=mongoserver
spring.data.mongodb.port=27017

** Mongo 3.0版以上**的配置

spring.data.mongodb.uri=mongodb://use:pwd@localhost:27017/test

我本機的配置如下(供參考MongoDB 3.2):

spring.data.mongodb.uri=mongodb://test01:test01@localhost:27017/test```

###建立資料實體

package com.vo;

import java.io.Serializable;

public class User implements Serializable {
private static final long serialVersionUID = -3258839839160856613L;

private Long id;

private String userName;

private String moblie;



public Long getId() {
    return id;
}

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

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getMoblie() {
    return moblie;
}

public void setMoblie(String moblie) {
    this.moblie = moblie;
}

}


###編寫單元測試用例:

import com.Application;

import com.vo.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.Instant;

/**

  • Description: mongoDB 使用
  • author: 慢慢來了
  • date: 2017/3/20 17:17
    */

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class MongoDBTest {

@Autowired
private  MongoTemplate mongoTemplate;

@Test
public void saveUser(){
    User user = new User();
    user.setId(Instant.now().toEpochMilli());
    user.setUserName("王大錘");
    user.setMobile("13300000000");
    mongoTemplate.save(user);
    Assert.assertTrue(mongoTemplate.findAll(User.class).size() == 1);
}

@Test
public void findUser(){
   User user =  mongoTemplate.findOne(new Query(Criteria.where("userName").is("王大錘")) , User.class);
    Assert.assertEquals("13300000000" , user.getMobile());

}

}


###檢視結果
通過Mongo命令直接登入並授權如下:
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/5224684-0780b80f6c278f48.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
執行查詢命令:
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/5224684-31fb9e04bc914b69.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


###參考資料:
 >  http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/

相關文章