Setting up a database connection
In every application, there is a need to access some data and conduct some operations on it. Most frequently, this source of data is a datastore of some kind, namely a database. Spring Boot makes it very easy to get started in order to connect to the database and start consuming the data via the JPA, among others.
Getting ready
In our previous example, we created the basic application that will execute a command-line runner by printing a message in the logs. Let's enhance this application by adding a connection to a database.
Earlier, we already added the necessary jdbc
and data-jpa
starters as well as an H2 database dependency to our build
file. Now we will configure an in-memory instance of the H2 database.
Note
In the case of an embedded database, such as H2, Hyper SQL Database (HSQLDB), or Derby, no actual configuration is required besides including the dependency on one of these in the build
file. When one of these databases is detected in the classpath and...