"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring Data With MongoDB Date : 28/10/2024 (Session - 55) _____________________________________________________________________________________________________________________________Last Session ============ * We completed interacting with MonogDB using MongoDBCompass(GUI). Today Session ============= * When we are working with Spring Data JPA Module we created the Entity Class using @Entity Annotation and inorder to perform curd operations on SQL Database we used CrudRepoistory,JPARepoistory,PagingAndSortingRepoistory. * When we are working with Spring Data MongoDB Module we have to create the Document Class using @Document Annotation and inorder to perform CURD operations on MongoDB We have to use MongoRepoistory Interface (or) MongoTemplate Class. Developing the spring Boot Application using MongoDB ==================================================== 1) Create the spring boot application with the following starter i.e.,Spring Boot data starter MongoDB 2) Create Document class & Dao Interface as below Employee.java ============= package com.ashokit.entity; import java.util.Arrays; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document public class Employee { @Id private String id; private String name; private String designation; private String[] emailIds; private String contactNo; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public String[] getEmailIds() { return emailIds; } public void setEmailIds(String[] emailIds) { this.emailIds = emailIds; } public String getContactNo() { return contactNo; } public void setContactNo(String contactNo) { this.contactNo = contactNo; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", designation=" + designation + ", emailIds=" + Arrays.toString(emailIds) + ", contactNo=" + contactNo + "]"; } } package com.ashokit.entity; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = "customers") public class Customer { @Id private Integer customerId; private String customerName; private String cusotmerlocation; public Integer getCustomerId() { return customerId; } public void setCustomerId(Integer customerId) { this.customerId = customerId; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public String getCusotmerlocation() { return cusotmerlocation; } public void setCusotmerlocation(String cusotmerlocation) { this.cusotmerlocation = cusotmerlocation; } @Override public String toString() { return "Customer [customerId=" + customerId + ", customerName=" + customerName + ", cusotmerlocation=" + cusotmerlocation + "]"; } } EmployeeDao.java ================ package com.ashokit.dao; import org.springframework.data.mongodb.repository.MongoRepository; import com.ashokit.entity.Employee; public interface EmployeeDao extends MongoRepository { } package com.ashokit.dao; import org.springframework.data.mongodb.repository.MongoRepository; import com.ashokit.entity.Customer; public interface CustomerDao extends MongoRepository { } package com.ashokit.dao; import org.springframework.data.mongodb.repository.MongoRepository; import com.ashokit.entity.Customer; public interface CustomerDao extends MongoRepository { } Application.java ================= package com.ashokit; import java.util.List; import java.util.Optional; import java.util.Random; import java.util.UUID; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.ashokit.dao.CustomerDao; import com.ashokit.dao.EmployeeDao; import com.ashokit.entity.Customer; import com.ashokit.entity.Employee; @SpringBootApplication public class Application implements CommandLineRunner{ @Autowired private EmployeeDao employeeDao; @Autowired private CustomerDao customerDao; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { //creating Employee Object /*Employee emp = new Employee(); //setting id property value explictly System.out.println("UUID.randomUUID().toString()" + UUID.randomUUID().toString()); emp.setId(UUID.randomUUID().toString()); emp.setName("Suresh"); emp.setDesignation("System Analyst"); emp.setContactNo("123456789"); emp.setEmailIds(new String[]{"suresh.ashokit@gmail.com","sureshashokit@gmail.com"}); //saving the emp object information Employee savedEmployee = employeeDao.save(emp); //displaying the saved employee System.out.println("Employee Record got Created:::::" + savedEmployee.getId()); */ System.out.println("************************************"); Optional employeeOpt = employeeDao.findByNameAndDesignation("Suresh1", "System Analyst"); if(employeeOpt.isPresent()) { System.out.println(employeeOpt.get()); }else { System.out.println("Employee Not Found...."); } System.out.println("************************************"); List allEmployees = employeeDao.getAllEmployeesByDesignation("System Analyst"); allEmployees.forEach(System.out::println); System.out.println("************************************"); Customer cust = new Customer(); cust.setCustomerId(new Random().nextInt(5000)); cust.setCustomerName("Mahesh"); //cust.setCusotmerlocation("Hyderabad"); Customer savedCustomer = customerDao.save(cust); System.out.println(savedCustomer); } } application.properties ====================== #MongoDB Connection Properties spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=maheshit OUTPUT ====== . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ Employee Record got Created:::::63d1eed2e520d11495988235) Differences between SQL Databases and No-SQL Databases ====================================================== SQL Databases ============= 1) SQL Databases are primarily called as RDBMS (or) Realational Databases. 2) By using SQL language to interact with Relational Database for storing and Retrieving the data. 3) Structured Query Language (SQL) i.e.,Declarative Query language. 4) SQL Databases are table based databases. 5) SQL Databases having predefined schema used for structured data only. 6) SQL Databases are vertically scalabe. 7) It was developed in the 1970's to deal with issues with flat files,storage. 8) A Mix of Postgresql,MySQL and commerical like oracle databases. 9) Cross-platform support,secure and free. 10) Mostly used for banking projects (or) large projects No-SQL Databases ================ 1) No-SQL Databases are primarily called as Non-Relational Database (or) distributed databases. 2) No-SQL Database system consists of various kind of database technologies.These databases are developed in response to the demands for modern application developement. 3) No declarative query language. 4) No-SQL Databases are key-value based,document based,graph based etc., 5) No-SQL Databases use dynamic schema for unstructured data only. 6) No-SQL Databases are horizontal Scalabe. 7) Developed in the late of 2000's to overcome issues and limitations of SQL Databases. 8) Open-Source 9) Easy to use,High performance,High Availabaility and Flexible tool. 10) Mostly used for medium scale (or) large scale projects. Assignments =========== * Writing some custom Queries in EmployeeDao (or) CustomerDao with below references https://javatechonline.com/spring-boot-mongodb-query-examples/ * Test the Pagination & Sorting functionalities in SpringData MonogoDB Application. NOTE ==== Github Url For Spring Data JPA Application : https://github.com/maheshashokit/47_SBMS_Workspace Email-ID : mahesh.ashokit@gmail.com +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++