In this short blog post, I am going to share with you how to add MySQL support to your Spring Boot Web app and with Spring Boot it is actually 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 MySQL database, have a look at this blog post of mine: Spring MVC and JPA. Save data to MySQL database.
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
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
where the photo_app is the name of 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.