To use JPA in any project, we need to declare its configuration including the database it will use, the username and password for it to connect to, etc. With the Spring Boot project, this is no exception if you use JPA Starter dependency. So in this tutorial, I would like to guide you to configure the database in Spring Boot project with JPA Starter dependency!
First, I will create a Spring Boot project with the JPA Starter dependency. Depending on the database system you want to use, add the corresponding JDBC Driver dependency. I will use JPA with the database is MySQL so I will also add MySQL Starter dependency. The project is as follows:
If someone does not know how to create Spring Boot project in Spring Tool Suite, you guys can refer to this tutorial.
Now, if you run this project, you will encounter the following error:
1 2 3 4 5 6 7 8 9 10 11 |
*************************** APPLICATION FAILED TO START *************************** Description: Cannot determine embedded database driver class for database type NONE Action: If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active). |
What is the cause? That’s because we have not configured the database for JPA Starter dependency yet.
To resolve this error, you need to do the following:
First of all, we must have a database first.
You create a database using a database system you want like MySQL, Oracle, …
In this example, I will create a database named springboottutorial in the MySQL server.
Next, we will configure the database information for Spring Boot.
To do this, open the application.properties file in your project’s src /main/resources directory and configure the following:
1 2 3 4 |
spring.datasource.url spring.datasource.driver-class-name spring.datasource.username spring.datasource.password |
In this example, I configure the following:
1 2 3 4 |
spring.datasource.url=jdbc:mysql://localhost:3306/springboottutorial spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456 |
In the Spring Tool Suite, when you open the application.properties file, you can display all of the properties that Spring Boot supports by entering “spring.” and then pressing Ctrl + Space.
For example:
Now try running your Spring Boot project again, you will see no more errors.