In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you how SpringBoot integrates Mongodb to achieve additions, deletions, queries and changes. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
What is MongoDB
Unlike the relational databases (MySQL, Oracle) that we are familiar with before, MongoDB is a document database with the scalability and flexibility required, as well as the queries and indexes required.
MongoDB stores data in flexible, JSON-like documents, which means that the fields of the document may vary from document to document, and the data structure will change over time. The document model maps to objects in the application code, making the data easy to process. MongoDB is a distributed database at its core, so high availability, scale-out, and geographic distribution are built-in and easy to use. Moreover, MongoDB is free and open source.
Install MongoDB on Window10
Open the MongoDB official website
Download the MSI version (installation version)
Select Custom when downloading
When installing, be careful not to check to install the visual plug-in, otherwise the installation will be very slow (unless your network speed is fast enough)
3. Configure MongoDB service
Configure environment variables
Copy the current path
My computer-> right-click-> Advanced system Settings-> Environment variables-> system variables
Find Path in the system variable, edit, and add the copied path above
IV. Start the service
Win+R- > enter services.msc
After the service starts, type 127.0.0.1purl 2701 in the browser
The appearance of this line of English indicates that the service has been started successfully.
5. SpringBoot integrates MongoDB
Environmental preparation
Operating system: Window10
IDE:IntelliJ IDEA 2018.2.4
Database: MongoDB
1) introduce dependency
Org.springframework.boot spring-boot-starter-data-mongodb
2) add the following configuration to application.yml
Spring: data: mongodb: uri: mongodb://localhost/test_mongodb
The complete configuration information is as follows:
Spring: data: mongodb: authentication-database: # Authentication database name. Database: # Database name. Field-naming-strategy: # Fully qualified name of the FieldNamingStrategy to use. Grid-fs-database: # GridFS database name. Host: # Mongo server host. Cannot be set with URI. Password: # Login password of the mongo server. Cannot be set with URI. Port: # Mongo server port. Cannot be set with URI. Repositories: type: # Type of Mongo repositories to enable. Uri: # Mongo database URI. Cannot be set with host, port and credentials. Username: # Login user of the mongo server. Cannot be set with URI.
3) add entity class UserEntity
Public class UserEntity {@ Id private String uid; private String username; private String password; public String getUid () {return uid;} public void setUid (String uid) {this.uid = uid;} public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public String getPassword () {return password } public void setPassword (String password) {this.password = password;} @ Override public String toString () {return "UserEntity {" + "uid='" + uid +'\'+ ", username='" + username +'\'+ ", password='" + password +'\'+'}';}}
4) create a new test. Here I use navicat as a visualization tool for MongoDB to view.
Test 1: insert operation
@ Autowired private MongoTemplate mongoTemplate; @ Test public void saveUser () {UserEntity userEntity1 = new UserEntity (); UserEntity userEntity2 = new UserEntity (); UserEntity userEntity3 = new UserEntity (); userEntity1.setUid ("111"); userEntity1.setUsername (" user 1 "); userEntity1.setPassword (" password 1 "); userEntity2.setUid (" 222"); userEntity2.setUsername ("user 2") UserEntity2.setPassword (password 2); userEntity3.setUid (333); userEntity3.setUsername (user 3); userEntity3.setPassword (password 3); mongoTemplate.save (userEntity1); mongoTemplate.save (userEntity2); mongoTemplate.save (userEntity3);}
Database information:
As you can see, MongoDB automatically creates the database and generates collections through entity classes (that is, data tables we often talk about), and we have inserted several documents (that is, several records) into the database's userEntity collection through MongoTemplate. _ id is the primary key, and _ class is the entity class package name + class name.
Test 2: query operation
@ Autowired private MongoTemplate mongoTemplate; @ Test public void findUserByUserName () {String username = "user 1"; Query query=new Query (Criteria.where ("username") .is (username)); UserEntity user = mongoTemplate.findOne (query, UserEntity.class); System.out.println (user);}
Output result:
UserEntity {uid='111', username=' user 1 password, password=' password 1'}
Test 3: update operation
@ Autowired private MongoTemplate mongoTemplate; @ Test public void updateUser () {UserEntity userEntity = new UserEntity (); userEntity.setUid ("111l"); userEntity.setUsername ("updated user name"); userEntity.setPassword ("updated password"); Query query = new Query (Criteria.where ("_ id") .is (userEntity.getUid () Update update = Update.update ("username", userEntity.getUsername ()) .set ("password", userEntity.getPassword ()); / / update the first mongoTemplate.updateFirst (query,update,UserEntity.class) that returns the result set; / / update all / / mongoTemplate.updateMulti (query,update,UserEntity.class) of the returned result set;}
The updated database is shown in the figure:
Test 4: delete operation
@ Autowired private MongoTemplate mongoTemplate; @ Test public void DeleteByUserId () {String id = "222"; Query query=new Query (Criteria.where (" _ id ") .is (id)); mongoTemplate.remove (query,UserEntity.class);}
The deleted database is shown in the figure:
These are all the contents of this article "how to integrate SpringBoot with Mongodb to add, delete, check and modify". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.