Start United States USA — software First Steps With GCP SQL

First Steps With GCP SQL

254
0
TEILEN

In this post, we will take a look at how we can use Google Cloud Platform (GCP) SQL as a database for our Spring Boot application.
Let’s be friends:
Comment (0)
Join the DZone community and get the full member experience.
In this post, we will take a look at how we can use Google Cloud Platform (GCP) SQL as a database for our Spring Boot application. We will investigate how we can use the Cloud database from our development machine and how we can use it from GCP itself.
First, you will need to create a GCP account. Check out the first paragraph of a previous post on how to do so.
We will create a Spring Boot MVC web application, which uses a PostgreSQL database in the Cloud. Go to start.spring.io and create a Java 11 Web MVC project. The application will simply return a list of Shows when accessing a shows URL. The sources are available at GitHub.
The only domain object is Show, which has an id and a title. The getters and setters are left out for brevity.
For accessing the database, we will use JdbcTemplate. We only define a findAll method in the ShowDao, which retrieves all Shows from the database. We use a ShowRowMapper in order to map the table columns to the Showdomain object.
The ShowService will retrieve the data from the database via the ShowDaoobject.
And finally, we define a shows URL that routes to the shows method from the ShowController and that will return the values from the database as JSON.
In file src/main/resources/schema.sql, we define the Show table:
In file src/main/resources/data.sql, we define some contents for the Showtable:
In the pom.xml file, we need a dependency for Spring Web MVC and a dependency for Spring Cloud GCP PostgreSQL.
Now that we have created our application, it is time to set up the database and the necessary configuration in our application in order to run it from our local development machine.
In the GCP console, go to SQL and create a SQL instance.
We have the choice between MySQL and PostgreSQL. We choose PostgreSQL.
Fill in the necessary configuration options. We set the Instance ID to myspringcloud, set up a password for the postgres user, choose a region, and click the Create button.
After a minute or so, the SQL instance is created.

Continue reading...