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';
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. |
Szükségünk van egy újabb függőségre:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>jakarta.annotation</groupId> <artifactId>jakarta.annotation-api</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> <version>3.1.0</version> </dependency> </dependencies>
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); } }
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; } }
Kétféle tároló érhető el:
Itt a Jpa Repository-t használjuk.
package lan.zold.emp; import org.springframework.data.jpa.repository.JpaRepository; public interface EmployeeRepository extends JpaRepository<Employee, Integer> {}
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<Employee> index() { return empRepository.findAll(); } }
mvn spring-boot:run
@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
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<Employee> 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<Employee> 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<Employee> orig = empRepository.findById(id); empRepository.deleteById(id); return orig.get(); } }