In my previous articles for Spring Data JPA, I have used in-memory databases like H2 for persisting data. But, these in-memory databases can not be used for the production. These are only used for the development environment. In this post I will explain the basic configurations for setting up the MYSQL database for your Spring Data JPA and Spring Boot applications.
Here I am going to use the same example used in my previous tutorials. I am just going to replace the database for the sample application. Note that this tutorial uses Spring Boot for deploying the sample application. If you are not using Spring Boot, this may not be much useful to configure your applications.
Here is the required configurations to use MySQL database to your Spring Boot application.
In the maven’s
pom.xml, you just need to add mysql connector. Once this is added to the classpath, Spring Boot will automatically search for the MySQL configurations and use it.
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
Next step is to add the MySQL configurations in the
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=admin spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Database Schema / Data
This is only optional for you. If you want to create the tables and load the data at the start up, you have to create
data.sql. The file name of the schema can be either
drop TABLE IF EXISTS book; create table book( id int not null primary key, name varchar_ignorecase(50) not null, author varchar_ignorecase(50) not null, price int, creation_time datetime);
I hope in this tutorial you have understood how to use MySQL configurations for spring boot applications. You can download the complete sample application here: