[[oktatas:web:back-end_framework:spring_boot|< Spring boot]] ====== Dolgozók Jpa tárolóval ====== * **Szerző:** Sallai András * Copyright (c) Sallai András, 2023 * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]] * Web: https://szit.hu ===== GitHub ===== * https://github.com/oktat/empsb.git ===== Adatbázis ===== create database emp default character set utf8 collate utf8_hungarian_ci; create table employee( id int not null primary key auto_increment, name varchar(50), city varchar(50), salary double ); grant all privileges on emp.* to emp@localhost identified by 'titok'; ===== Adatbázis elérése ===== spring.datasource.url=jdbc:mariadb://localhost:3306/emp spring.datasource.username=emp spring.datasource.password=titok spring.datasource.driver-class-name=org.mariadb.jdbc.Driver spring.jpa.hibernate.ddl-auto=create-drop A Spring Boot a Hibernate ORM-t használja. Az utolsó sorral azt állítjuk mi, hogyan viselkedjen az adatbázissal az alkalmazásunk. | none | Az adatbázison nem változtat. | | update | Ha változás van, frissíti az adatbázis. Itt is lehet adatvesztés. | | create | Adatbázis létrehozása induláskor. | | create-drop | Adatbázis létrehozása induláskor, törlése leálláskor. | ===== Függőségek ===== Szükségünk van egy újabb függőségre: * spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-data-jdbc 3.1.3 org.mariadb.jdbc mariadb-java-client 3.2.0 org.springframework.boot spring-boot-starter-web 3.1.3 org.springframework.boot spring-boot-devtools runtime true org.springframework.boot spring-boot-starter-test test jakarta.annotation jakarta.annotation-api 2.1.1 jakarta.persistence jakarta.persistence-api 3.1.0 ===== Belépési pont ===== package lan.zold.emp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class EmpApplication { public static void main(String[] args) { SpringApplication.run(EmpApplication.class, args); } } ===== Model ===== package lan.zold.emp; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @Entity public class Employee { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; private String name; private String city; private double salary; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } } ===== Tároló ===== Kétféle tároló érhető el: * Jpa Repository * Crud Repository Itt a Jpa Repository-t használjuk. package lan.zold.emp; import org.springframework.data.jpa.repository.JpaRepository; public interface EmployeeRepository extends JpaRepository {} ===== Kontroller ===== A kontroller nem változik a Crud megoldáshoz képest. Adatok lekérdezése: package lan.zold.emp; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(path="/api") public class EmployeeController { @Autowired EmployeeRepository empRepository; @GetMapping(path="/employees") public @ResponseBody Iterable index() { return empRepository.findAll(); } } ===== Futtatás parancssorból ===== mvn spring-boot:run ===== Hozzáadás ===== @PostMapping(path="/employees") public Employee store(@RequestBody Employee emp) { Employee res = empRepository.save(emp); return res; } Tesztelés: http post http://localhost:8080/api/employees name='Valaki' city='Valahol' salary=358 Nézzük meg lekérdezéssel: http http://localhost:8080/api/employees ===== Minden művelet ===== package lan.zold.emp; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; 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.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class EmployeeController { @Autowired EmployeeRepository empRepository; @CrossOrigin @GetMapping("/employees") public @ResponseBody Iterable index() { return empRepository.findAll(); } @GetMapping("/employees/{id}") public @ResponseBody Employee show(@PathVariable Integer id) { return empRepository.findById(id).get(); } @PostMapping(path="/employees") public Employee store(@RequestBody Employee emp) { Employee res = empRepository.save(emp); return res; } @PutMapping("/employees/{id}") public Employee update(@RequestBody Employee emp, @PathVariable Integer id) { Optional orig = empRepository.findById(id); if(orig.isPresent()) { Employee emp2 = orig.get(); emp2.setName(emp.getName()); emp2.setCity(emp.getCity()); emp2.setSalary(emp.getSalary()); return empRepository.save(emp2); }else { return emp; } } @DeleteMapping("/employees/{id}") public Employee delete(@PathVariable Integer id) { Optional orig = empRepository.findById(id); empRepository.deleteById(id); return orig.get(); } }