Spring MVC and MongoDB In this blogs we will discussed about spring MVC and MongoDB and also define difference between Spring and MongoDB. You should read this blogs for full details. Repository Class Using MongoDB : Spring has the capability to create loose coupling application's layers. To dealing with database we create a DAO layer. In Spring DAO layer contains the Repository class this class is responsible for all database related operations. In this example we will use the @Repository annotaion to lable the Repository class.
MongoDB is different kind of database, it is scalable, NoSQL Document Oriented database. MongoDB holds data in the form of document and collections. If we compare the MongoDb to other Relational databases then we can say that the document in the MongoDB is like a row in Relational database and collection is like a table which holds the collection of documents.
Document is like a key-value information. Document holds the all information of a single entity like a row in relational database. Key in the documents are called field. Collection is a group of documents. It is like a table in relational database.
Spring provides the MongoTemplate class to handle the database related operations. This class will provides the important functions like save(), insert(), findAll(), remove () etc. which facilitate the developer to use the MongoDB without any problem. The following sample code describes some of the functions. Example of Spring MVC and MongoDB : Use following dependencies in pom.xml 1. <dependencies> 2. <dependency> 3. <groupId>org.springframework</groupId> 4. <artifactId>spring-orm</artifactId> 5. <version>3.2.0.RELEASE</version> 6. </dependency> 7. <dependency> 8. <groupId>org.springframework</groupId> 9. <artifactId>spring-webmvc</artifactId> 10. <version>3.2.0.RELEASE</version> 11. </dependency> 12. <dependency> 13. <groupId>org.springframework.data</groupId> 14. <artifactId>spring-data-mongodb</artifactId> 15. <version>1.2.0.RELEASE</version> 16. </dependency> 17. <dependency> 18. <groupId>jstl</groupId> 19. <artifactId>jstl</artifactId> 20. <version>1.2</version>
</dependency> </dependencies>
21. 22.
Create model class : Customer.java 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24.
package com.evon.model; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document public class Customer { @Id private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Create DAO interface : CustomerDAO.java 1. 2. 3. 4. 5. 6.
package com.evon.service; import java.util.List; import com.evon.model.Customer; public interface CustomerDAO { Read Full Blogs :- Spring MVC and
MongoDB
You can check more informative blogs and tutorials at iphone development blogs section and can also browse the iOS developer forum, Java, PHP, etc. for posting and viewing latest questions on development.