Spring Boot CRUD API with MySQL Guide
Spring Boot CRUD API with MySQL Guide
Check out my 10+ Udemy bestseller courses and discount coupons: Udemy Courses - Ramesh Fadatare
Java JavaEE Library REST Spring Boot Microservices Full Stack YouTube UI Quiz Hibernate DB Programs
Kotlin Python Me
This tutorial will teach you how to build CRUD REST APIs using Spring Boot 3, Spring Data JPA, and MySQL Database.
We’ll first build the APIs to create, retrieve, update and delete a user, then test them using postman.
Note that we are using the latest version of Spring boot which is version 3.
Refer to the below screenshot to enter details while creating the spring boot application using the spring initializr:
[Link] 1/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
Click on Generate button to download the Spring boot project as a zip file. Unzip the zip file and import the Spring boot project
in IntelliJ IDEA.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
[Link] 2/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
<groupId>[Link]</groupId>
Tutorials Guides Annotations Interview Quizzes
Java Guides <artifactId>lombok</artifactId>
YouTube Udemy MCQs SCE
<optional>true</optional>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>[Link]</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>[Link]
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>[Link]
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
2. Project Structure
Refer to the below screenshot to create a project structure or a packing structure for our Spring boot application:
[Link] 3/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
[Link]=jdbc:mysql://localhost:3306/user_management
[Link]=root
[Link]=Mysql@123
[Link]=[Link]
[Link]-auto=update
Don’t forget to change the [Link] and [Link] as per your MySQL installation.
Also, create a database named user_management in MySQL before proceeding to the next section.
You don’t need to create any tables. The tables will automatically be created by Hibernate from the User entity that we will
define in the next step. This is made possible by the property [Link]-auto = update.
Let's create a User JPA entity class with the following fields:
id - primary key
firstName - user first name
lastName - user last name
email - user email ID
[Link] 4/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = [Link])
private Long id;
@Column(nullable = false)
private String firstName;
@Column(nullable = false)
private String lastName;
@Column(nullable = false, unique = true)
private String email;
}
Note that we are using Lombok annotations to reduce the boilerplate code such as getter/setter methods, and constructors.
We are using below JPA annotations to map an Entity with a database table:
@Table annotation is used to provide the details of the table that this entity will be mapped to.
@GeneratedValue annotation is used to define the primary key generation strategy. In the above case, we have declared the
@Column annotation is used to define the properties of the column that will be mapped to the annotated field. You can define
Well, Spring Data JPA comes with a JpaRepository interface that defines methods for all the CRUD operations on the entity,
package [Link];
import [Link];
import [Link];
[Link] 5/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
UserService Interface
package [Link];
import [Link];
import [Link];
List<User> getAllUsers();
UserServiceImpl Class
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {
@Override
public User createUser(User user) {
return [Link](user);
}
@Override
public User getUserById(Long userId) {
Optional<User> optionalUser = [Link](userId);
return [Link]();
}
@Override
public List<User> getAllUsers() {
[Link] 6/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
return [Link]();
Tutorials Guides Annotations Interview Quizzes
Java Guides }
YouTube Udemy MCQs SCE
@Override
public User updateUser(User user) {
User existingUser = [Link]([Link]()).get();
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
User updatedUser = [Link](existingUser);
return updatedUser;
}
@Override
public void deleteUser(Long userId) {
[Link](userId);
}
}
Let's create the REST APIs for creating, retrieving, updating, and deleting a User :
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
@RestController
@AllArgsConstructor
@RequestMapping("api/users")
public class UserController {
[Link] 7/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
// Build Get All Users REST API
Tutorials Guides Annotations Interview Quizzes
Java Guides // [Link]
YouTube Udemy MCQs SCE
@GetMapping
public ResponseEntity<List<User>> getAllUsers(){
List<User> users = [Link]();
return new ResponseEntity<>(users, [Link]);
}
container(embedded tomcat).
1. From the root directory of the application and type the following command to run it -
$ mvn spring-boot:run
2. From your IDE, run the [Link]() method as a standalone Java class that will start the
embedded Tomcat server on port 8080 and point the browser to [Link]
[Link] 8/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
[Link] 9/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
[Link] 10/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
10. Conclusion
Congratulations guys! We successfully built a Restful CRUD API using Spring Boot 3, Spring Data JPA, and MySQL database.
Spring Boot Kafka Microservices Spring Boot + Apache Kafka Tutorial Spring Core Tutorial
Spring MVC Tutorial Spring Data JPA Tutorial Spring Framework For Beginners Spring AOP Tutorial
Spring Security Tutorial Spring Exceptions Tutorial Spring Boot Interview Questions
Spring Boot Microservices Interview Questions Apache Kafka Tutorials Docker Tutorials And Guides
Spring Boot RabbitMQ Tutorials Angular CRUD Example With Spring Boot
Spring Boot + Angular 12 CRUD Full Stack Spring Boot + Angular 8 CRUD Full Stack
Spring Boot + Angular 10 CRUD Full Stack Spring Boot + React JS CRUD Full Stack
React JS ( React Hooks) + Spring Boot Spring Boot Thymeleaf CRUD Full Stack
Spring Boot User Registration And Login Node Js + Express + MongoDB CRUD
[Link] 11/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
YouTube 130K
[Link] 12/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
[Link] 13/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
About Me
Follow Me on Twitter
Follow
[Link] 14/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
Learn Spring Boot 3 Learn Java 8 Java 8 Quiz
Tutorials Guides Annotations Interview Quizzes
Java Guides
Learn Spring MVC Learn Spring Boot Java Coding Quiz
YouTube Udemy MCQs SCE
Learn Spring Data JPA Learn Spring Framework Spring Boot Quiz
Learn Spring Boot React Learn Spring MVC Spring Quiz
Learn Java Collections Learn Spring Security Spring MVC Quiz
Learn Java 8 in 4 Hours Learn Spring Data JPA Spring Data JPA Quiz
Learn 25+ Spring Boot Annotations Learn Spring Annotations Hibernate Quiz
Spring Boot Kafka Microservices Learn Microservices Miroservices Quiz
Learn Building Spring Boot Projects Learn REST API REST API Quiz
Learn Spring Boot REST API Learn JPA JavaScript Quiz
Learn Event-Driven Microservices Learn Hibernate ORM JavaScript Coding Quiz
Learn Spring Boot + Kafka Learn JSP React JS Quiz
Learn Spring Boot + Angular Learn Servlet Kotlin Quiz
Learn Spring Boot + Thymeleaf Learn JUnit SQL Quiz
Learn Thymeleaf CSS Quiz
JSON Quiz
Copyright © 2018 - 2025 Java Guides All rights reversed | Privacy Policy | Contact | About Me | YouTube | GitHub
Powered by Blogger
[Link] 15/15