Use spring to connect and operate mongodb3 0 instance

In the previous article, it was recorded that you can directly connect and operate mongodb database in Java code without using spring. Here, it is followed by the simple operation of mongodb in Java when using spring.

Maven guided package configuration:

Because sping and spring MVC are involved, you also need to import their related packages:

Spring basic configuration:

It mainly starts annotation scanning, etc

Spring connects mongodb and establishes the configuration of related factories:

Entity class corresponding to database:

It should be noted that the serialized interface needs to be implemented and the uid attribute needs to be set here. Otherwise, the returned results from the database cannot be directly converted into object attributes in the operation:

According to the spring configuration, to obtain the mongotemplette that operates mongodb, you need to implement the applicationcontextaware interface:

Interface for operating database and corresponding implementation class:

It demonstrates the most basic addition, deletion, modification and query. It should be noted that the declaration of parameters and the conversion of entity classes when receiving returned data:

(1) Interface:

(2) Implementation class. Here, we need to inherit the abstractbasemongotempelet class to obtain mongotempelet for various operations:

Test class:

In order to verify the correctness of the above codes and configurations, the test class codes are as follows:

Add the demo later. Download address: Demo

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>