Navigation überspringen

Spring Data JPA on Azure

This sample shows how to use Spring Data JPA module with different Azure Database services.

TOC

Prerequisite

  • Azure Account
  • JDK 1.8 or above
  • Maven 3.0 or above
  • Curl
  • Database Client
    • MySQL CLI
    • pgAdmin for PostgreSQL

Build and Package

You can build this sample with different Azure Database services. SQL Server, MySQL and PostgreSQL are supported by Azure. Follow below sections to use one of them.

Azure Database for MySQL

  1. Create an Azure Database for MySQL server by following tutorial at here.

  2. Configure a firewall rule to allow your machine accessing the created MySQL server by following tutorial at here.

  3. Use mysql to connect to your MySQL server and create a database named mysqldb by following tutorial at here.

  4. Find application.properties at src/main/resources directory and fill in below properties.

    spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
    
    spring.datasource.url=<replace with your database server URL>
    spring.datasource.username=<replace with your database server username>
    spring.datasource.password=<replace with your database server password>
    

    NOTE spring.datasource.url should be in the form of jdbc:mysql://<MySQL Host>:3306/mysqldb?useSSL=true&requireSSL=false

  5. Package the sample application by running below command.

    mvn package -P mysql
    

Azure Database for PostgreSQL

  1. Create an Azure Database for PostgreSQL server by following tutorial at here.

  2. Configure a firewall rule to allow your machine accessing the created PostgreSQL server by following tutorial at here.

  3. Use pgAdmin to connect to your PostgreSQL server and create a database named mypgsqldb by following tutorial at here.

  4. Find application.properties at src/main/resources directory and fill in below properties.

    spring.datasource.url=<replace with your database server URL>
    spring.datasource.username=<replace with your database server username>
    spring.datasource.password=<replace with your database server password>
    

    NOTE spring.datasource.url should be in the form of jdbc:postgresql://<PostgreSQL Host>:5432/mypgsqldb?ssl=true&sslmode=prefer

  5. Package the sample application by running below command.

    mvn package -P postgresql
    

Azure SQL Database

  1. Create an Azure SQL Database by following tutorial at here.

  2. Create a firewall rule to allow your machine accessing the created Azure SQL Database by following tutorial at here.

  3. Find application.properties at src/main/resources directory and fill in below properties.

    spring.datasource.url=<replace with your database server URL>
    spring.datasource.username=<replace with your database server username>
    spring.datasource.password=<replace with your database server password>
    

    NOTE spring.datasource.url should be in the form of jdbc:sqlserver://{SQL Host}:1433;database=sqldb;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;

  4. Package the sample application by running below command.

    mvn clean package -P sql
    

Run

Following below steps to run and test the sample application.

  1. Run application.

    java -jar target/spring-data-jpa-on-azure-0.1.0-SNAPSHOT.jar
    
  2. Create new users by running below command.

    curl -s -d '{"name":"Tom","species":"cat"}' -H "Content-Type: application/json" -X POST http://localhost:8080/pets
    curl -s -d '{"name":"Jerry","species":"mouse"}' -H "Content-Type: application/json" -X POST http://localhost:8080/pets
    

    Sample output is as below. text Added Pet(id=1, name=Tom, species=cat). ... Added Pet(id=2, name=Jerry, species=mouse).

  3. Get all existing pets by running below command.

    curl -s http://localhost:8080/pets
    

    Sample output is as below. txt [{"id":1,"name":"Tom","species":"cat"},{"id":2,"name":"Jerry","species":"mouse"}]