"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring Data JPA - CrudRepoistory(I) Methods Date : 05/10/2024 (Session - 39) _____________________________________________________________________________________________________________________________ Today Session ============== CrudRepoistory Interface Methods ================================ 1) T save(T t) ::: This method is used for Saving given entity (or) Inserting Record. 2) Iterable saveAll(Iterable items) ::: This method is used for saving group of entities. NOTE ==== * Iterable is root interface of Collection interface and came in the version of Java 1.5 * Iterable interface provides capability processing the collection object using "Enhanced For Loop" which came in the version of Java 1.5. 3) Optional findById(Serializable id) ::: To retreive record based on the given primary key value. NOTE ==== * Optional is an utility class from java8 Version which helps us to avoid the null checks in Java program. * Optional Class contains some utility methods to check whether object is present or not & We can get the object from optional object as below i) boolean isPresent() ii) T get(); 4) Iterable findAllById(Iterable ids) ::: To retreive records based on given primary key values. 5) Iterable findAll() ::: To Retreive all the records from table. 6) void delete(T t) ::: Deleting record based on given entity. 7) void deleteById(Serializable id) ::: Deleting record based on given primary key 8) void deleteAllById(Iterable ids) ::: Deleting records based on given primary key values . 9) void deleteAll() ::: Deleting all records from table with out primary key 10) void deleteAll(Iterable objects) ::: Deleting all records from table with out primary key 11) int count() :::: To get total records as count from table. 12) boolean existsById(Serializable id) ::: Check whether the record is existed or not for given primary key. ShoppingCustomerService.java ============================ package com.ashokit.service; import java.util.List; import com.ashokit.entity.ShoppingCustomer; public interface ShoppingCustomerService { //creating new customer record public ShoppingCustomer createNewBrandCustomer(ShoppingCustomer customer); //creating bulk of new customers public Iterable createBulkNewBrandCustomers(List customers); //Finding the customer By CustomerId public ShoppingCustomer findCustomerById(Integer customerId); //Finding All Customers public Iterable fetchAllCustomers(); } ShoppingCustomerServiceImpl.java ================================ package com.ashokit.service; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ashokit.dao.ShoppingCustomerRepo; import com.ashokit.entity.ShoppingCustomer; @Service public class ShoppingCustomerServiceImpl implements ShoppingCustomerService { @Autowired private ShoppingCustomerRepo customerRepo; @Override public ShoppingCustomer createNewBrandCustomer(ShoppingCustomer customer) { System.out.println("Repo Implementation Class Name::::" + customerRepo.getClass().getName()); ShoppingCustomer savedCustomerInfo = customerRepo.save(customer); return savedCustomerInfo; } @Override public Iterable createBulkNewBrandCustomers(List customers) { Iterable savedCustomers = customerRepo.saveAll(customers); return savedCustomers; } @Override public ShoppingCustomer findCustomerById(Integer customerId) { //Getting the CustomeDetails based On CustomerId Optional customerDetails = customerRepo.findById(customerId); //checking the customerDetails optional object if(customerDetails.isPresent()) { ShoppingCustomer shoppingCustomer = customerDetails.get(); return shoppingCustomer; }else { //Throw the Exception return null; } } @Override public Iterable fetchAllCustomers() { Iterable allCustomers = customerRepo.findAll(); return allCustomers; } } ShoppingCustomerController.java =============================== package com.ashokit.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.ashokit.entity.ShoppingCustomer; import com.ashokit.service.ShoppingCustomerService; @Controller public class ShoppingCustomerController { @Autowired private ShoppingCustomerService customerService; public void createNewCustomer(ShoppingCustomer customer) { ShoppingCustomer customerDetails = customerService.createNewBrandCustomer(customer); System.out.println("New Customer Got Created Successfully......"); System.out.println(customerDetails); } public void createBulkNewCustomers(List customerList) { Iterable savedCustomers = customerService.createBulkNewBrandCustomers(customerList); //forEach() from Java1.8 version which accepts Consumer FunctionaInterface as parameter //Consumer Functional Interface abstract method implementation we can provide through LambdaExpression //Consumer FI contains one abstract method i.e.,accept(T t) savedCustomers.forEach(eachCustomer -> System.out.println(eachCustomer)); } public void fetchCustomerDetailsById(Integer customerId) { ShoppingCustomer customerDetails = customerService.findCustomerById(customerId); System.out.println(customerDetails); } public void fetchAllCustomersDetails() { Iterable customerDetails = customerService.fetchAllCustomers(); //processing Java8 forEach loop customerDetails.forEach(eachCustomer -> System.out.println(eachCustomer)); } } Application.java ================= package com.ashokit; import java.util.List; 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.controller.ShoppingCustomerController; import com.ashokit.entity.ShoppingCustomer; @SpringBootApplication public class Application implements CommandLineRunner{ @Autowired private ShoppingCustomerController customerController; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { //New Customer Information ShoppingCustomer customer1 = new ShoppingCustomer(123,"Mahesh","Hyderabad","mahesh.ashokit@gmail.com","123456789"); ShoppingCustomer customer2 = new ShoppingCustomer(124,"Suresh","Pune","suresh.ashokit@gmail.com","545454454"); ShoppingCustomer customer3 = new ShoppingCustomer(125,"Rajesh","Chennai","rajesh.ashokit@gmail.com","23323232"); ShoppingCustomer customer4 = new ShoppingCustomer(126,"Ramesh","Hyderabad","ramesh.ashokit@gmail.com","121212121"); //calling the Controller class method for creating new customer //customerController.createNewCustomer(newCustomerInfo); //customerController.createBulkNewCustomers(List.of(customer1,customer2,customer3,customer4)); //customerController.fetchCustomerDetailsById(124); customerController.fetchAllCustomersDetails(); } } OUTPUT-1(Bulk Records Insertion) ======== . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/  :: Spring Boot ::   (v3.1.1) ShoppingCustomer [custId=123, name=Mahesh, location=Hyderabad, email=mahesh.ashokit@gmail.com, contactNo=123456789] ShoppingCustomer [custId=124, name=Suresh, location=Pune, email=suresh.ashokit@gmail.com, contactNo=545454454] ShoppingCustomer [custId=125, name=Rajesh, location=Chennai, email=rajesh.ashokit@gmail.com, contactNo=23323232] ShoppingCustomer [custId=126, name=Ramesh, location=Hyderabad, email=ramesh.ashokit@gmail.com, contactNo=121212121] OUTPUT-2(Finding Customer Details) ======== . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/  :: Spring Boot ::   (v3.1.1) ShoppingCustomer [custId=124, name=Suresh, location=Pune, email=suresh.ashokit@gmail.com, contactNo=545454454] OUTPUT-3(Fetching All Customers) ======== . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/  :: Spring Boot ::   (v3.1.1) ShoppingCustomer [custId=123, name=Mahesh, location=Hyderabad, email=mahesh.ashokit@gmail.com, contactNo=123456789] ShoppingCustomer [custId=124, name=Suresh, location=Pune, email=suresh.ashokit@gmail.com, contactNo=545454454] ShoppingCustomer [custId=125, name=Rajesh, location=Chennai, email=rajesh.ashokit@gmail.com, contactNo=23323232] ShoppingCustomer [custId=126, name=Ramesh, location=Hyderabad, email=ramesh.ashokit@gmail.com, contactNo=121212121] Assignment ========== * Develop the Service method & Controller Methods for findAllById(Iterable ids). * Develop the service method & controller method for Count(). * Develop the service method & controller method for deleteAllById(Iterable ids) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++