Sunday 14 February 2016

Spring Data MongoDB Integration

A brief on MongoDB:
  • MongoDB is a cross-platform, document oriented database, and leading NoSQL database. 
  • It provides, high performance, high availability, and easy scalability. It works on concept of collection and document.
Few terminologies of MongoDB related to RDBMS :
  • Database -- Database
  • Collection -- Table
  • Document -- Record
  • Column -- Field
For more on MongoDB, please visit : MongoDB Tutorials

A simple example with MongoDB :
     Get the source here https://github.com/PrabhatKJena/SpringDataMongo
  • Installation guide : Install MongoDB 
  • Libraries : include mongo-java-driver-2.11.0.jar into class path
  • create database as "booksDB"
  • create collection as "books"
Output : 
Spring Data Integration with MongoDB:

Here we will see an example using MongoTemplate (same as JdbcTemplate) and Repository.

Defining document class like entity class.
Book.java
Defining annoation based Spring configuration class
SpringConfiguration.java
Note : Add @EnableMongoRepositories to enable all repositories defined

Defining repogitory for Book entity
BookRepository.java
Defining test class (main class). This class can be the service class in the real time application.
Main.java
Output:
Thank You for Visiting the blog. Please feel free to share yout comment/suggestion. 

No comments:

Post a Comment