In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to three-step integration of MongoDB + Spring Boot, many novices are not very clear, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
The first step is to deploy the mongo database
Deployment of mongodb, specific according to their own needs, or roughly two types of it, one is to download and install directly, regardless of windows,linux. The other is container startup.
The main purpose of this article is to start a mongodb container using docker.
There are two ways in docker.
First, pull and start docker run-d-- network some-network-- name some-mongo\-v / my/own/datadir:/data/db\-e MONGO_INITDB_ROOT_USERNAME=mongoadmin\-e MONGO_INITDB_ROOT_PASSWORD=secret\ mongo directly with the command
Other detailed parameters can be found on the website https://hub.docker.com/_/mongo.
Start using the yml configuration file
My yml file is attached for your reference.
Version: "3.7" services: mangodb: image: mongo:latest restart: always networks:-loc_net ports:-27017 volumes:-/ myPath/mongo/db:/data/db environment: MONGO_INITDB_ROOT_USERNAME: username MONGO_INITDB_ROOT_PASSWORD: password deploy: replicas: 1 placement: constraints: [node.role = = manager] networks: loc_net: external: true
There is also a more detailed configuration in https://hub.docker.com/_/mongo.
One thing to note is that
To put it simply, if there is already database data in the directory mapped by the host, the initialized user name and password will not take effect.
About the directory of configuration files for mongo:
Step 2: configure the mongodb data source
Introduce maven dependency
Org.springframework.boot spring-boot-starter-data-mongodb
Configure in the spring boot project
Spring: data: mongodb: uri: mongodb://username:password@127.0.0.1:27017/test?authSource=admin&readPreference=primary&ssl=false
AuthSource: specify user authentication database readPreference: set read priority node ssl: connect using ssl
Similar to mysql, here is the configuration of single data sources. If the configuration of multiple data sources is also very simple, you can check it on https://docs.mongodb.com/manual/mongo/, including all the connection parameters.
The third step is injection.
Single data source, ordinary use, does not need to be re-injected in the spring factory, can be used directly.
@ Autowiredprivate MongoTemplate mongoTemplate
And the sdk of mongo itself is already relatively upper-level, so it is very convenient to use it directly.
For the CRUD operation of mongo, go to https://docs.mongodb.com/manual/crud/ to learn. (1) understanding of memory database:
The data file is still on the storage disk, but it has its own database cache in memory, and the data that needs to be read will be mapped and read into the cache through MMAP, which greatly speeds up the query of data.
(2) A simple comparison between mongo and mysql:
First, the main difference is that mongo is a non-relational database and mysql is a relational database.
Second, different mysql storage engines have different storage methods, most of which are mainly disk IO. Mongo is virtual memory persistence, which, as mentioned above, takes advantage of memory reads. Memory reads far more data than disk IO, which is why mongo queries far exceed mysql.
Third, mysql is more stable. Transactionality is strong, while mongo transaction support is not strong.
4. Https://www.cnblogs.com/liaocheng/p/4237091.html is recommended for the performance comparison of inserts and queries between mysql and mongo.
Fifth, the primary key in the mongo is "_ id", do not specify the primary key mongo will generate one, do not specify the primary key insertion speed is faster than the specified primary key.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.