我需要将Mongodb嵌入到Spring Boot应用程序中。但我的方式如何 它导致每次我启动应用程序时重新创建mongo db。我的意思是,我的文档不会持久保存到数据库。
我在日志中看到的每一个开始:
2017-11-22 12:03:49.188 INFO 10464 --- [ main] c.j.s.embedmongo.EmbeddedMongoBuilder : Initializing embedded MongoDB instance
2017-11-22 12:03:49.215 INFO 10464 --- [ main] d.f.embed.process.store.Downloader : Extract C:\Users\lev\.embedmongo\win32\mongodb-win32-x86_64-2008plus-3.2.1.zip : starting...
2017-11-22 12:03:49.483 INFO 10464 --- [ main] d.f.embed.process.store.Downloader : Extract C:\Users\lev\.embedmongo\win32\mongodb-win32-x86_64-2008plus-3.2.1.zip : extract mongodb-win32-x86_64-2008plus-3.2.1/bin/mongod.exe
2017-11-22 12:03:49.484 INFO 10464 --- [ main] d.f.embed.process.store.Downloader : Extract C:\Users\lev\.embedmongo\win32\mongodb-win32-x86_64-2008plus-3.2.1.zip : noting left
2017-11-22 12:03:49.484 INFO 10464 --- [ main] d.f.embed.process.store.Downloader : Extract C:\Users\lev\.embedmongo\win32\mongodb-win32-x86_64-2008plus-3.2.1.zip : finished
2017-11-22 12:03:49.484 INFO 10464 --- [ main] c.j.s.embedmongo.EmbeddedMongoBuilder : Starting embedded MongoDB instance
2017-11-22 12:03:51.180 INFO 10464 --- [ main] d.f.embed.process.runtime.Executable : start de.flapdoodle.embed.mongo.config.MongodConfigBuilder$ImmutableMongodConfig@867ba60
2017-11-22 12:03:51.383 INFO 10464 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:3707], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2017-11-22 12:03:51.418 INFO 10464 --- [-localhost:3707] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:1}] to localhost:3707
2017-11-22 12:03:51.419 INFO 10464 --- [-localhost:3707] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:3707, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 1]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=432755}
2017-11-22 12:03:51.509 INFO 10464 --- [ main] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:2}] to localhost:3707
2017-11-22 12:03:51.939 WARN 10464 --- [ main] o.s.d.m.c.m.BasicMongoPersistentProperty : Customizing field name for id property not allowed! Custom name will not be considered!
这部分可能导致数据库覆盖: 下载器:解压缩C:\ Users \ lev.embedmongo \ win32 \ mongodb-win32
我的Pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>guru.springframework</groupId>
<artifactId>spring-boot-mongodb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-mongodb</name>
<description>Demo project for Spring Boot and Mongo DB</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
</dependency>
<dependency>
<groupId>cz.jirutka.spring</groupId>
<artifactId>embedmongo-spring</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
用于存储文档的控制器:
@RequestMapping("/check")
@ResponseBody
public List<Domain> check(){
System.out.println("/check");
Domain domain = new Domain(i++, "Check_"+String.valueOf(i), true);
dm.save(domain);
return dm.findAll();
}
存储库:
public interface DomainRepository extends MongoRepository<Domain, Long> {
Domain findFirstByDomain(String domain);
Domain findByDomainAndDisplayAds(String domain, boolean displayAds);
//Supports native JSON query string
@Query("{domain:'?0'}")
Domain findCustomByDomain(String domain);
@Query("{domain: { $regex: ?0 } })")
List<Domain> findCustomByRegExDomain(String domain);
}
appilcation.properties:
server.port = 8091
spring.data.mongodb.repositories.enabled=true
spring.data.mongodb.database=testme
我该如何解决这个问题?
答案 0 :(得分:1)
因为它是embedded
,每次启动应用程序时,都会重新创建数据库,因为当应用程序停止时,之前的数据库已被删除。