"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring Boot - Layered Application Date : 03/04/2025 (Session - 19) _____________________________________________________________________________________________________________________________ SpringBoot Layered Application ============================== * We will be developing the Spring boot layered Application 1) Controller Component 2) Service Component 3) Dao Component * We are establishing the communication between these components Controller --------------------------> Service ------------------------> DAO -------------------> Database(Oracle) * In the Spring Layered Application we implemented the DAO logic using "Spring Jdbc Module" 1) spring-jdbc-version.jar 2) spring-tx-version.jar 3) programmer need to create Spring Bean objects of "DriverManagerDataSource" & "JdbcTemplate". * In Spring Boot layered Application will using one below starter 1) Spring Boot Jdbc Starter 2) We no need to worry about creating the DriverManagerDatasource & JdbcTemplate objects and these objects are automatically creating in Spring boot through "AutoConfiguration" NOTE ==== * Inorder to create DriverManagerDataSource & JdbcTemplate class objects as programmer need to configure the Jdbc driver details such as JdbcdriverClassname,Jdbcurl,databaseusername,database password in application.properties * DataSource(I) <------------------------------------ DriverManagerDataSource(IC) >>>>>>>>>>> Spring Framework <------------------------------------ BasicDataSource(IC) >>>>>>>>>>>>>>>>>>> Apache Software Foundation <------------------------------------ HikariDataSource(IC) >>>>>>>>>>>>>>>>>> Hikari Corporation EmployeeController.java ======================= package com.ashokit.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.ashokit.model.Employee; import com.ashokit.service.EmployeeService; @Controller public class EmployeeController { @Autowired private EmployeeService empService; public void addingNewEmployee(Employee employee) { //calling the service method empService.createBrandNewEmployee(employee); } public void addingNewEmployees(List employees) { //calling the service method empService.createBrandNewEmployees(employees); } } EmployeeService.java ==================== package com.ashokit.service; import java.util.List; import com.ashokit.model.Employee; public interface EmployeeService { public void createBrandNewEmployee(Employee employee); public void createBrandNewEmployees(List employees); } EmployeeServiceImpl.java ======================== package com.ashokit.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ashokit.dao.EmployeeDao; import com.ashokit.model.Employee; @Service public class EmployeeServiceImpl implements EmployeeService { @Autowired private EmployeeDao employeeDao; @Override public void createBrandNewEmployee(Employee employee) { //calling the DAO Method boolean employeeCreatedStatus = employeeDao.createNewEmployee(employee); if(employeeCreatedStatus) { System.out.println("Employee Created SuccessFully.........."); } } @Override public void createBrandNewEmployees(List employees) { //Calling the DAO Method int employeeCreatedcount = employeeDao.creatNewEmployees(employees); System.out.println("Employee Created Count:::"+employeeCreatedcount+" Successfully"); } } EmployeeDao.java ================ package com.ashokit.dao; import java.util.List; import com.ashokit.model.Employee; public interface EmployeeDao { //creating the new employee public boolean createNewEmployee(Employee emp); //creating the employees based on List public int creatNewEmployees(List employees); } EmployeeDaoImpl.java ==================== package com.ashokit.dao; import java.util.List; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import com.ashokit.model.Employee; @Repository public class EmployeeDaoImpl implements EmployeeDao { //Will be getting as spring bean through Auto-configuration @Autowired private JdbcTemplate jdbcTemplate; @Autowired private DataSource datasource; @Override public boolean createNewEmployee(Employee emp) { System.out.println("DataSource Implementation class...." + datasource.getClass().getName()); int rowCount = jdbcTemplate.update("insert into ashokit_employees values(?,?,?)", emp.getEmployeeId(),emp.getEmployeeName(),emp.getEmailId()); //If row inserted successful means will return true otherwise will return as false return rowCount > 0; } @Override public int creatNewEmployees(List employees) { System.out.println("DataSource Implementation class...." + datasource.getClass().getName()); int rowCount=0; for(Employee emp : employees) { rowCount += jdbcTemplate.update("insert into ashokit_employees values(?,?,?)", emp.getEmployeeId(),emp.getEmployeeName(),emp.getEmailId()); } System.out.println("Row Count Checking Variable ::::::" + rowCount); return rowCount; } } Employee.java ============= package com.ashokit.model; public class Employee { private Integer employeeId; private String employeeName; private String emailId; public Employee() { } public Employee(Integer employeeId, String employeeName, String emailId) { this.employeeId = employeeId; this.employeeName = employeeName; this.emailId = emailId; } public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } public Integer getEmployeeId() { return employeeId; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public String getEmployeeName() { return employeeName; } public void setEmailId(String emailId) { this.emailId = emailId; } public String getEmailId() { return emailId; } @Override public String toString() { return "Employee [employeeId=" + employeeId + ", employeeName=" + employeeName + ", emailId=" + emailId + "]"; } } EmployeeUtils.java ================== package com.ashokit.util; import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Component; import com.ashokit.model.Employee; @Component public class EmployeeUtils { //Return the List object public List getAllEmployeeInfo(){ List empList = new ArrayList(); try( FileReader fr = new FileReader("src/main/resources/ashokit_employees.txt"); //Reading the file data as line by line BufferedReader br = new BufferedReader(fr); ){ //Need to define logic to read the data from file String currentRecord; while((currentRecord = br.readLine()) != null) { //getting the current line and spliting based on comma seperated value String[] employeeValues = currentRecord.split(","); //extract the employee values from each record Integer empId = Integer.valueOf(employeeValues[0]); String empName = employeeValues[1]; String emailId = employeeValues[2]; //adding current employee information into List object empList.add(new Employee(empId,empName,emailId)); } }catch(Exception e) { e.printStackTrace(); } //returning the employeeList return empList; } } Application.java ================= package com.ashokit; import java.util.List; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.ashokit.controller.EmployeeController; import com.ashokit.model.Employee; import com.ashokit.util.EmployeeUtils; @SpringBootApplication public class Application { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(Application.class, args); //getting the Spring Bean objects EmployeeController ec = context.getBean(EmployeeController.class); EmployeeUtils emputils = context.getBean(EmployeeUtils.class); List employees = emputils.getAllEmployeeInfo(); //calling the controller class method //ec.addingNewEmployee(employees.get(0)); //calling the controller class method to test list of employees creating ec.addingNewEmployees(emputils.getAllEmployeeInfo()); } } application.properties ====================== spring.application.name=10_SpringBoot_Layered_App #configuration database properties spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/62_springbootms_db spring.datasource.username=root spring.datasource.password=root OUTPUT ===== . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ DataSource Implementation Class from SpringBoot::::com.zaxxer.hikari.HikariDataSource New Employee got onboarded... IIQ) RowMapper