Spring Boot + Microsoft SQL Server + JPA/Hibernate CRUD Restful API Tutorial

spring-boot-tutorial-master.zip

spring-boot-tutorial-master.zip
 
In this article, you’ll learn how to configure Spring Boot to use Microsoft SQL server database and build a RESTful CRUD API for Employee Management System.

You’ll also learn how Spring Data JPA and Hibernate can be used with a Microsoft SQL database. Before development, make sure that the MS-SQL server is installed on your machine.

Spring makes switching between RDBM’s simple. When you’re using Spring Data JPA with an ORM technology such as Hibernate, the persistence layer is nicely well decoupled. As we are using Hibernate so which will support out of the box to work with different database vendor without changing underlying code.

You may like these similar articles with different databases:

SQL Server Dependencies

To connect with SQL Server from Java applications, Microsoft provides a Microsoft JDBC Driver for SQL Server. However, till November 2016, Maven did not directly support the driver as it was not open source. By making it open source, Microsoft finally made the driver available on the Maven Central Repository. More information can be found here.

Follow these quick two steps to configure Microsoft SQL server in Spring boot application:
 
Step 1: Provide MS-SQL driver dependency in your pom.xml file:
 
<dependency>
 <groupId>com.microsoft.sqlserver</groupId>
 <artifactId>sqljdbc4</artifactId>
 <version>4.0</version>
</dependency>
Step 2: Let’s configure Spring Boot to use MS-SQL server database as our data source. We are simply adding Microsoft SQL server URL, username, and password in the src/main/resources/application.properties file -
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=employees
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql = true

## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2012Dialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
That's all. Now you are good to go.
 
Let's develop a complete CRUD RESTFul APIs for a Simple Employee Management System using Spring Boot 2 JPA and Microsoft SQL database.

Table of Contents

  1. What we’ll build?
  2. Tools and Technologies Used
  3. Creating and Importing a Project
  4. Packaging Structure
  5. The pom.xml File
  6. Configuring MS-SQL Server Database
  7. Create JPA Entity - Employee.java
  8. Create Spring Data Repository - EmployeeRepository.java
  9. Create Spring Rest Controller - EmployeeController.java
  10. Exception(Error) Handling for RESTful Services
  11. Running Application
  12. Integration Testing for REST APIs
  13. Testing REST APIs via Postman Client
  14. Source code on GitHub Repository

1. What we’ll build

We will build a CRUD RESTFul APIs for a Simple Employee Management System using Spring Boot 2 JPA and MS-SQL database. Following are five REST APIs (Controller handler methods) are created for Employee resource.

2. Tools and Technologies Used

  • Spring Boot - 2.0.4.RELEASE
  • JDK - 1.8 or later
  • Spring Framework - 5.0.8 RELEASE
  • Hibernate - 5.2.17.Final
  • JPA
  • Maven - 3.2+
  • IDE - Eclipse or Spring Tool Suite (STS)
  • Microsoft SQL server - 4.0

3. Creating and Importing a Project

There are many ways to create a Spring Boot application. The simplest way is to use Spring Initializr, which is an online Spring Boot application generator.
 
 
Look at the above diagram, we have specified the following details:
  • Generate: Maven Project
  • Java Version: 1.8 (Default)
  • Spring Boot:2.0.4
  • Group: net.guides.springboot2
  • Artifact: springboot2-mssql-jpa-hibernate-crud-example
  • Name: springboot2-mssql-jpa-hibernate-crud-example
  • Description: springboot2-mssql-jpa-hibernate-crud-example
  • Package Name : net.guides.springboot2.crud
  • Packaging: jar (This is the default value)
  • Dependencies: Web, JPA
Once, all the details are entered, click on Generate Project button will generate a spring boot project and downloads it. Next, Unzip the downloaded zip file and import it into your favorite IDE.

4. Packaging Structure

Following is the packing structure of our Employee Management System -
 

5. The pom.xml File

Note that this pom.xml file contains MS-SQL database driver:
<?xml version="1.0" encoding="UTF-8"?>
<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.guides.springboot2</groupId>
    <artifactId>springboot2-mssql-jpa-hibernate-crud-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>springboot2-mssql-jpa-hibernate-crud-example</name>
    <description>springboot2-mssql-jpa-hibernate-crud-example</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath />
        <!-- lookup parent from reposictory -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

6. Configuring MS-SQL Server Database

Let’s configure Spring Boot to use Microsoft SQL server as our data source. You can do that simply by adding Microsoft SQL database URL, username, and password in the src/main/resources/application.properties file -
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=employees
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql = true

## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2012Dialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

7. Create JPA Entity - Employee.java

package net.guides.springboot2.crud.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "employees")
public class Employee {

    private long id;
    private String firstName;
    private String lastName;
    private String emailId;
 
    public Employee() {
  
    }
 
    public Employee(String firstName, String lastName, String emailId) {
         this.firstName = firstName;
         this.lastName = lastName;
         this.emailId = emailId;
    }
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
        public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
 
    @Column(name = "first_name", nullable = false)
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
 
    @Column(name = "last_name", nullable = false)
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
 
    @Column(name = "email_address", nullable = false)
    public String getEmailId() {
        return emailId;
    }
    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", emailId=" + emailId
       + "]";
    }
 
}

8. Create Spring Data Repository - EmployeeRepository.java

package net.guides.springboot2.crud.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import net.guides.springboot2.springboot2jpacrudexample.model.Employee;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{

}

9. Create Spring Rest Controller - EmployeeController.java

package net.guides.springboot2.crud.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
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.RestController;

import net.guides.springboot2.springboot2jpacrudexample.exception.ResourceNotFoundException;
import net.guides.springboot2.springboot2jpacrudexample.model.Employee;
import net.guides.springboot2.springboot2jpacrudexample.repository.EmployeeRepository;

@RestController
@RequestMapping("/api/v1")
public class EmployeeController {
    @Autowired
    private EmployeeRepository employeeRepository;

    @GetMapping("/employees")
    public List<Employee> getAllEmployees() {
        return employeeRepository.findAll();
    }

    @GetMapping("/employees/{id}")
    public ResponseEntity<Employee> getEmployeeById(@PathVariable(value = "id") Long employeeId)
        throws ResourceNotFoundException {
        Employee employee = employeeRepository.findById(employeeId)
          .orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
        return ResponseEntity.ok().body(employee);
    }
    
    @PostMapping("/employees")
    public Employee createEmployee(@Valid @RequestBody Employee employee) {
        return employeeRepository.save(employee);
    }

    @PutMapping("/employees/{id}")
    public ResponseEntity<Employee> updateEmployee(@PathVariable(value = "id") Long employeeId,
         @Valid @RequestBody Employee employeeDetails) throws ResourceNotFoundException {
        Employee employee = employeeRepository.findById(employeeId)
        .orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));

        employee.setEmailId(employeeDetails.getEmailId());
        employee.setLastName(employeeDetails.getLastName());
        employee.setFirstName(employeeDetails.getFirstName());
        final Employee updatedEmployee = employeeRepository.save(employee);
        return ResponseEntity.ok(updatedEmployee);
    }

    @DeleteMapping("/employees/{id}")
    public Map<String, Boolean> deleteEmployee(@PathVariable(value = "id") Long employeeId)
         throws ResourceNotFoundException {
        Employee employee = employeeRepository.findById(employeeId)
       .orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));

        employeeRepository.delete(employee);
        Map<String, Boolean> response = new HashMap<>();
        response.put("deleted", Boolean.TRUE);
        return response;
    }
}

10. Exception(Error) Handling for RESTful Services

Spring Boot provides a good default implementation for exception handling for RESTful Services. Let’s quickly look at the default Exception Handling features provided by Spring Boot.

Resource Not Present

Heres what happens when you fire a request to not resource found: http://localhost:8080/some-dummy-url
{
  "timestamp": 1512713804164,
  "status": 404,
  "error": "Not Found",
  "message": "No message available",
  "path": "/some-dummy-url"
}
That's a cool error response. It contains all the details that are typically needed.

What happens when we throw an Exception?

Let’s see what Spring Boot does when an exception is thrown from a Resource. we can specify the Response Status for a specific exception along with the definition of the Exception of ‘@ResponseStatus’ annotation.
Lets create a ResourceNotFoundException.java class.
package net.guides.springboot2.crud.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends Exception{

    private static final long serialVersionUID = 1L;

    public ResourceNotFoundException(String message){
        super(message);
    }
}

Customizing Error Response Structure

Default error response provided by Spring Boot contains all the details that are typically needed.
However, you might want to create a framework independent response structure for your organization. In that case, you can define a specific error response structure.
Let’s define a simple error response bean.
package net.guides.springboot2.crud.exception;

import java.util.Date;

public class ErrorDetails {
    private Date timestamp;
    private String message;
    private String details;

    public ErrorDetails(Date timestamp, String message, String details) {
         super();
         this.timestamp = timestamp;
         this.message = message;
         this.details = details;
    }

    public Date getTimestamp() {
         return timestamp;
    }

    public String getMessage() {
         return message;
    }

    public String getDetails() {
         return details;
    }
}
To use ErrorDetails to return the error response, let’s create a GlobalExceptionHandler class annotated with @ControllerAdvice annotation. This class handles exception specific and global exception in a single place.
package net.guides.springboot2.crud.exception;

import java.util.Date;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<?> resourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
         ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
         return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<?> globleExcpetionHandler(Exception ex, WebRequest request) {
        ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
        return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

11. Running Application

This spring boot application has an entry point Java class called SpringBootCrudRestApplication.java with the public static void main(String[] args) method, which you can run to start the application.
import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
@SpringBootApplication is a convenience annotation that adds all of the following:
  • @Configuration tags the class as a source of bean definitions for the application context.
  • @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
  • Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
  • @ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.
The main() method uses Spring Boot’s SpringApplication.run() method to launch an application.
 

12. Integration Testing for REST APIs


There is a separate beautiful article for integration testing for REST APIs on:

>> Spring Boot 2 REST APIs Integration Testing

13. Testing REST APIs via Postman Client

1. Create Employee REST API

 
HTTP Method: POST 
Note that request and response JSON in the above diagram, the response contains database auto generated id.

2. Get Employee by ID REST API

HTTP Method: GET 

3. Get all Employees REST API

HTTP Method: GET 

4. Update Employee REST API

HTTP Method: PUT

5. Delete Employee REST API

HTTP Method: DELETE 

14. Source code on GitHub

 
The source code of this tutorial is available on my GitHub Repository
>> https://github.com/RameshMF/spring-boot-tutorial/tree/master/springboot2-mssql-jpa-hibernate-crud-example

 

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

[출처] https://www.javaguides.net/2019/01/spring-boot-microsoft-sql-server-jpa-hibernate-crud-restful-api-tutorial.html

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
166 [java][JAVA web][JSP] JSTL의 기본 개념과 사용방법 정리 졸리운_곰 2021.04.22 97
165 [java][jsp][EL/JSTL 정리] EL/JSTL 정리 file 졸리운_곰 2021.04.22 42
164 [Redis] [Spring] Spring session을 Redis에 저장했을 때 어떻게 저장될까? file 졸리운_곰 2021.04.11 111
163 [Java Web programming] Java 시스템 운영 중 알아두면 쓸모 있는 지식들 file 졸리운_곰 2021.03.07 69
162 React와 JSP(java server page) file 졸리운_곰 2021.01.09 232
161 [MongoDB] 몽고디비와 JSP 연동 방법! 조회 후 레코드 값 저장 방법! 졸리운_곰 2021.01.05 57
160 [SpringBoot] Hello world! ( IntelliJ + Gradle + SpringBoot ) file 졸리운_곰 2020.09.10 71
159 [visual studio code] 스프링 부트 + 리액트 개발 셋업 file 졸리운_곰 2020.07.01 1464
158 Eclipse의 Workspace와 IntelliJ의 Project file 졸리운_곰 2020.05.03 88
157 [IntelliJ] 전자정부 프로젝트 IntelliJ에서 세팅 file 졸리운_곰 2020.05.03 89
156 Intellij JSP - MySQL 연동 file 졸리운_곰 2020.03.26 186
155 Spring Boot with React 설정 file 졸리운_곰 2020.03.05 1022
154 Spring SQL Server 연동 (실습) file 졸리운_곰 2020.01.24 758
153 전자정부프레임워크 MSSQL 연동하기 졸리운_곰 2020.01.24 562
152 전자정부프레임워크 mssql 연동 졸리운_곰 2020.01.24 113
151 Spring Boot::DB 에 한글이 깨져서 저장될 때 해결 방법. 졸리운_곰 2020.01.24 78
150 Configuring Spring Boot for Microsoft SQL Server file 졸리운_곰 2020.01.24 76
149 SQL Server 에게 String 이란? (NVARCHAR 인가 VARCHAR 인가) 졸리운_곰 2020.01.24 72
» Spring Boot + Microsoft SQL Server + JPA/Hibernate CRUD Restful API Tutorial file 졸리운_곰 2020.01.24 1506
147 MS SQL Server에서 JPA NativeQuery 사용시 유니코드(Unicode) 문제 file 졸리운_곰 2020.01.24 1923
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED