Skip to content

Commit 22a7674

Browse files
committed
Spring Boot Web MySQL Hibernate JPA JDBC
1 parent f70e2d2 commit 22a7674

File tree

10 files changed

+300
-47
lines changed

10 files changed

+300
-47
lines changed

Part-1 Spring Boot Basic Fund Projects/SpringBootSources/SpringRestAPI/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@
4343
<version>4.3.8.RELEASE</version>
4444
</dependency>
4545

46+
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
47+
<dependency>
48+
<groupId>javax.validation</groupId>
49+
<artifactId>validation-api</artifactId>
50+
<version>2.0.1.Final</version>
51+
</dependency>
52+
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
53+
<dependency>
54+
<groupId>org.apache.tomcat</groupId>
55+
<artifactId>tomcat-jasper</artifactId>
56+
<version>10.0.0-M3</version>
57+
</dependency>
58+
<dependency>
59+
60+
<groupId>mysql</groupId>
61+
62+
<artifactId>mysql-connector-java</artifactId>
63+
64+
<scope>runtime</scope>
65+
66+
</dependency>
67+
4668
<dependency>
4769
<groupId>org.springframework.boot</groupId>
4870
<artifactId>spring-boot-starter-test</artifactId>

Part-1 Spring Boot Basic Fund Projects/SpringBootSources/SpringRestAPI/src/main/java/spring/boot/EmployeeController.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

Part-1 Spring Boot Basic Fund Projects/SpringBootSources/SpringRestAPI/src/main/java/spring/boot/SpringRestClient.java

