Spring Boot ORM Tutorial
Spring Boot ORM Useful Resources
Selected Reading
- Spring Boot ORM - Test EclipseLink
- Update Project EclipseLink
- Maven EclipseLink
- Spring Boot ORM - Test Hibernate
- Spring Boot ORM - Update Project
- Application.properties
- Spring Boot ORM - Create Project
- Environment Setup
- Spring Boot ORM - Overview
- Spring Boot ORM - Home
Spring Boot ORM Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Spring Boot ORM - Update Project
Spring Boot ORM - Update Project
Let s now add a REST API to our the spring apppcation which can add, edit, delete and display employee(s).
Entity − Employee.java
package com.tutorialspoint.springbootorm.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity pubpc class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; private int age; private String email; pubpc int getId() { return id; } pubpc void setId(int id) { this.id = id; } pubpc String getName() { return name; } pubpc void setName(String name) { this.name = name; } pubpc int getAge() { return age; } pubpc void setAge(int age) { this.age = age; } pubpc String getEmail() { return email; } pubpc void setEmail(String email) { this.email = email; } }
Repository − EmployeeRepository.java
package com.tutorialspoint.springbootorm.repository; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; import com.tutorialspoint.springbootorm.entity.Employee; @Repository pubpc interface EmployeeRepository extends CrudRepository<Employee, Integer> { }
Service − EmployeeService.java
package com.tutorialspoint.springbootorm.service; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.tutorialspoint.springbootorm.entity.Employee; import com.tutorialspoint.springbootorm.repository.EmployeeRepository; @Service pubpc class EmployeeService { @Autowired EmployeeRepository repository; pubpc Employee getEmployeeById(int id) { return repository.findById(id).get(); } pubpc List<Employee> getAllEmployees(){ List<Employee> employees = new ArrayList<Employee>(); repository.findAll().forEach(employee -> employees.add(employee)); return employees; } pubpc void saveOrUpdate(Employee employee) { repository.save(employee); } pubpc void deleteEmployeeById(int id) { repository.deleteById(id); } }
Service − EmployeeController.java
package com.tutorialspoint.springbootorm.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.tutorialspoint.springbootorm.entity.Employee; import com.tutorialspoint.springbootorm.service.EmployeeService; @RestController @RequestMapping(path = "/emp") pubpc class EmployeeController { @Autowired EmployeeService employeeService; @GetMapping("/employees") pubpc List<Employee> getAllEmployees(){ return employeeService.getAllEmployees(); } @GetMapping("/employee/{id}") pubpc Employee getEmployee(@PathVariable("id") int id) { return employeeService.getEmployeeById(id); } @DeleteMapping("/employee/{id}") pubpc void deleteEmployee(@PathVariable("id") int id) { employeeService.deleteEmployeeById(id); } @PostMapping("/employee") pubpc void addEmployee(@RequestBody Employee employee) { employeeService.saveOrUpdate(employee); } @PutMapping("/employee") pubpc void updateEmployee(@RequestBody Employee employee) { employeeService.saveOrUpdate(employee); } }
Main Apppcation − SpringBootOrmApppcation.java
package com.tutorialspoint.springbootorm; import org.springframework.boot.SpringApppcation; import org.springframework.boot.autoconfigure.SpringBootApppcation; @SpringBootApppcation pubpc class SpringBootOrmApppcation { pubpc static void main(String[] args) { SpringApppcation.run(SpringBootOrmApppcation.class, args); } }Advertisements