As we know, MongoDB is a no-SQL database. Before looking at the example of MongoDB with spring framework, we need to know few things about it.
We can go for mongodb instead of relation databases, when your data is sort of document data which means you just need to open the document, do the changes and save it again, it doesn’t have any relationship among them. we can also have the mongodb when we want to compute user data before accessing it and having it ready to read be the user at anytime which we use to call precomputation.
Before seeing an example, we’ll see how to set up mongodb,
We can go for mongodb instead of relation databases, when your data is sort of document data which means you just need to open the document, do the changes and save it again, it doesn’t have any relationship among them. we can also have the mongodb when we want to compute user data before accessing it and having it ready to read be the user at anytime which we use to call precomputation.
Before seeing an example, we’ll see how to set up mongodb,
Command to start the server :
<mongodb_Home_directory>/bin/mongod --dbpath <mogodb_workspace> &
<mongodb_Home_directory>/bin/mongod --dbpath <mogodb_workspace> &
Default port : 27017
DB client, we used here : Robomongo (version : 0.8.5)
Connected the mongodb server from this Robomongo and created a database named Mine. Inside the database, we can see Collections, Functions and Users. I have create my own collection named studentdocs.
Now we used these connection details and the collection in the below configuration bean. It extended the AbstractMongoConfiguration class from spring and Overrode the methods getDatabaseName and mongo which returns the Database name and the MongoClient respectively. MongoClient object was created from the hostname 127.0.0.1 and port 27017. So now, spring can take care of creating connection and managing it.
@Configuration public class SpringMongoDBConfig extends AbstractMongoConfiguration { @Override public String getDatabaseName() { return "Mine"; } @Override @Bean public Mongo mongo() throws Exception { return new MongoClient("127.0.0.1",27017); } }
Created a Student class and mapped it with the collection studentdocs we created earlier.
@Document(collection = "studentdocs") public class Student implements Serializable { @Id private String studentDocId; private String studentId; private String studentName; private String dob; private String contactNo; private Address address; private StudentCourse studentCourse; private Set<MonthAttendance> monthAttendances; private Set<Subject> subjects;//accessors and mutators are here }
Now we had the connection and collection class. We’ll see how to fetch the student records by the contactNo and by the courseId. For this, we created StudentRepository interface which extends another interface CrudRepository of spring framework.
public interface StudentRepository extends CrudRepository<Student, String> { Iterable<Student> findByContactNo(String contactNo); Iterable<Student> findByStudentCourseCourseId(String courseId); }
The first method findByContactNo will fetch the student objects which has the contactNo which we pass and return the same. Note that the second method findByStudentCourseCourseId will fetch the student object which has the courseId which we pass. Here, courseId is the property of studentCourse.
CrudRepository has some default methods like save, delete, findAll and etc. We can also use these methods.
Spring framework has the implementation for all these methods. Please see the below service class where we used all these methods.
@Service public class StudentServiceImpl implements StudentService { @Autowired private StudentRepository studentRepository; public Map<String, Iterable<Student>> getStudentsByContactNo(String contactNo) { Map<String, Iterable<Student>> studentData = new HashMap<String, Iterable<Student>>(); Iterable<Student> iStudent = studentRepository.findByContactNo(contactNo); studentData.put(contactNo,iStudent); return studentData; } public Map<String, Iterable<Student>> getStudentsByCourseId(String courseId) { Map<String, Iterable<Student>> studentData = new HashMap<String, Iterable<Student>>(); Iterable<Student> iStudent = studentRepository.findByStudentCourseCourseId(courseId); studentData.put(courseId,iStudent); return studentData; } public boolean saveContactsProducts(List<Student> students) { Iterable<Student> resultUserProducts = studentRepository.save(students); if(null != resultUserProducts) return true; return false; } }
No comments:
Post a Comment