Lines changed: 0 additions & 27 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package spring.boot.controller;
2+
3+
import jdk.management.resource.ResourceRequestDeniedException;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.*;
7+
import spring.boot.exception.ResourceNotFoundException;
8+
import spring.boot.model.Employee;
9+
import spring.boot.repository.EmployeeRepository;
10+
11+
import javax.validation.Valid;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
/**
17+
* @Created 25 / 03 / 2020 - 5:21 PM
18+
* @project SpringRestAPI
19+
* @Author Hamdamboy
20+
*/
21+
@RestController
22+
@RequestMapping("/api/v1")
23+
public class EmployeeController {
24+
//
25+
@Autowired
26+
private EmployeeRepository employeeRepository;
27+
28+
@GetMapping
29+
public String Hello(){
30+
return "hello";
31+
}
32+
33+
@GetMapping("/employees")
34+
public List<Employee> getAllEmployees() {
35+
return employeeRepository.findAll();
36+
}
37+
38+
@GetMapping("/employee/{id}")
39+
public ResponseEntity<Employee> getEmployeeById(@PathVariable(value = "id") Long employeeId) throws ResourceNotFoundException {
40+
Employee employee = employeeRepository.findById(employeeId)
41+
.orElseThrow(()-> new ResourceNotFoundException("Employee not found for this id::" + employeeId));
42+
return ResponseEntity.ok().body(employee);
43+
44+
}
45+
46+
@PostMapping("/employees")
47+
public Employee createEmployee(@Valid @RequestBody Employee employee) {
48+
return employeeRepository.save(employee);
49+
}
50+
51+
@PutMapping("/employees/{id}")
52+
public ResponseEntity<Employee> updateEmployee(@PathVariable(value = "id") Long empoyeedId,
53+
@Valid @RequestBody Employee employeeDetails) throws ResourceNotFoundException {
54+
Employee employee = employeeRepository.findById(empoyeedId)
55+
.orElseThrow(()-> new ResourceNotFoundException("Employee not found for this id::" + empoyeedId));
56+
employee.setEmailId(employeeDetails.getEmailId());
57+
employee.setLastName(employeeDetails.getLastName());
58+
employee.setFirstName(employeeDetails.getFirstName());
59+
final Employee updateEmployee = employeeRepository.save(employee);
60+
return ResponseEntity.ok(updateEmployee);
61+
}
62+
63+
@DeleteMapping("/employees/{id}")
64+
public Map<String, Boolean> deleteEmployee(@PathVariable(value = "id") Long employeeId) throws ResourceNotFoundException {
65+
Employee employee = employeeRepository.findById(employeeId)
66+
.orElseThrow(()-> new ResourceRequestDeniedException("Employee not found far this id::" + employeeId));
67+
68+
employeeRepository.delete(employee);
69+
Map<String, Boolean> response = new HashMap<>();
70+
response.put("deleted", Boolean.TRUE);
71+
72+
return response;
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package spring.boot.exception;
2+
3+
import java.util.Date;
4+
5+
/**
6+
* @Created 26 / 03 / 2020 - 2:30 PM
7+
* @project SpringRestAPI
8+
* @Author Hamdamboy
9+
*/
10+
public class ErrorDetails {
11+
//
12+
private Date timestamp;
13+
private String message;
14+
private String details;
15+
16+
17+
public ErrorDetails(Date timestamp, String message, String details) {
18+
super();
19+
this.timestamp = timestamp;
20+
this.message = message;
21+
this.details = details;
22+
}
23+
24+
public Date getTimestamp() {
25+
return timestamp;
26+
}
27+
28+
public String getMessage(){
29+
return message;
30+
}
31+
32+
public String getDetails(){
33+
return details;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package spring.boot.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.ControllerAdvice;
6+
import org.springframework.web.bind.annotation.ExceptionHandler;
7+
import org.springframework.web.context.request.WebRequest;
8+
9+
import java.util.Date;
10+
11+
/**
12+
* @Created 26 / 03 / 2020 - 2:32 PM
13+
* @project SpringRestAPI
14+
* @Author Hamdamboy
15+
*/
16+
17+
@ControllerAdvice
18+
public class GlobalExceptionHandler {
19+
//
20+
@ExceptionHandler(ResourceNotFoundException.class)
21+
public ResponseEntity<?> resourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
22+
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
23+
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
24+
}
25+
26+
@ExceptionHandler(Exception.class)
27+
public ResponseEntity<?> globalExceptionHandler(Exception ex, WebRequest request) {
28+
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
29+
return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package spring.boot.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
/**
7+
* @Created 26 / 03 / 2020 - 2:17 PM
8+
* @project SpringRestAPI
9+
* @Author Hamdamboy
10+
*/
11+
12+
@ResponseStatus(value = HttpStatus.NOT_FOUND)
13+
public class ResourceNotFoundException extends Exception{
14+
15+
private static final long serialVersionUID = 1L;
16+
17+
public ResourceNotFoundException(String message) {
18+
super(message);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package spring.boot.model;
2+
3+
import javax.persistence.Column;
4+
5+
import javax.persistence.Entity;
6+
7+
import javax.persistence.GeneratedValue;
8+
9+
import javax.persistence.GenerationType;
10+
11+
import javax.persistence.Id;
12+
13+
import javax.persistence.Table;
14+
/**
15+
* @Created 26 / 03 / 2020 - 12:06 PM
16+
* @project SpringRestAPI
17+
* @Author Hamdamboy
18+
*/
19+
@Entity
20+
@Table(name = "restapi")
21+
public class Employee {
22+
//
23+
private long id;
24+
private String firstName;
25+
private String lastName;
26+
private String emailId;
27+
28+
public Employee() {
29+
}
30+
31+
@Id
32+
@GeneratedValue(strategy = GenerationType.AUTO)
33+
public long getId() {
34+
return id;
35+
}
36+
37+
public void setId(long id) {
38+
this.id = id;
39+
}
40+
41+
@Column(name = "first_name", nullable = false)
42+
43+
public String getFirstName() {
44+
45+
return firstName;
46+
47+
}
48+
49+
public void setFirstName(String firstName) {
50+
51+
this.firstName = firstName;
52+
53+
}
54+
55+
56+
57+
@Column(name = "last_name", nullable = false)
58+
59+
public String getLastName() {
60+
61+
return lastName;
62+
63+
}
64+
65+
public void setLastName(String lastName) {
66+
67+
this.lastName = lastName;
68+
69+
}
70+
71+
72+
73+
@Column(name = "email_address", nullable = false)
74+
75+
public String getEmailId() {
76+
77+
return emailId;
78+
79+
}
80+
81+
public void setEmailId(String emailId) {
82+
83+
this.emailId = emailId;
84+
85+
}
86+
87+
88+
@Override
89+
public String toString() {
90+
return "Employee{" +
91+
"id=" + id +
92+
", firstName='" + firstName + '\'' +
93+
", lastName='" + lastName + '\'' +
94+
", emailId='" + emailId + '\'' +
95+
'}';
96+
}
97+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package spring.boot.repository;
22

3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
import spring.boot.model.Employee;
6+
37
/**
48
* @Created 25 / 03 / 2020 - 5:24 PM
59
* @project SpringRestAPI
610
* @Author Hamdamboy
711
*/
8-
public class EmployeeRepository {
12+
13+
@Repository
14+
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
915

1016
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11

2+
## Spring DATASOURCE (DataSource AutoConfiguration & DataSourceProperties)
3+
4+
spring.datasource.url = jdbc:mysql://localhost:3306/management?useSSL=false
5+
6+
spring.datasource.username=root
7+
spring.datasource.password=posilka2020
8+
9+
## Hibernate Properties
10+
#The SQL dialect makes Hibernate generate better SQL the chosen database
11+
12+
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
13+
14+
#Hibernate dll auto (create, create-drop, validate, update)
15+
spring.jpa.hibernate.ddl-auto=update

0 commit comments

Comments
 (0)