Spring Boot + MySQL | Database connection

Spring Boot + MySQL database connection

The most important thing about an information system or computer software is to be able to save your information and that is achieved by connecting to a database.

In this tutorial I will show you how to connect to a MYSQL database. Obviously as a pre-requisite you must have installed a MYSQL server.

To connect to MYSQL you have to add the following code in your pom.xml and configure the application.properties to point to your database.

pom.xml

<!-- ORM Dependency for spring boot persistency-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- Java Connector/library for MYSQL -->
<dependency>
    <groupId>Mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

application.properties

#Data Source 
#Indica The driver/lib to connect Java to MySQL
Spring.dataSource.driver-class-name = com.mysql.cj.jdbc.driver

#Url where is the service of your MySQL and the name of the database
Spring.dataSource.url = jdbc:mysql://localhost: 3306/MyDatabase

#Usuario and password for your database described in the previous line
Spring.dataSource.username = root
Spring.dataSource.password = root

[opcional]#Imprime in your console the instructions made in your database.
spring.jpa.show-sql = True

With this you already have spring Boot connected to MYSQL, you can combine this tutorial with (Spring-boot-crud-mvc-JPA-h2-part-1/ and spring-boot-crud-mvc-JPA-relations-11-and/) to avoid using the embedded database H2.

Let me know if you have any problems or doubts.

Thanks for coming to the end of this video,

Cruizg93

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *