Configure Spring Data Mongodb in Spring Application
Posted By : Prabjot Singh | 07-Jun-2015
I will show you a configuration of spring data mongodb by extending abstractMongoConfiguration class. It is very easy and rapid process to configure spring data mongodb in application. Let's start with following a few steps
Add dependency for spring data mongodb
org.springframework.data spring-data-mongodb 1.7.0.RELEASE
you can find latest version at Spring Data Mongodb
Spring Data mongodb Configuration by using AbstractMongoConfiguration
So make your own class and extend it by abstractMongoConfiguration. It will provides method to return your database name and darabase host.
@Configuration
public class Datasource extends AbstractMongoConfiguration{
@Override
public String getDatabaseName() {
return "testsampinch";
}
@Override
@Bean
public Mongo mongo() throws Exception {
return new MongoClient("127.0.0.1");
}
}
it will give you a mongotemplate object. So you can use mongotemplate object for basic operation. It has a very good support for criteria
Make Your Domain class
public class User {
String name;
}
Your Service
@Service
public class UserService {
@Autowired MongoTemplate mongoTemplate;
public void saveUser(){
User user = new User();
user.setName(“prabjot”);
mongoTemplate.save(user);
}
}
Thanks!!!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Prabjot Singh
Prabjot is a Java and Grails developer.He has a good hands on neo4J, AngularJS. He likes to work on new technologies