Skip to main content

Command Palette

Search for a command to run...

Spring Boot with Mysql Database

Published
•2 min read•View as Markdown
K

Working as a Backend Developer at Nimbbl. I have 2+ experience in Java, Spring, SpringBoot, Hibernate and I'm currently working on Kotlin. Talking about my achievements I have solved 250+ problems on Leetcode as well as I’m currently an Expert on Codestudio(Level-7) and solved 180+ problems there. I am a finalist in Innovative India Coding Challenge that was organized by Coding Ninjas all over india. I have a decent problem solving skills.

Spring Boot is a popular framework for building enterprise applications, and MySQL is a widely-used relational database management system. Connecting a Spring Boot application with a MySQL database is a common requirement in many projects. In this blog, we'll go through the steps to connect a Spring Boot application with a MySQL database.

Step 1: Add the MySQL dependency First, we need to add the MySQL dependency to the project. Open the pom.xml file and add the following dependency:

<dependency>

<groupId>mysql</groupId>

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

<version>8.0.27</version>

</dependency>

This will add the MySQL JDBC driver to the project.

Step 2: Configure the database properties Next, we need to configure the database properties in the application.properties file. Open the file and add the following properties:

spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Here, we are configuring the database URL, username, password, and driver class.

Step 3: Create a model class Next, we need to create a model class that represents the database table. For example, if we have a table called "employees", we can create a model class like this:

@Entity @Table(name = "employees")

public class Employee {

@Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id;

private String name;

private String department;

// getters and setters

}

Here, we are using JPA annotations to map the class to the "employees" table in the database.

Step 4: Create a repository interface Next, we need to create a repository interface that extends the JpaRepository interface. This interface will provide all the CRUD operations for our model class. For example, if we have a model class called "Employee", we can create a repository interface like this:

@Repository

public interface EmployeeRepository extends JpaRepository<Employee, Long> { }

Here, we are using the JpaRepository interface to provide all the CRUD operations for the Employee class.

Step 5: Use the repository in the controller Finally, we need to use the repository in the controller to perform database operations. For example, if we want to retrieve all employees from the database, we can create a controller method like this:

@RestController @RequestMapping("/employees")

public class EmployeeController {

@Autowired private EmployeeRepository employeeRepository;

@GetMapping("/") public List<Employee> getAllEmployees() {

return employeeRepository.findAll();

}

}

Here, we are using the findAll() method of the EmployeeRepository to retrieve all employees from the database.

That's it! Now you know how to connect a Spring Boot application with a MySQL database. By following these steps, you can easily create a database-backed Spring Boot application.