Add MySQL Support to Spring Boot Web App

In this short blog post, I will share with you how to add MySQL support to your Spring Boot Web app, and with Spring Boot, it is very easy.

If you are interested to learn how to add the JPA and MySQL support to your Spring Boot Web app and learn how to implement a very simple web service and how to use Spring Data JPA to save data to the MySQL database, have a look at this blog post of mine: Spring MVC and JPA. Save data to MySQL database.

You might also be interested to learn how to run MySQL Server in a Docker container on your computer.

Add MySQL Dependency to POM.XML

Open the pom.xml file of your project and add the below dependency XML element to the list of dependencies:

<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
</dependency>

If you have just created your Web app Spring Boot project then you should have the following dependencies in your pom.xml file.

<dependencies>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

 <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
 </dependency>

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>
</dependencies>

Add Database Connection Details to application.properties

The next step is to update the application.properties file in your Spring Boot web app. Open the application.properties file and add the following database connection details:

spring.datasource.url=jdbc:mysql://localhost:3306/photo_app
spring.datasource.username=sergey
spring.datasource.password=sergey
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect

where the photo_app is the name of the MySQL database, you would like to establish the connection with. Also, replace the username and the password with the username and password you use to connect to your MySQL database.

Leave a Reply

Your email address will not be published. Required fields are marked *