Spring Framework supports MongoDb as a part of the overall Spring Data Framework. Following are some of the support features for MongoDB
Provides Spring configuration support, both java-based as well as XML based, for MongoDB driver and MongoDB replica sets.
Provides a helper class - MongoTemplate, that helps with common MongoDB operations, and object mapping between documents and POJOs.
Translates MongoDB exceptions to Spring Data framework's Data Access Exception hierarchy.
Provides rich annotation-based Object mapping features.
Provides Java-based Query, Criteria and Update DSLs.
You connect to MongoDB using Spring framework by instantiating an instance of com.mongodb.client.MongoClient. This can be done either by using java-based configuration as well as by using XML-based configuration.
@Configuration
public class AppConfig {
public @Bean MongoClient mongoClient() {
return MongoClients.create('mongodb://localhost:27017');
}
}
The MongoTemplate class is the key class provided by Spring Data MongoDB framework. It provides rich features to interact with MongoDb database. Following are some of the key features.
1. MongoTemplate class provides convenience operations to create, update, delete and query MongoDb databases. MongoTemplate class implements MongoOperations interface which provides methods similar to native MongoDB collections method names, such as - find(), findOne(), findAll(), findAndReplace(), insert(), save(), update() etc.
2. MongoTemplate class implements interface MongoConverter which facilitates mapping between domain objects and MongoDB documents.
3. MongoTemplate class translates MongoDb exceptions thrown by MongoDB driver to Spring Data Framework's Data Access Exception hierarchy.
MongoTemplate has multiple overloaded constructors that can be used to create an instance of MongoTemplate.
MongoTemplate(MongoClient mongoClient, String databaseName) - Takes MongoClient object and default database name.
MongoTemplate(MongoDatabaseFactory mongoFactory) - Takes a MongoDataBaseFactory object that encapsulates MongoClient, database, and its credentials.
You can create, update and delete MongoDB documents using MongoTemplate, which provides convenience methods similar to native MongoDB collection methods.
insert(), update(), and remove() methods can be used to create, update and delete documents respectively.
MongoTemplate mongoTemplate = new MongoTemplate(MongoClients.create(), 'database');
//Create MongoDB document
Website w = new Website('interviewgrid','www.interviewgrid.com');
mongoTemplate.insert(w);
//find document
w = findById('interviewgrid',Website.class);
//update document
mongoTemplate.updateFirst(query(where('id')is('interviewgrid')),update('url','http://www.interviewgrid.com'), Website.class);
//delete document
mongoTemplate.remove(w);
Spring Data JDBC, part of Spring Data, builds on top of the core repository support of Spring Data and provides support for JDBC repositories.
Spring Data JDBC aims at being a simple, limited, opinionated ORM. It does not implement many features of JPA such as caching and lazy loading.
Following are some key features provided by Spring Data JDBC.
CRUD operations with customizable NamingStrategy
Support for @Query operations
JavaConfig based repository configuration using @EnableJdbcRepositories.