DockerのMongoDBのインストールとSpringbootとの統合

27689 ワード

注意:基本的な使い方一般的なインストール前の記事を参照してください:mongodb入門レベル権威ガイドDockerインストール入門前のブログ:Docker基礎とインストール入門

1.dockerでMongoDBをインストールする

#    
docker pull mongo:4.0.3
#    
docker create --name mongodb -p 27017:27017 -v /data/mongodb:/data/db mongo:4.0.3
#    
docker start mongodb
#    
docker exec -it mongodb /bin/bash
#  MongoDB       
mongo
> show dbs  #        
admin  0.000GB
config  0.000GB
local  0.000GB

2.SpringBoot統合MongoDB


Spring-dataはMongoDBをサポートしており、spring-data-mongodbを使用するとMongoDBの操作、アドレスを簡略化できます.https://spring.io/projects/spring-data-mongodb.
依存のインポート
<parent>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-parentartifactId>
  <version>2.1.0.RELEASEversion>
parent>
<dependency>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-data-mongodbartifactId>
dependency>
<dependency>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-testartifactId>
  <scope>testscope>
dependency>

アプリケーションを作成します.propertiesプロファイル
spring: 
	application:
		name: qqxhb-mongodb
	data:
	mongodb:
	uri: mongodb://172.16.55.185:27017/testdb

PersonDaoの作成
import cn.qqxhb.mongodb.Person;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
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.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class PersonDao {
  @Autowired
  private MongoTemplate mongoTemplate;
  public void savePerson(Person person) {
    this.mongoTemplate.save(person);
 }
 public List<Person> queryPersonListByName(String name) {
    Query query = Query.query(Criteria.where("name").is(name));
    return this.mongoTemplate.find(query, Person.class);
 }
  public List<Person> queryPersonListByName(Integer page, Integer rows) {
    Query query = new Query().limit(rows).skip((page - 1) * rows);
    return this.mongoTemplate.find(query, Person.class);
 }
  public UpdateResult update(Person person) {
    Query query = Query.query(Criteria.where("id").is(person.getId()));
    Update update = Update.update("age", person.getAge());
    return this.mongoTemplate.updateFirst(query, update, Person.class);
 }
  public DeleteResult deleteById(String id) {
    Query query = Query.query(Criteria.where("id").is(id));
    return this.mongoTemplate.remove(query, Person.class);
 }
}

起動クラスの作成
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MongoApplication {
  public static void main(String[] args) {
    SpringApplication.run(MongoApplication.class, args);
 }
}

ユニットテストの作成
import cn.qqxhb.mongodb.dao.PersonDao;
import org.bson.types.ObjectId;
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.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestPersonDao {
  @Autowired
  private PersonDao personDao;
  @Test
  public void testSave() {
    Person person = new Person(ObjectId.get(), "  ", 20);
    this.personDao.savePerson(person);
 }
  @Test
  public void testQuery() {
    List<Person> personList = this.personDao.queryPersonListByName("  ");
    for (Person person : personList) {
      System.out.println(person);
   }
 }
  @Test
  public void testQuery2() {
    List<Person> personList = this.personDao.queryPersonListByName(2, 2);
    for (Person person : personList) {
      System.out.println(person);
   }
 }
  @Test
  public void testUpdate() {
    Person person = new Person();
    person.setId(new ObjectId("3f326ce235e1925204629537"));
    person.setAge(30);
    this.personDao.update(person);
 }
  @Test
  public void testDelete() {
    this.personDao.deleteById("3f326ce235e1925204629537");
 }
}