Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure MongoDB database in SpringBoot2

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to configure MongoDB database in SpringBoot2". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to configure MongoDB database in SpringBoot2" can help you solve the problem.

A brief introduction to NoSQL 1. The concept of NoSQL

NoSQL (Not Only SQL), which means "not just SQL". A general term for a database management system that is different from traditional relational databases. NoSQL is used to store very large-scale data. These types of data stores do not require fixed schemas and can be scaled out without redundant operations.

2. Advantages / disadvantages of NoSQL-advantages: flexibility of low-cost architecture of highly scalable distributed computing, no complex relationship of semi-structured data-disadvantages: no standardized limited query function (so far) data presentation is not intuitive 2, MongoDB database 1, introduction to MongoDB

MongoDB is a product between relational database and non-relational database, which is the most functional and most like relational database among non-relational databases. The data structure he supports is very loose, which is similar to json's bjson format, so it can store more complex data types. The most important feature of MongoDB is that the query language it supports is very powerful, and its syntax is somewhat similar to the object-oriented query language. It can almost achieve most of the functions similar to the single table query of relational database, and also supports the establishment of data indexing.

2. Characteristics of MongoDB

1) MongoDB is written in C++ language and is an open source database system based on distributed file storage.

2) in the case of high load, adding more nodes can ensure server performance.

3) MongoDB aims to provide scalable high-performance data storage solutions for WEB applications.

4) MongoDB stores the data as a document, and the data structure consists of key-value (key= > value) pairs. MongoDB documents are similar to JSON objects. Field values can contain other documents, arrays, and document arrays.

3. Integrate with SpringBoot2 1. MongoDB basic environment # Open command line MongoDB4.0\ bin > mongo# to show all databases > show databases# create a new admin database, the command is more embarrassing > db.admin.insert ({"name": "administrator database"}) # create a root user using admin database > use admin#, with read and write permissions > db.createUser ({user: "root", pwd: "root", roles: [{role: "readWrite", db: "admin"}]}) Successfully added user:2, core dependency org.springframework.boot spring-boot-starter-data-mongodb3, configuration file

User name: root

Password: root

Database: admin

Spring: data: mongodb: uri: mongodb://root:root@localhost:27017/admin4, wrapper API public interface ImgInfoRepository {void saveImg (ImgInfo imgInfo); ImgInfo findByImgTitle (String imgTitle); long updateImgInfo (ImgInfo imgInfo); void deleteById (Integer imgId);} 5, core code block

The use of MongoDB is as follows.

Import com.boot.mongodb.entity.ImgInfo;import com.boot.mongodb.repository.ImgInfoRepository;import com.mongodb.client.result.UpdateResult;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.Service;import javax.annotation.Resource;@Servicepublic class ImgInfoRepositoryImpl implements ImgInfoRepository {@ Resource private MongoTemplate mongoTemplate @ Override public void saveImg (ImgInfo imgInfo) {mongoTemplate.save (imgInfo);} @ Override public ImgInfo findByImgTitle (String imgTitle) {Query query=new Query (Criteria.where ("imgTitle") .is (imgTitle)); return mongoTemplate.findOne (query,ImgInfo.class);} @ Override public long updateImgInfo (ImgInfo imgInfo) {Query query=new Query (Criteria.where ("imgId") .is (imgInfo.getImgId () Update update= new Update () .set ("imgTitle", imgInfo.getImgTitle ()) .set ("imgUrl", imgInfo.getImgUrl ()); UpdateResult result = mongoTemplate.updateFirst (query,update,ImgInfo.class); return result.getMatchedCount ();} @ Override public void deleteById (Integer imgId) {Query query = new Query (Criteria.where ("imgId") .is (imgId)); mongoTemplate.remove (query,ImgInfo.class) 6. Test code block import com.boot.mongodb.MongoDBApplication;import com.boot.mongodb.entity.ImgInfo;import com.boot.mongodb.repository.ImgInfoRepository;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.annotation.Resource;import java.util.Date;@RunWith (SpringJUnit4ClassRunner.class) @ SpringBootTest (classes = MongoDBApplication.class) public class MongoTest {@ Resource private ImgInfoRepository imgInfoRepository @ Test public void test1 () {ImgInfo record = new ImgInfo (); record.setImgId (1); record.setUploadUserId ("A123"); record.setImgTitle ("blog Picture"); record.setSystemType (1); record.setImgType (2); record.setImgUrl ("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4");") Record.setLinkUrl ("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4"); record.setShowState (1); record.setCreateDate (new Date ()); record.setUpdateDate (record.getCreateDate ()); record.setRemark (" cicada "); record.setbEnable (" 1 "); imgInfoRepository.saveImg (record) } @ Test public void test2 () {ImgInfo imgInfo = imgInfoRepository.findByImgTitle ("blog picture"); System.out.println ("imgInfo = >" + imgInfo);} @ Test public void test3 () {ImgInfo record = new ImgInfo (); record.setImgId (1); record.setUploadUserId ("A123"); record.setImgTitle ("cicada picture"); record.setSystemType (1) Record.setImgType (2); record.setImgUrl ("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4"); record.setLinkUrl (" https://avatars0.githubusercontent.com/u/50793885?s=460&v=4"); record.setShowState (1); record.setCreateDate (new Date ()); record.setUpdateDate (record.getCreateDate ()); record.setRemark ("cicada") Record.setbEnable ("1"); long result = imgInfoRepository.updateImgInfo (record); System.out.println ("result = = >" + result);} @ Test public void test4 () {imgInfoRepository.deleteById (1);}} that's all for "how to configure the MongoDB database in SpringBoot2". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report