Spring Boot + Microsoft SQL Server + Hibernate Example

In this example, we will show you how to configure the Microsoft SQL Server database in Spring boot application.

Before development, make sure that the MS-SQL server is installed on your machine.

Read the complete tutorial at https://www.javaguides.net/2019/01/spring-boot-microsoft-sql-server-jpa-hibernate-crud-restful-api-tutorial.html
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 vendors without changing underlying code.

Two Quick Steps to configure the Microsoft SQL Server database in Spring boot application

Follow these quick two steps to configure Microsoft SQL server in Spring boot application:

Step 1. SQL Server Dependencies

To connect with SQL Server from Java applications, Microsoft provides a Microsoft JDBC Driver for SQL Server. However, until 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.

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. Add MS-SQL server database configuration in the application.properties file

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.

Check out the complete tutorial

Let's develop a complete CRUD RESTFul APIs for a Simple Employee Management System using Spring Boot 2 JPA and Microsoft SQL database at https://www.javaguides.net/2019/01/spring-boot-microsoft-sql-server-jpa-hibernate-crud-restful-api-tutorial.html.

Download source code of this example from GitHub repository 

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


Comments