![]() ![]() Public Employee getEmployeeByFirstName(String firstname) Public void createEmployee(Employee employee) Create Data Access Layer (EmployeeDao) package com.javainterviewpoint MongoRepository behind scenes uses the MongoTemplate to query the Mongo database. MongoTemplate – MongoTemplate provides the Out-of-the-box methods to Spring Data MongoDB, we just need to pass the mongoDbFactory instance which we have created above.We will be passing the URL and port information to the MongoClient and which in turn will be passed to the constructor of the SimpleMongoDbFactory along with database name. ![]() MongoDbFactory – This Factory Bean will create an instance of the MongoDbFactory.In the configuration file, we will be creating two beans scans the current package or packages mentioned in the basePackages attribute for any interface that extends Spring Data interface.scans for the stereotype annotations specified in etc.indicates that our SpringConfig class should be used by the Spring IoC container as a source of bean definitions.MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory()) Return new SimpleMongoDbFactory(mongoClient, MongoTemplate mongoTemplate() MongoClient mongoClient = new MongoClient("localhost", 27017) Public class MongoDbFactory mongoDbFactory() Just execute the below command to insert 3 Employee documents. Once we have created the Employee Collection, we need to add some data employee data into the collection, each of them is called as Document and contains the firstname and lastname of the employee. In MongoDB, we can relate the Collections to a Table in the relational database, Let’s create the Employee Collection which holds the employee details db.createCollection('employee') Use the Database which you have already created use mydb Creating Collection and Employee Document Let’s create an Employee management application which has abilities to create a new employee, update the existing employee, get a particular employee/ all employee and finally delete the existing employee. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |