How to integrate Sprint Boot into MongoDB: sprintmongodb

Source: Internet
Author: User
Tags mongodb version

How to integrate Sprint Boot into MongoDB: sprintmongodb

Mongodb is one of the first popular non-relational databases and is widely used. It is usually used for offline data analysis and is mostly used on the Intranet. Many companies use cloud services and their servers are open to external addresses by default. As a result, a large number of MongoDB instances were attacked due to configuration vulnerabilities and their data was deleted some time ago, which aroused people's attention, if you are interested, take a look at this article: The Feast of MongoDB slaughter reflection: over 33000 databases have been compromised and ransomware. It also shows that many companies are using mongodb in their production.

Mongodb Introduction

MongoDB (from the English word "Humongous", which has a Chinese meaning of "huge") is an open source database that can be applied to enterprises of all sizes, industries and applications. A Database Based on Distributed File storage. Written in C ++. It is designed to provide scalable, high-performance data storage solutions for WEB applications. MongoDB is a high-performance, open-source, and non-pattern document-based database. It is a popular NoSql database.

MongoDB is a product between relational databases and non-relational databases. It has the most abundant functions and features like relational databases. The supported data structure is very loose and is similar to the json bjson format. Therefore, it can store complicated data types. The biggest feature of Mongo is that it supports a very powerful query language. Its syntax is somewhat similar to an Object-Oriented Query Language. It can almost implement most of the functions similar to single-table queries in relational databases, it also supports data indexing.

Traditional relational databases are generally composed of three levels: database, table, and record. MongoDB is composed of databases and collections) and document objects. MongoDB has no concept of columns, rows, and links for tables in relational databases, which reflects the free mode.

A record in MongoDB is a document and a data structure consisting of fields and value pairs. MongoDB documents are similar to JSON objects. The Field Values may include other documents, arrays, and document arrays. MongoDB supports OS X, Linux, Windows, and other operating systems. It also provides drivers for Python, PHP, Ruby, Java, and C ++. The community also provides Erlang and.. NET.

MySQL is suitable for storing a large amount of or no fixed format data, such as logs and caches. It has weak support for transactions and does not apply to cascading queries of complex multi-document (Multi-table. This document demonstrates that the mongodb version is 3.4.

I recently took over a Springboot project and needed to add some requirements in the original project to use mongodb. Let's take a look at the integration path!

1. First, introduce the mongodbDe dependency jar package in pom. xml.

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

2. Create an object class

@Document(collection = "spiderConfig")public class SpiderConfig implements Serializable {  @Id  private String id;  private String spiderConfig;  private long updateTime;  public String getId() {    return id;  }  public void setId(String id) {    this.id = id;  }  public String getSpiderConfig() {    return spiderConfig;  }  public void setSpiderConfig(String spiderConfig) {    this.spiderConfig = spiderConfig;  }  public long getUpdateTime() {    return updateTime;  }  public void setUpdateTime(long updateTime) {    this.updateTime = updateTime;  }}

3. Code for operating mongodb at the dao Layer

@Componentpublic class SpiderConfigDaoImpl implements ISpiderConfigDao{  @Autowired  private MongoTemplate mongoTemplate;  @Override  public SpiderConfig findById(String id) {    Query query=new Query(Criteria.where("_id").is(id));    SpiderConfig user = mongoTemplate.findOne(query , SpiderConfig.class);    return user;  }  @Override  public void saveSpiderConfig(SpiderConfig spiderConfig) {    mongoTemplate.save(spiderConfig);  }}

4. Configure the monggodb database in application. properties.

# Configure spring. data. mongodb. uri = mongodb: // 192.168.86.888: 27017/test

The related information has been configured. test and verify that the interaction with the database is correct!

Summary

The above is a small part of the Sprint Boot integration MongoDB operation method, I hope to help you, if you have any questions, please leave a message, the small part will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.