page_type | description | languages | products | urlFragment | ||
---|---|---|---|---|---|---|
sample |
This sample shows how to use Spring Data JPA module with different Azure Database services. |
|
|
spring-jpa-azure |
This sample shows how to use Spring Data JPA module with different Azure Database services.
- Azure Account
- JDK 1.8 or above
- Maven 3.0 or above
- Curl
- Database Client
- MySQL CLI
- pgAdmin for PostgreSQL
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.
-
Create an Azure Database for MySQL server by following tutorial at here.
-
Configure a firewall rule to allow your machine accessing the created MySQL server by following tutorial at here.
-
Use
mysql
to connect to your MySQL server and create a database namedmysqldb
by following tutorial at here. -
Find
application.properties
atsrc/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 ofjdbc:mysql://<MySQL Host>:3306/mysqldb?useSSL=true&requireSSL=false
-
Package the sample application by running below command.
mvn package -P mysql
-
Create an Azure Database for PostgreSQL server by following tutorial at here.
-
Configure a firewall rule to allow your machine accessing the created PostgreSQL server by following tutorial at here.
-
Use
pgAdmin
to connect to your PostgreSQL server and create a database namedmypgsqldb
by following tutorial at here. -
Find
application.properties
atsrc/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 ofjdbc:postgresql://<PostgreSQL Host>:5432/mypgsqldb?ssl=true&sslmode=prefer
-
Package the sample application by running below command.
mvn package -P postgresql
-
Create an Azure SQL Database by following tutorial at here.
-
Create a firewall rule to allow your machine accessing the created Azure SQL Database by following tutorial at here.
-
Find
application.properties
atsrc/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 ofjdbc:sqlserver://{SQL Host}:1433;database=sqldb;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
-
Package the sample application by running below command.
mvn clean package -P sql
Following below steps to run and test the sample application.
-
Run application.
java -jar target/spring-data-jpa-on-azure-0.1.0-SNAPSHOT.jar
-
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.
Added Pet(id=1, name=Tom, species=cat). ... Added Pet(id=2, name=Jerry, species=mouse).
-
Get all existing pets by running below command.
curl -s http://localhost:8080/pets
Sample output is as below.
[{"id":1,"name":"Tom","species":"cat"},{"id":2,"name":"Jerry","species":"mouse"}]