Employee Record is a Spring Boot Web Application that allows you to add, update, delete & view employees.
Employee Record is a Spring Boot Web Application that allows you to add, update, delete & view employees.
pom.xml (Project Object Model file)
<dependencies>
<!-- Dependency for Spring Boot Web Application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Dependency for testing the Spring Boot Application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
SpringBootApplication.java (Main file)
import org.springframework.boot.SpringApplication;
@org.springframework.boot.autoconfigure.SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
}
CREATE TABLE employee (
emp_id INT(3) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(255),
last_name VARCHAR(255),
email VARCHAR(255),
gender VARCHAR(255),
dob VARCHAR(255),
age INT(3),
salary DECIMAL(10,2),
status VARCHAR(255)
);
pom.xml
<!-- Dependency for Spring Boot Data JPA which allows us to access and persist data between Java object/class and relational database -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Dependency for MySQL driver to integrate MySQL Database with the Spring Boot Application -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
application.properties
# Connect MySQL Database
spring.datasource.platform=mysql
spring.datasource.url=jdbc:mysql://localhost:3306/employee_record
spring.datasource.username=root
spring.datasource.password=YourPassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Spring Data JPA & Hibernate properties
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.open-in-view=false
spring.jpa.properties.hibernate.id.new_generator_mappings=false
Employee.java
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "emp_id")
private int employeeId;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
@Column(name = "gender")
private String gender;
@Column(name = "dob")
private String dob;
@Column(name = "age")
private int age;
@Column(name = "salary")
private double salary;
@Column(name = "status")
private String status;
}
EmployeeRepository.java
@Repository
public interface EmployeeRepository extends JpaRepository<Employee,Integer> {
}
EmployeeController.java
@Controller
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@RequestMapping(value ="/", method = RequestMethod.GET)
public RedirectView redirectToIndex() {
return new RedirectView("http://localhost:8080/employees");
}
}
@RequestMapping(value ="/employees", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/employees/add", method = RequestMethod.POST)
public @ResponseBody ResponseEntity addNewEmployee(@RequestBody Map<String,String> body) {
if (isEmployee(body.get("email"))) {
return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE);
} else {
String firstName = StringUtils.capitalize(body.get("firstName").trim());
String lastName = StringUtils.capitalize(body.get("lastName").trim());
String email = body.get("email").trim();
String gender = body.get("gender").trim();
String dob = body.get("dob").trim();
double salary = Double.parseDouble(body.get("salary").trim());
String status = body.get("status").trim();
// Save form data to the database
try {
Employee emp = new Employee(firstName,lastName,email,gender,dob,salary,status);
employeeRepository.save(emp);
} catch (Exception e) {
return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE);
}
return new ResponseEntity(HttpStatus.CREATED);
}
@RequestMapping(value = "/employees/edit/{id}", method = RequestMethod.PUT)
public @ResponseBody ResponseEntity updateEmployee(@RequestBody Map<String,String> body, @PathVariable int id) {
String firstName = StringUtils.capitalize(body.get("firstName").trim());
String lastName = StringUtils.capitalize(body.get("lastName").trim());
String email = body.get("email").trim();
String gender = body.get("gender").trim();
String dob = body.get("dob").trim();
double salary = Double.parseDouble(body.get("salary").trim());
String status = body.get("status").trim();
// Update form data in the database
try {
Employee emp = new Employee();
emp.setEmployeeId(id);
emp.setFirstName(firstName);
emp.setLastName(lastName);
emp.setEmail(email);
emp.setGender(gender);
emp.setDob(dob);
emp.setAge(dob);
emp.setSalary(salary);
emp.setStatus(status);
employeeRepository.save(emp);
} catch (Exception e){
return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE);
}
return new ResponseEntity(HttpStatus.CREATED);
}
@RequestMapping(value = "/employees/delete/{id}", method = RequestMethod.DELETE)
public @ResponseBody ResponseEntity deleteAnEmployee(@PathVariable String id){
int empId = Integer.parseInt(id);
if (isEmployeeId(empId)) {
employeeRepository.deleteById(empId);
return new ResponseEntity(HttpStatus.OK);
} else {
return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE);
}
}
Drop your queries at dev.paurav@gmail.com.