As for the MongoDB web has a lot of relevant information, so there is not too much to introduce here, we are here mainly to introduce how to use MongoDB and spring boot combination. This section outlines:
(1) Preparatory work;
(2) Create a new MAVEN Java project;
(3) to introduce relevant dependence;
(4) write the test code;
(5) configuration file;
(6) using mongotemplate to operate;
Let's take a look at how each of these steps work.
(1) preparatory work;
Now that you need to use MongoDB, it's obvious that you need to install MongoDB services. This network has a lot of relevant information, to operate on its own.
After the download installs, the MongoDB service is started up for later operation.
(2) Create a new Maven Java project;
Here we create a new Spring-boot-mongodb project.
(3) to introduce relevant dependence;
The introduction of dependency, where the most important is the dependency of MongoDB, other dependencies are basic, see the following Pom.xml file:
<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>com.kfit</groupId>
<artifactId>spring-boot-mongodb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-mongodb</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--JDK version number, Angel use 1.8 here, you can modify the JDK version number for your local configuration.
<java.version>1.8</java.version>
</properties>
<!--
Spring Boot parent Node dependency,
After introducing this, the relevant introduction does not need to add the version configuration,
Spring boot will automatically select the most appropriate version to add.
-
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!--Spring Boot Web support: Mvc,aop ...-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--add MongoDB Support--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
</project>
(4) write the test code;
Here we need to write a simple example to test, we need to create a Demoinfo entity class, we need to create an operation MongoDB repository class, and another is a control class Democontroller. See the following code specifically:
The entity class code DemoInfo:com.kfit.demo.DemoInfo, the specific code is as follows:
Package Com.kfit.demo;
import org.springframework.data.annotation.Id;
/**
* Entity class.
* @author Angel--Guardian Angel
* @version v.0.1
* @date August 18, 2016 3:15:39
*/
Public class Demoinfo {
The id attribute is for MongoDB, decorated with @id annotations
@Id
Private String ID;
Private String name;
Privateintage;
Public String GetName () {
returnname;
}
Public void setName (String name) {
this. Name = name;
}
public int getage () {
return age;
}
Public void setage (int.) {
this. Age = age;
}
@Override
Public String toString () {
return "Demoinfo [id=" + ID + ", name=" + name + ", age=" + Age + "]";