A simple Spring Boot application that uses MySQL and Rest endpoints

Kambiz Z
4 min readMar 22, 2023

We go straight to the point, no clutters!

Here’s a high-level plan we will follow to build our first Spring Boot application that uses MySQL and Rest endpoints:

  1. Set up the development environment: We will install the required software and tools to develop the application, such as Java, Spring Boot, MySQL, and an IDE.
  2. Create a new Spring Boot project: We will create a new Spring Boot project using the Spring Initializr and include the required dependencies, such as Spring Web and MySQL.
  3. Create a MySQL database and table: We will create a MySQL database and table to store the data for our application.
  4. Create a Rest endpoint: We will create a Rest endpoint to retrieve the data from the MySQL database.
  5. Test the Rest endpoint: We will test the Rest endpoint using a tool such as Postman to ensure it returns the correct data.
  6. Run the application: We will run the Spring Boot application and verify that the Rest endpoint is accessible and returns the expected data.
  7. Further development: We will explore additional features such as creating, updating, and deleting data through Rest endpoints.

Let’s proceed step by step together based on this plan.

Part 1:

Before we proceed with the steps, let’s make sure you have the necessary software and tools installed. Here’s a list of what you’ll need:

  • Java Development Kit (JDK) 11 or higher
  • MySQL server
  • An IDE of your choice (e.g. Eclipse, IntelliJ IDEA, or Visual Studio Code)
  • Postman (optional, but recommended for testing the Rest endpoint)

Part 2:

Let’s start by creating a new Spring Boot project using the Spring Initializr.

2.1: Create a new Spring Boot project

  1. Open your web browser and go to the Spring Initializr website (https://start.spring.io/).
  2. In the “Project” section, select “Maven Project”.
  3. In the “Language” section, select “Java”.
  4. In the “Spring Boot” section, select the latest version of Spring Boot (currently 2.6.3).
  5. In the “Group” field, enter a unique identifier for your project (e.g. com.example).
  6. In the “Artifact” field, enter a name for your project (e.g. springboot-mysql-demo).
  7. In the “Dependencies” section, search for “Spring Web” and select it.
  8. Search for “Spring Data JPA” and select it.
  9. Search for “MySQL Driver” and select it.
  10. Click on the “Generate” button to download a ZIP file containing your project.

2.2: Import the project into your IDE

  1. Extract the downloaded ZIP file to a folder on your machine.
  2. Open your IDE and import the project as a Maven project.
  3. Once the project is imported, you should see a file called “DemoApplication.java” in the “src/main/java/com/example/springbootmysqldemo” folder.

2.3: Create a MySQL database and table

  1. Open your MySQL client (e.g. MySQL Workbench) and connect to your MySQL server.
  2. Create a new database by running the following SQL command:
CREATE DATABASE springboot_mysql_demo;
  1. Create a new table by running the following SQL command:
CREATE TABLE user (   id INT PRIMARY KEY AUTO_INCREMENT,   name VARCHAR(255),   email VARCHAR(255) );

2.4: Create a Rest endpoint

  1. Open the "DemoApplication.java" file in your IDE.
  2. Add the following code to the file to define a Rest controller:
package com.example.springbootmysqldemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

@Autowired
private UserRepository userRepository;

@GetMapping("/")
public List<User> getAllUsers() {
return userRepository.findAll();
}

}

3. Add the following code to create a “User” class:

package com.example.springbootmysqldemo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

4. Add the following code to create a “UserRepository” interface:

   package com.example.springbootmysqldemo;

import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {

}

2.5: Configure the MySQL database

  1. Open the “application.properties” file located in the “src/main/resources” folder.
  2. Add the following code to the file to configure the MySQL database:
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_mysql_demo
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

Make sure to replace “root” and “password” with your MySQL username and password, respectively.

2.6: Run the application

  1. In your IDE, right-click on the “DemoApplication.java” file and select “Run As” > “Spring Boot App”.
  2. Open a web browser and navigate to http://localhost:8080/users/. You should see an empty JSON array ([]).
  3. Use Postman (or any other REST client) to send a GET request to http://localhost:8080/users/. You should receive an empty JSON array.

2.7: Add some data to the database

  1. Use a MySQL client (e.g. MySQL Workbench) to insert some data into the “user” table.

For example, you could run the following SQL command:

INSERT INTO user (name, email) VALUES ('John Doe', 'john.doe@example.com');

2. Use Postman (or any other REST client) to send a GET request to http://localhost:8080/users/. You should receive a JSON array containing the data you just inserted.

Congratulations! You have successfully created a Spring Boot application that uses MySQL and Rest endpoints.

Thank you for taking the time to read this article. I hope that my writing has been informative and thought-provoking.

If you’ve enjoyed this article so far, I would highly encourage you to follow me on Medium. As a follower, you’ll also have the opportunity to engage with me and other like-minded readers in the comments section. I’m always open to feedback and discussion, and I value the input of my followers greatly.

link to my other articles:
1. best practices to follow when developing REST APIs in a microservice application using Spring Boot
2. the most common Spring Boot annotations that are used in microservice applications
3. PART ONE: The most common Spring Boot annotations that are used in microservice applications — Now with examples
4. PART TWO: The most common Spring Boot annotations that are used in microservice applications — Now with examples

--

--

Kambiz Z

full-stack dev, tech enthusiast, and gadget lover. Passionate about new tools and tech, with experience in complex systems & web dev.