[[oktatas:web:back-end_framework:spring_boot|< Spring boot]]
====== Dolgozók Crud 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
===== 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
A spring.jpa.hibernate.ddl-auto tulajdonság CrudRepository esetén hatástalan.
A Spring Boot a Hibernate ORM-t használja.
===== Függőségek =====
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ésipont =====
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 Crud Repository-t használjuk.
package lan.zold.emp;
import org.springframework.data.repository.CrudRepository;
public interface EmployeeRepository extends CrudRepository {}
===== Kontroller =====
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