oktatas:web:back-end_framework:spring_boot:dolgozok_crud_taroloval
Tartalomjegyzék
Dolgozók Crud tárolóval
- Szerző: Sallai András
- Copyright © Sallai András, 2023
- 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
- src/main/resources/application.properties
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
<dependencies> <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>
Belépésipont
- EmpApplication.java
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
- Emplyoee.java
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.
- EmployeeRespository.java
package lan.zold.emp; import org.springframework.data.repository.CrudRepository; public interface EmployeeRepository extends CrudRepository<Employee, Integer> {}
Kontroller
Adatok lekérdezése:
- EmployeeController.java
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(); } }
Futtatás parancssorból
mvn spring-boot:run
oktatas/web/back-end_framework/spring_boot/dolgozok_crud_taroloval.txt · Utolsó módosítás: 2023/09/05 22:14 szerkesztette: admin