Using spring boot to operate mongodb
MongoDB
As a NoSQL database product, mongodb is already very famous. Last year, due to the weak security certification of mongodb, tens of thousands of companies were recruited. Although it is a negative news, it also explains the popularity of mongodb from the side.
The following figure shows the ranking of global database engine usage in May 2017 according to DB engines. As can be seen from the figure, mongodb ranks fifth in the overall list and first in non relational databases, ranking very high.
Personally, I am not very familiar with mongodb, but after a period of understanding, I still have some simple understanding of the characteristics of mongodb. Here are some records.
Having said so much, we still have to go back to practical use. Here, I will use spring boot to operate mongodb. Because spring boot has the support of spring data, it is easy and convenient to use mongodb.
The method is as follows:
First, add the configuration dependency of spring data Mongo, as shown below:
The second is in application Configure the connection parameters of mongodb in properties, as shown below:
Spring boot configuration is so simple, and then the specific code is written. First, you need to define an entity class, which is illustrated by a simple user entity class:
Getter and setter are omitted from this code, so it looks very simple. One thing worth noting is the @ document annotation, which is dedicated to mongodb. If you know mongodb, you know that collection is to mongodb, just like a table in a relational database. By specifying collection, you can realize the mapping relationship between entity classes and mongodb collections. If collection is not explicitly specified, spring will guess the name of the collection according to the name of the entity class.
After configuration, the entity class is also implemented. What we need is to implement various operations of mongodb to connect the database with the application. According to the current situation of spring data, there are probably two ways to realize database operation - mongorepository and mongotemplate. Mongorepository is a relatively simple method, which can help us easily implement simple CRUD operations.
Here is how it is used:
Then directly inject the dependency into the userrepository where the database is used. It's really very simple, because spring has done two things for us. First, spring will generate beans for the repository. Generally speaking, the built-in repository and its subclasses in spring use the @ norepositorybean annotation, so only the user-defined interface without the annotation will be instantiated as a bean. Secondly, spring will automatically generate the function of crud operation according to the method name in the interface, so we don't even need to write the implementation code.
Using mongotemplate is a little more troublesome, but it can do more work at the same time. The following is the way to use mongotemplate. In order to demonstrate its powerful ability, we use a slightly more complex example.
The code is as follows:
For the entity class article, we don't need to care about its details. In order to realize the function of paging query (mainly implemented in findpage), the code uses a slightly complex query operation, which reflects the more powerful customization operation ability of mongotemplate. The Component annotation here is only a way to declare beans. In addition to it, there are many configuration methods, but the functions are the same, so I won't go into it here.
Return the database results through a restful interface, and you can see the database query results. The code is as follows:
The access results through the browser are as follows:
summary
The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.