build.gradle
file will look like the one below. apply plugin: 'java' repositories { mavenCentral() } dependencies { compile group: 'org.springframework.data', name: 'spring-data-mongodb', version: '2.0.5.RELEASE' implementation 'com.google.guava:guava:23.0' testImplementation 'junit:junit:4.12' }
build.gradle
file build.gradle
in the apply plugin: 'java'
line apply plugin: 'java'
it tells you which plugin to install. In our case, this is a Java plugin.repositories{}
tag reports the repository from which dependencies should be pulled. We chose mavenCentral
to pull up dependent jar files. We can also use jcenter to pull the corresponding dependent jar files.dependencies {}
tag is used to provide the necessary data about the jar files that need to be pulled up for the project.AbstractMongoConfiguration
class. The MongoConfig.java
class will look like the one below. Here we use annotations instead of xml. But XML can also be used to customize the configuration. package com.tutorial.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import com.mongodb.MongoClient; @Configuration public class MongoConfig extends AbstractMongoConfiguration { @Override public String getDatabaseName() { return "local"; } @Override @Bean public MongoClient mongoClient() { return new MongoClient("127.0.0.1"); } }
@Configuration
used to define the MongoConfig.java
class as a configuration class. @Bean
defines a @Bean
bin.student.java
as a model class that contains attributes for Student, such as Name and Age. The class model Student.java
used to map the POJO to the MongoDB collection. package com.tutorial.model; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = "students") public class Student { public Student(String studentName, int studentAge) { this.studentName = studentName; this.studentAge = studentAge; } @Id private String id; String studentName; int studentAge; public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getStudentAge() { return studentAge; } public void setStudentAge(int studentAge) { this.studentAge = studentAge; } @Override public String toString() { return String.format( "Student[id=%s, studentName='%s', studentAge="+studentAge+"]", id, studentName); } }
@Document
defines the document. The collection
property determines the collection that will be used to match the collection. All attributes that are referred to as part of the collection must be available in the POJO class. @Id
defines the collection id.MongoOperations
.MongoDBPOperations.java
class. This class contains the implementation of all methods of CRUD operations. package com.tutorial; import java.util.List; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import com.tutorial.model.Student; public class MongoDBPOperations { public void saveStudent(MongoOperations mongoOperation, Student student) { mongoOperation.save(student); System.out.println("Student saved successfully"); // student object got created with id. System.out.println("student : " + student); } public void searchStudent(MongoOperations mongoOperation, String critera,String value) { // query to search student Query searchStudent = new Query(Criteria.where(critera).is(value)); // find student based on the query Student resultStudent = mongoOperation.findOne(searchStudent, Student.class); System.out.println("Student found!!"); System.out.println("Student details: " + resultStudent); } public void updateStudent(MongoOperations mongoOperation, String critera,String value, String updateCriteria, String updateValue) { // query to search student Query searchStudent = new Query(Criteria.where(critera).is(value)); mongoOperation.updateFirst(searchStudent, Update.update(updateCriteria, updateValue), Student.class); System.out.println("Student got updated successfully"); } public void getAllStudent(MongoOperations mongoOperation) { List listStudent = mongoOperation.findAll(Student.class); for(Student student:listStudent) { System.out.println("Student = " + student); } } public void removeStudent(MongoOperations mongoOperation, String critera,String value) { Query searchStudent = new Query(Criteria.where(critera).is(value)); mongoOperation.remove(searchStudent, Student.class); System.out.println("Student removed successfully!! "); } }
main
method.Application.java
class. We will use this class to call methods from the MongoDBPOperations
class. package com.tutorial; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.data.mongodb.core.MongoOperations; import com.tutorial.config.MongoConfig; import com.tutorial.model.Student; public class Application { public static void main (String[] args) { // For Annotation ApplicationContext ctx = new AnnotationConfigApplicationContext(MongoConfig.class); MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate"); MongoDBPOperations ops = new MongoDBPOperations(); Student student = new Student("John", 15); //save student ops.saveStudent(mongoOperation, student); // get student based on search criteria ops.searchStudent(mongoOperation, "studentName", "John"); //update student based on criteria ops.updateStudent(mongoOperation, "StudentName", "John", "studentAge", "18"); // get student based on search criteria ops.searchStudent(mongoOperation, "studentName", "John"); // get all the students ops.getAllStudent(mongoOperation); //remove student based on criteria ops.removeStudent(mongoOperation, "studentName", "John"); // get all the students ops.getAllStudent(mongoOperation); } }
Application.java
class:ApplicationContext
. This is due to the need to load the configuration.MongoOperations
object was MongoOperations
to load the MongoTemplate
component.MongoDBOperations
object provides access to methods for performing various MongoOperation
operations.saveMethod
method of the saveMethod
class, and pass the necessary parameters to save the object in the database.MongoDBOperations
various methods of MongoDBOperations
one after another. package com.tutorial; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.data.mongodb.core.MongoOperations; import com.tutorial.config.MongoConfig; import com.tutorial.model.Student; public class Application { public static void main (String[] args) { // For Annotation ApplicationContext ctx = new AnnotationConfigApplicationContext(MongoConfig.class); MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate"); MongoDBPOperations ops = new MongoDBPOperations(); Student student = new Student("John", 15); //save student ops.saveStudent(mongoOperation, student); // get student based on search criteria ops.searchStudent(mongoOperation, "studentName", "John"); //update student based on criteria ops.updateStudent(mongoOperation, "StudentName", "John", "studentAge", "18"); // get student based on search criteria ops.searchStudent(mongoOperation, "studentName", "John"); // get all the students ops.getAllStudent(mongoOperation); //remove student based on criteria //ops.removeStudent(mongoOperation, "studentName", "John"); // get all the students //ops.getAllStudent(mongoOperation); } }
Source: https://habr.com/ru/post/430520/
All Articles