SpringBoot - Integrate Mongodb

a brief introdction

Mongdb is an open source, high-performance, schema-free document database , one of NoSQL database products, and a non-relational database most similar to a relational database.

scenes to be used

  • User data
    • Storage location: database
    • Features: Permanent storage, extremely low modification frequency
  • Game equipment data
    • Storage location: database, MongDB
    • Features: Combination of permanent storage and temporary storage, high modification frequency
  • Live broadcast data, reward data, fan data
    • Storage address: database, MongDB
    • Features: Combination of permanent storage and temporary storage, extremely frequent modification
  • IoT data
    • Storage location: MongDB
    • Features: Temporary storage, fast modification frequency

Summary: MongDB is suitable for storing temporary data that changes quickly.

Download and install

Download MongoDB Community Server | Mongo Database

After clicking the link to open it, you will come to the MongoDB download interface:

Scroll down to find the download button, and you will find the MongDB download button. After downloading, there will be an msi installation package. Follow the steps to download and install it step by step. A faster way can also choose to directly download the ZIP package and decompress it. use.

After installation, we open the bin directory of MongDB, as long as we can see these files

Then we return to the main directory of MongDB and create a directory to store MongDB data: Note the location of the path above, which is to put a directory called data under the main directory of MongDB, and then create a directory called db under data, and then Let’s go back to the bin directory again: this file is the MongDB service startup program. We use the command on the command line to start it:

mongod --dbpath=..\data\db

You see a lot of output. This is the creation process of initializing the database.

The default port of MongDB is 27017, which can be seen in the above information:

All startup information can be seen above, but it’s not very easy to find.

Now start the MongDB server, and then start the MongDB client. Depending on your installation method, the client startup method is different. If you installed MongDB using msi, then your client is yours. Desktop icon, if you do not generate a desktop icon, then its location is:

If you use unzip installation, then the name of the client is directly called mong, and the location is the same as the location of the server.

But in fact, the default client is not easy to use. I opened IDEA directly and found the connection options of MongDB:

Just keep the configuration as default, and then click OK directly:

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

 You can directly open a console for operating MongDB in IDEA. 

SpringBoot integrates MongDB

First add dependencies

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

Modify the corresponding MongDB configuration information:

 There is only one key configuration information, which is the connection address. The format of its value is

mongodb://host address:port number/database

If the port number is not written, the default is 27017

Then just write the test class:

package com.example.spring01;

import com.example.spring01.pojo.book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;

@SpringBootTest
class Spring01ApplicationTests {
//	自动装配操作MongDB的对象
	@Autowired
	private MongoTemplate mongoTemplate;

	@Test
	void contextLoads() {
		book b = new book();
		b.setName("语文");
		b.setPrice(12);
//		save方法对应保存数据,可以直接将Java对象保存到MongDB的数据库中
		mongoTemplate.save(b);
	}
	@Test
	void find(){
//		fondAll方法查询所有的数据,参数是一个class文件,表示将数据库中的数据封装到class中
		for (book book : mongoTemplate.findAll(book.class)) {
			System.out.println(book);
		}
	}
}

The method for SpringBoot to integrate third-party technologies is to first import the starter, then write the configuration, and finally write the corresponding API method to realize the corresponding business

Guess you like

Origin blog.csdn.net/hssjsh/article/details/131986935