Skip to content

Commit 32ec36c

Browse files
author
rfadatare
committed
added springboot2-mssql-jpa-hibernate-crud-example
1 parent 1813ece commit 32ec36c

File tree

14 files changed

+444
-0
lines changed

14 files changed

+444
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>net.guides.springboot2</groupId>
8+
<artifactId>springboot2-mssql-jpa-hibernate-crud-example</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>springboot2-mssql-jpa-hibernate-crud-example</name>
13+
<description>springboot2-mssql-jpa-hibernate-crud-example</description>
14+
15+
<parent>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-parent</artifactId>
18+
<version>2.0.5.RELEASE</version>
19+
<relativePath /> <!-- lookup parent from reposictory -->
20+
</parent>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25+
<java.version>1.8</java.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-jpa</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>com.microsoft.sqlserver</groupId>
40+
<artifactId>sqljdbc4</artifactId>
41+
<version>4.0</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-maven-plugin</artifactId>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package net.guides.springboot2.crud;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
6+
7+
@SpringBootApplication
8+
@EnableJpaAuditing
9+
public class Application {
10+
public static void main(String[] args) {
11+
SpringApplication.run(Application.class, args);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package net.guides.springboot2.crud.controller;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
import javax.validation.Valid;
8+
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.http.ResponseEntity;
11+
import org.springframework.web.bind.annotation.DeleteMapping;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.PathVariable;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.PutMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.RestController;
19+
20+
import net.guides.springboot2.crud.exception.ResourceNotFoundException;
21+
import net.guides.springboot2.crud.model.Employee;
22+
import net.guides.springboot2.crud.repository.EmployeeRepository;
23+
24+
@RestController
25+
@RequestMapping("/api/v1")
26+
public class EmployeeController {
27+
@Autowired
28+
private EmployeeRepository employeeRepository;
29+
30+
@GetMapping("/employees")
31+
public List<Employee> getAllEmployees() {
32+
return employeeRepository.findAll();
33+
}
34+
35+
@GetMapping("/employees/{id}")
36+
public ResponseEntity<Employee> getEmployeeById(@PathVariable(value = "id") Long employeeId)
37+
throws ResourceNotFoundException {
38+
Employee employee = employeeRepository.findById(employeeId)
39+
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
40+
return ResponseEntity.ok().body(employee);
41+
}
42+
43+
@PostMapping("/employees")
44+
public Employee createEmployee(@Valid @RequestBody Employee employee) {
45+
return employeeRepository.save(employee);
46+
}
47+
48+
@PutMapping("/employees/{id}")
49+
public ResponseEntity<Employee> updateEmployee(@PathVariable(value = "id") Long employeeId,
50+
@Valid @RequestBody Employee employeeDetails) throws ResourceNotFoundException {
51+
Employee employee = employeeRepository.findById(employeeId)
52+
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
53+
54+
employee.setEmailId(employeeDetails.getEmailId());
55+
employee.setLastName(employeeDetails.getLastName());
56+
employee.setFirstName(employeeDetails.getFirstName());
57+
final Employee updatedEmployee = employeeRepository.save(employee);
58+
return ResponseEntity.ok(updatedEmployee);
59+
}
60+
61+
@DeleteMapping("/employees/{id}")
62+
public Map<String, Boolean> deleteEmployee(@PathVariable(value = "id") Long employeeId)
63+
throws ResourceNotFoundException {
64+
Employee employee = employeeRepository.findById(employeeId)
65+
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
66+
67+
employeeRepository.delete(employee);
68+
Map<String, Boolean> response = new HashMap<>();
69+
response.put("deleted", Boolean.TRUE);
70+
return response;
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package net.guides.springboot2.crud.exception;
2+
3+
import java.util.Date;
4+
5+
public class ErrorDetails {
6+
private Date timestamp;
7+
private String message;
8+
private String details;
9+
10+
public ErrorDetails(Date timestamp, String message, String details) {
11+
super();
12+
this.timestamp = timestamp;
13+
this.message = message;
14+
this.details = details;
15+
}
16+
17+
public Date getTimestamp() {
18+
return timestamp;
19+
}
20+
21+
public String getMessage() {
22+
return message;
23+
}
24+
25+
public String getDetails() {
26+
return details;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.guides.springboot2.crud.exception;
2+
3+
import java.util.Date;
4+
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.ControllerAdvice;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
import org.springframework.web.context.request.WebRequest;
10+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
11+
12+
@ControllerAdvice
13+
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
14+
@ExceptionHandler(ResourceNotFoundException.class)
15+
public ResponseEntity<?> resourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
16+
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
17+
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
18+
}
19+
20+
@ExceptionHandler(Exception.class)
21+
public ResponseEntity<?> globleExcpetionHandler(Exception ex, WebRequest request) {
22+
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
23+
return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package net.guides.springboot2.crud.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
@ResponseStatus(value = HttpStatus.NOT_FOUND)
7+
public class ResourceNotFoundException extends Exception{
8+
9+
private static final long serialVersionUID = 1L;
10+
11+
public ResourceNotFoundException(String message){
12+
super(message);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package net.guides.springboot2.crud.model;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
import javax.persistence.Table;
9+
10+
@Entity
11+
@Table(name = "employees")
12+
public class Employee {
13+
14+
private long id;
15+
private String firstName;
16+
private String lastName;
17+
private String emailId;
18+
19+
public Employee() {
20+
21+
}
22+
23+
public Employee(String firstName, String lastName, String emailId) {
24+
this.firstName = firstName;
25+
this.lastName = lastName;
26+
this.emailId = emailId;
27+
}
28+
29+
@Id
30+
@GeneratedValue(strategy = GenerationType.IDENTITY)
31+
public long getId() {
32+
return id;
33+
}
34+
public void setId(long id) {
35+
this.id = id;
36+
}
37+
38+
@Column(name = "first_name", nullable = false)
39+
public String getFirstName() {
40+
return firstName;
41+
}
42+
public void setFirstName(String firstName) {
43+
this.firstName = firstName;
44+
}
45+
46+
@Column(name = "last_name", nullable = false)
47+
public String getLastName() {
48+
return lastName;
49+
}
50+
public void setLastName(String lastName) {
51+
this.lastName = lastName;
52+
}
53+
54+
@Column(name = "email_address", nullable = false)
55+
public String getEmailId() {
56+
return emailId;
57+
}
58+
public void setEmailId(String emailId) {
59+
this.emailId = emailId;
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", emailId=" + emailId
65+
+ "]";
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package net.guides.springboot2.crud.repository;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
import net.guides.springboot2.crud.model.Employee;
7+
8+
@Repository
9+
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
2+
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=employees
3+
spring.datasource.username=sa
4+
spring.datasource.password=
5+
spring.jpa.show-sql=true
6+
spring.jpa.properties.hibernate.format_sql = true
7+
8+
## Hibernate Properties
9+
# The SQL dialect makes Hibernate generate better SQL for the chosen database
10+
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2012Dialect
11+
12+
# Hibernate ddl auto (create, create-drop, validate, update)
13+
spring.jpa.hibernate.ddl-auto = update
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package net.guides.springboot2.springboot2jpacrudexample;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class ApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)