As we had done in Spring Boot JDBC tutorial we will first create the DAO and the Service classes.
In the application.properties file specify the datasource properties
spring.datasource.url=jdbc:mysql://localhost/cameldb
spring.datasource.username=root
spring.datasource.password=rida
spring.datasource.platform=mysql
Create the schema-mysql.sql file and specify the initialization scripts-
DROP TABLE IF EXISTS employee;
CREATE TABLE employee (
empId VARCHAR(10) NOT NULL,
empName VARCHAR(100) NOT NULL
);
Create the Employee Domain class
package com.javainuse.model;
public class Employee {
private String empId;
private String empName;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName + "]";
}
}
Create Service interface to specify employee operations to be performed.
package com.javainuse.service;
import java.util.List;
import com.javainuse.model.Employee;
public interface EmployeeService {
void insertEmployee(Employee emp);
List<Employee> getAllEmployees();
}
The Service class implementation.
package com.javainuse.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.javainuse.dao.EmployeeDao;
import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
EmployeeDao employeeDao;
@Override
public void insertEmployee(Employee employee) {
employeeDao.insertEmployee(employee);
}
@Override
public List<Employee> getAllEmployees() {
return employeeDao.getAllEmployees();
}
}
Create the DAO interface.
package com.javainuse.dao;
import java.util.List;
import com.javainuse.model.Employee;
public interface EmployeeDao {
void insertEmployee(Employee cus);
List<Employee> getAllEmployees();
}
The DAO implementation class.
package com.javainuse.dao.impl;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import com.javainuse.dao.EmployeeDao;
import com.javainuse.model.Employee;
@Repository
public class EmployeeDaoImpl extends JdbcDaoSupport implements EmployeeDao{
@Autowired
DataSource dataSource;
@PostConstruct
private void initialize(){
setDataSource(dataSource);
}
@Override
public void insertEmployee(Employee emp) {
String sql = "INSERT INTO employee " +
"(empId, empName) VALUES (?, ?)" ;
getJdbcTemplate().update(sql, new Object[]{
emp.getEmpId(), emp.getEmpName()
});
}
@Override
public List<Employee> getAllEmployees(){
String sql = "SELECT * FROM employee";
List<Map<String, Object>> rows = getJdbcTemplate().queryForList(sql);
List<Employee> result = new ArrayList<Employee>();
for(Map<String, Object> row:rows){
Employee emp = new Employee();
emp.setEmpId((String)row.get("empId"));
emp.setEmpName((String)row.get("empName"));
result.add(emp);
}
return result;
}
}
Next we create a controller class where
@RequestMapping maps /welcome request to firstPage()
method.
We also have mappings for adding and showing employees.
package com.javainuse.controllers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;
@Controller
public class EmployeeController {
@Autowired
EmployeeService employeeService;
//the welcome page
@RequestMapping("/welcome")
public ModelAndView firstPage() {
return new ModelAndView("welcome");
}
//show the add employee form and also pass an empty backing bean object to store the form field values
@RequestMapping(value = "/addNewEmployee", method = RequestMethod.GET)
public ModelAndView show() {
return new ModelAndView("addEmployee", "emp", new Employee());
}
//Get the form field vaues which are populated using the backing bean and store it in db
@RequestMapping(value = "/addNewEmployee", method = RequestMethod.POST)
public ModelAndView processRequest(@ModelAttribute("emp") Employee emp) {
employeeService.insertEmployee(emp);
List<Employee> employees = employeeService.getAllEmployees();
ModelAndView model = new ModelAndView("getEmployees");
model.addObject("employees", employees);
return model;
}
//show all employees saved in db
@RequestMapping("/getEmployees")
public ModelAndView getEmployees() {
List<Employee> employees = employeeService.getAllEmployees();
ModelAndView model = new ModelAndView("getEmployees");
model.addObject("employees", employees);
return model;
}
}